【ACCESS精品源码栏目提醒】:本文主要为网学会员提供“自定义myeclipse逆向工程模板的代码自动生成规则 - 其它资料”,希望对需要自定义myeclipse逆向工程模板的代码自动生成规则 - 其它资料网友有所帮助,学习一下!
在项目中,往往会用到 myeclipse 的自动生成代码功能,比如本人项目用其从数据库表生成对应的实体类和映射文件,但是,由于数据库表字段名称很不规范,无业务含义很难认,用myeclipse 默认的规则生成得到的实体属性名是驼峰命名, 很难由此推断出对应的数据库表字段名,故项目制定统一规范, 实体类属性名为表字段名小写。
需要修改默认的代码生成规则。
网上找了不少资料以及文档,方法总结如下:方法一:修改 myeclipse 中自动生成代码的模板文件在 myeclipse 目录中搜索关键字templates,可以找到一个如 myeclipse_templates_6.0.1GA.zip的包,解压即可得到自动生成代码用到的所有模板文件,如需修改其中几个模板文件,则按原目录结构只留下修改的模板文件。
在 Hibernate Reverse-Engineering 的对话框中,钩选 Usecustom templates,输入你存放修改后模板文件的文件夹(custom_myeclipse_templates)路径,就可以使用到自定义的模板了。
具体看参考该包的说明文档。
方法二:集成并自定义反转策略类自定义一个类,继承自 org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy,覆盖其中生成代码的方法。
具体参考方法一中那个包的说明文档。
我把其中一个例子贴出来如下,在自动生成的所有实体属性名前加ME_前缀下:5.6 sing a custom reverse engineering strategy Add the MyEclipse Persistence Tools library to your project.Right click your project and select Properties. On the Java Build Path page choose Add Library... on the Libraries tab. Choose MyEclipse Libraries and then MyEclipse Persistence Tools. Press Finish. Figure 16. Adding the MyEclipse Persistence Tools library Create a new class in your project which extends theorg.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy class. Define the single argument constructor and override a method of interest for examplecolumnToPropertyName.Heres an example which prefixes all generated propertes with ME_MyStrategy.javapackagecom.genuitec.hibernate importorg.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy importorg.hibernate.cfg.reveng.ReverseEngineeringStrategy importorg.hibernate.cfg.reveng.TableIdentifier public classMyStrategy extendsDelegatingReverseEngineeringStrategy publicMyStrategyReverseEngineeringStrategystrategy superstrategy publicString columnToPropertyNameTableIdentifier table String columnName returnME_ super.columnToPropertyNametable columnName Figure 17. MyStrategy.java Source Code Listing On page 2 of the reverse engineering wizard specify the class you just created Figure 18. Specifying the reverse engineering strategy附资料一:自定义 Struts 的 JSP 模板和 Hibernate 的 DAO 模板这一阵子在 MyEclipse 里做项目,用到了 Struts 和 hibernate 功能,起初,对自动生成赞不绝口,但后来,发现自动生成的代码也有不方便的地方,比如:一 Struts 当 new 一个 JSP,使用 Standard JSP using Struts 1.2时,生成的页面代码中只包含了 Struts 标签但很多时候, 我们需要在页面中使用到 JSTL 标签, 我们应该把常用的几个 JSTL 标签加到JSP 页 面中,就像这样: 其次,Struts1.2的 似乎有些问题,它表示的地址是当前页面的地址,而不是应用程序的根目录,因此,这个 base 我们还是得用内嵌的 Java 代码得到,就像这样:String path request.getContextPath String basePath request.getScheme://request.getServerName: request.getServerPortpath/然后在之间加入 base 标签:这样,用起来才比较