hibernate总配置文件hibernate.cfg.xml(使用该方法的前提条件)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">xuan</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/springmvc_hibernate?useUnicode=true&characterEncoding=GBK&autoReconnect=true</property>
<property name="hibernate.connection.shutdown">true</property>
</session-factory>
</hibernate-configuration>
<!--
(在Hibernate中配置hibernate.cfg.xml文件时,有些人配置Oracle 11g的dialect属性时找不到相关配置,
其实Oracle 11g和Oracle 10g的配置是一样的,都是“org.hibernate.dialect.Oracle10gDialect”。)
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@<server>:<port>:<instance> //(jdbc:oracle:thin:@127.0.0.1:1521:databaseName)
hibernate.connection.username=xuan
hibernate.connection.password=123456
-->
指定生成数据库相关数据表的配置文件:hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering
SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<!--<schema-selection match-schema="TMDCS"/>-->
<!-- <table-filter match-schema="cityhome" match-name=".*"/> -->
<schema-selection match-schema="springvmc_hibernate" match-table="student"/>
</hibernate-reverse-engineering>
<!--
<componentProperties>
<revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
</componentProperties>
在componentProperties中增加revengfile标签并指明文件的路径
关于hibernate.reveng.xml的代码示例如下:
match-schema="cityhome"//cityhome表示数据库名称,
match-table="ACCOUNT" // ACCOUNT表示cityhome数据库的ACCOUNT数据表名称
match-schema=" COMMON_SCHEMA" 数据库拥有着match-table="ACCOUNT" 表名 支持"CODES_开头的表名match-table="CODES_.*"
上述配置文件起到的作用就是在schema cityhome 中 只生成 ACCOUNT这个表的java实体类,其他的都被过滤了!
Reveng.xml有很多种反向控制,这里只介绍了最简单的一种,如果不满足,可以参考hibernate tools 查询其他更详细的配置
-->
maven配置文件:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xuan</groupId>
<artifactId>mavenExample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mavenExample</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<dependencies>
<!-- mysql链接驱动,如果是Oracle的就换成Oracle的驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!-- oracle 链接驱动 -->
<!--
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.1.0.6.0</version>
</dependency>
-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>3.3.2.Beta1</version>
</dependency>
</dependencies>
<!-- 运行命令: mvn hibernate3:hbm2java hibernate3:hbm2hbmxml -->
<configuration>
<components>
<!-- 映射文件夹 项目目录下运行命令:mvn hibernate3:hbm2hbmxml -->
<component>
<name>hbm2hbmxml</name>
<implementation>jdbcconfiguration</implementation>
<outputDirectory>src/main/java/</outputDirectory>
</component>
<!-- 实体类 项目目录下运行命令:mvn hibernate3:hbm2java -->
<component>
<name>hbm2java</name>
<outputDirectory>src/main/java/</outputDirectory>
<implementation>jdbcconfiguration</implementation>
</component>
</components>
<componentProperties>
<revengfile>src/main/resources/hibernate.reveng.xml</revengfile> <!-- 生成指定表名的实体类和映射文件(不配置则生成所有的数据表) -->
<configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile><!-- hibernate总配置文件,注意是链接数据库的信息 -->
<packagename>com.xuan.model</packagename> <!-- 输出的包名 -->
<ejb3>false</ejb3> <!-- true为注解实体类方式,只需要生成java类即可,false为映射文件和非注解java实体类,结合 -->
</componentProperties>
<!-- 1、如果你要生成注解方式的 java文件,就运行 mvn hibernate3:hbm2java
2、如果你要生成 hbm.xml 配合 java 的形式就先把 pom 里面 <ejb3>false</ejb3> 弄成false
然后分别运行 mvn hibernate3:hbm2hbmxml 和 mvn hibernate3:hbm2java -->
</configuration>
</plugin>
</plugins>
</build>
</project>
当前项目目录下打开终端窗口运行:
1、注解实体类:(使用较主流)
mvn hibernate3:hbm2java
2、映射文件与实体类结合 (传统的方式)
mvn hibernate3:hbm2java hibernate3:hbm2hbmxml