开发hibernate项目,可以采用手动下载hibernate jar包的方式,但由于hibernate项目的jar包较多,进行手工下载是非常繁琐的事情,可以采用maven进行hibernate包构建,可以大大的简化开发。而使用maven-hibernate 插件可以自动生成java 类和hbm配置文件。
1、首先修改项目的maven配置文件,增加hibernate 依赖项目,修改内容如下:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
2、由于使用maven-hibernate项目中的hbm2java自动生成数据库对象,以及使用hbm2hbmxml生成hbm配置文件,还需要修改项目 pom.xml文件增加相应的插件配置
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2java</name>
<!--<outputDirectory>target/hibernate3/generated-sources</outputDirectory>-->
<outputDirectory>src/main/java</outputDirectory>
</component>
<component>
<name>hbm2hbmxml</name>
<outputDirectory>src/main/resources/</outputDirectory>
</component>
<component>
<name>hbm2cfgxml</name>
<!--<outputDirectory>target/hibernate3/generated-mappings</outputDirectory>-->
<outputDirectory>src/main/resources/</outputDirectory>
</component>
<componentProperties>
<drop>true</drop>
<jdk5>true</jdk5>
<packagename>>xerlly.java.maven.HibernetTest.Model</packagename>
<configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
<!-- <propertyfile>src/main/resources/jdbc.properties</propertyfile> -->
<!-- <revengfile>src/main/resources/hibernate.reveng.xml</revengfile>-->
</componentProperties>
</components>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
</dependencies>
</plugin>