使用maven命令行进行反向hibernate reverse hbm code 代码生成

来源:互联网 发布:智能马桶圈 知乎 编辑:程序博客网 时间:2024/06/08 08:07
转:http://www.xuebuyuan.com/1152192.html
2013年06月09日
⁄ 综合⁄ 共 2258字 ⁄ 字号 小 中 大评论关闭

用了Eclipse的插件半天没搞明白怎么用,还非常慢,最后看到一外国友人写的直接用mvn生成反向代码的帖子https://community.jboss.org/message/801327#801327才成功,现在介绍一下

1. 先创建一个空白maven项目,这个项目你爱叫啥名都可以,反正只是拿来生成代码用的,用完就可以删掉了

2. 往pom.xml 里面添加plugin

<build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target><encoding>UTF-8</encoding><optimise>true</optimise><compilerArgument>-nowarn</compilerArgument></configuration></plugin>     <plugin>   <groupId>org.codehaus.mojo</groupId>   <artifactId>hibernate3-maven-plugin</artifactId>   <version>2.2</version> <configuration>   <components> <component>   <name>hbm2hbmxml</name>   <implementation>jdbcconfiguration</implementation>   <outputDirectory>target/generated-resources/hibernate3</outputDirectory> </component> <component>   <name>hbm2java</name>   <implementation>jdbcconfiguration</implementation>   <outputDirectory>target/generated-sources/hibernate3</outputDirectory> </component>   </components>   <componentProperties> <revengfile>src/main/resources/reveng.xml</revengfile> <propertyfile>src/main/resources/hibernate.properties</propertyfile> <packagename>com.whatever.domain</packagename> <jdk5>true</jdk5> <ejb3>false</ejb3>   </componentProperties> </configuration> <dependencies>   <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.2.2</version>   </dependency>   <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.25</version></dependency> </dependencies>   </plugin></plugins></build>

注意那个 com.whatever.domain 换成你自己的域名

3. 往 src/main/resources 下添加 hibernate.properties

hibernate.connection.driver_class=com.mysql.jdbc.Driverhibernate.connection.url=jdbc:mysql://xxxxxx:xxxxx/xxxxxhibernate.connection.username=xxxxxhibernate.connection.password=xxxxx

4. 往 src/main/resources 下添加 reveng.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-reverse-engineering PUBLIC  "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" ><hibernate-reverse-engineering>  <schema-selection match-schema="MATCHDB"/></hibernate-reverse-engineering>

这个 MATCHDB 就是你要反向的库名

5. 如果你要生成注解方式的 java文件,就运行 mvn hibernate3:hbm2java

如果你要生成 hbm.xml 配合 java 的形式就先把 pom 里面 <ejb3>false</ejb3> 弄成false 然后分别运行 mvn hibernate3:hbm2hbmxml 和 mvn hibernate3:hbm2java

0 0