在以前的项目中,我们都是先设计数据库,将数据库中的表一一建好,在考虑实现。在Hibernate中,我们不用在手动去建表,而是通过映射来操作数据库。怎样操作数据库呢?Hibernate3中为我们提供了hbm2ddl这样的工具,下面让我们看一个具体事例:
package com.bjpowernode.hibernate;
import java.util.Date;
/**
* 实体类User
* @author lyj
*
*/
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
![](https://img-my.csdn.net/uploads/201212/11/1355214525_4833.png)
package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl工具类
* @author lyj
*
*/
public class ExportDB {
public static void main(String args[]){
//默认读取hibernate.cfg.xml文件
Configuration cfg=new Configuration().configure();
//Create(script,export)方法根据持久类和映射文件先删除架构后创建删除数据库架构。
//有两个参数,第一个为True就是把DDL语句输出到控制台,
//第二个为True就是根据持久类和映射文件先执行删除再执行创建操作
SchemaExport export=new SchemaExport(cfg);
export.create(true, true);
}
}
如何理解 ?默认读取hibernate.cfg.xml文件,在Configuration类里我们可以看到:
![](https://img-my.csdn.net/uploads/201212/11/1355214621_9367.png)
![](https://img-my.csdn.net/uploads/201212/11/1355214650_6871.png)
User.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- "com.bjpowernode.hibernate.User" 这里的User是实体类而不是数据库要对应的表 -->
<class name="com.bjpowernode.hibernate.User">
<!-- 主键 -->
<id name="id">
<!-- 主键生成策略 -->
<generator class="uuid"/>
</id>
<!-- 实体类的属性 -->
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
![](https://img-my.csdn.net/uploads/201212/11/1355214707_5402.png)
Hibernate核心配置文件详细解析提供一个链接:
http://www.cnblogs.com/jqyp/archive/2010/06/28/1766851.html
<!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.connection.driver_class">com.mysql.jdbc.Driver</property>
<properyt name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</properyt>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjpowernode</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 具体映射文件 -->
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate的映射流程大概就是
实体类(.class)--->实体类映射文件(xx.hbm.xml)--->hibernate核心配置文件(hibernate.cfg.xml)
--->数据库导出工具(DBExport)依赖于SchemaExport类