参考资料:http://blog.csdn.net/kenhins/article/details/51233776
1.pom.xml增加hibernate自动代码插件
1)2.2版本
<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>src/main/java/</outputDirectory> </component> <component> <name>hbm2java</name> <implementation>jdbcconfiguration</implementation> <outputDirectory>src/main/java/</outputDirectory> </component> </components> <componentProperties> <revengfile>src/main/resources/hibernate.reveng.xml</revengfile> <propertyfile>src/main/resources/hibernate.properties</propertyfile> <!--<configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>--> <packagename>com.generator.hibernate</packagename> <ejb3>true</ejb3> <!-- 生成基于注解的javapojo时必须设置为true,否则为false --> <jdk5>true</jdk5> </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>
2)3.0版本
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>3.0</version> <configuration> <hibernatetool destDir="src/main/java/"> <jdbcconfiguration propertyfile="src/main/resources/hibernate.properties" revengfile="src/main/resources/hibernate.reveng.xml" packageName="com.generator.hibernate"/> <hbm2java ejb3="true" jdk5="true"/> </hibernatetool> </configuration> <dependencies> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.1.GA</version> </dependency> </dependencies> </plugin>
2)hibernate.properties
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/ssm hibernate.connection.username=ssm hibernate.connection.password=ssm
3)hibernate.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="ssm" match-table="sys_user"/> <!--匹配全部表--> <!-- <table-filter match-schema="ssm" match-name=".*"/> --> <!--匹配全部表--> <!--<schema-selection match-schema="ssm" />--> </hibernate-reverse-engineering>
4.执行
hibernate3:hbm2java :生成javapojo
hibernate3:hbm2hbmxml :生成*.hbm.xml,如果是基于注解的javapojo则不需要生成它
hibernate3:hbm2cfgxml :生成hibernate.cfg.xml