以自動生成User表爲例,首先需要配置hibernate.cfg.xml:
<!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>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml文件裏主要配置數據庫連接信息:mysql驅動,數據庫表信息,用戶名,密碼,Hibernate方
言,以及映射文件User.hbm.xml的路徑。
然後是User實體類:
package com.bjpowernode.hibernate;
import java.util.Date;
/**
* 建立User實體類
* @author why_768
*
*/
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;
}
}
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>
<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>
User.hbm.xml文件裏的主要配置:id標識(主鍵標識),name裏對應的是User實體裏的字段信息,如果想在數
據庫表中進行重命名,可以加上column屬性;其他的字段信息用property。關於數據庫的一對多,一對一以及多對多
的關係也是在這裏配置的,稍後會有具體的博客進行說明。
基本的配置就完成了,然後就可以建立測試類,進行數據庫表的導入。
package com.bjpowernode.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 將hbm生成ddl
* @author why_768
*
*/
public class ExportDB {
public static void main(String[] args){
//hibernate讀取hibernate.hbm.xml文件
Configuration cfg=new Configuration().configure();
SchemaExport export=new SchemaExport(cfg);
export.create(true, true);
}
}
Hibernate默認讀取的映射文件是hibernate.properties,在Configuration()後加“.configure()”,就能讀取
hibernate.hbm.xml映射文件了。關於SchemaExport,它的源碼:
/**
* Create a schema exporter for the given Configuration
* and given settings
*/
public SchemaExport(Configuration cfg, Settings settings) throws HibernateException {
dialect = settings.getDialect();
connectionHelper = new SuppliedConnectionProviderConnectionHelper(
settings.getConnectionProvider()
);
dropSQL = cfg.generateDropSchemaScript( dialect );
createSQL = cfg.generateSchemaCreationScript( dialect );
format = settings.isFormatSqlEnabled();
}
官方解釋:Commandline tool to export table schema to the database. This class may also be called from
inside an application. 具體的鏈接:http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/tool/hbm2ddl
控制檯輸出結果:
drop table if exists User
create table User (id varchar(255) not null, name varchar(255), password varchar(255), createTime datetime, expireTime datetime, primary key (id))
當然,數據庫中的表也創建好了!