【Hibernate】自动生成数据库表

时间:2022-06-20 21:40:46

  虽说整体上对SSH有一定的把控使用能力,但还是见微知著,点滴积累。Hibernate本意是冬眠,很好的封装了JDBC和数据库交互,实现了对象的持久化操作。所以也可以理解对象的持久化其实就是“冬眠”。那么如何通过Hibernate实现自动生成数据库表?不再依赖于db工具。

一、User类

<span style="font-size:18px;">package com.xx.hibernate;

import java.util.Date;

public class User {
private String id;

private String name;

private String password;

private Date createDate;

private Date expireDate;

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 getCreateDate() {
return createDate;
}

public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

public Date getExpireDate() {
return expireDate;
}

public void setExpireDate(Date expireDate) {
this.expireDate = expireDate;
}

}
</span>


二、User.hbm.xml 映射文件

<span style="font-size:18px;"><?xml version="1.0" encoding='utf-8'?>
<!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.xx.hibernate.User">
<id name="id">
<generator class="uuid"></generator>
</id>
<property name="name" />
<property name="password" />
<property name="createDate" />
<property name="expireDate" />
</class>
</hibernate-mapping></span>


三、hibernate.cfg.xml 配置文件

<span style="font-size:18px;"><?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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=UTF8</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/xx/hibernate/User.hbm.xml" />
</session-factory>
</hibernate-configuration></span>


四、ExportDB 工具类

<span style="font-size:18px;"><pre name="code" class="html">package com.xx.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

/**
* 将hbm转成ddl
*
* @param args
*/
public static void main(String[] args) {
// 如果直接new Configuration 默认读取的是hibernate.properties 文件
// 后边再加.configure()读取的才是hibernate.cfg.xml 文件,
Configuration cfg = new Configuration().configure();
// 创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);
// 生成ddl文件,并在控制台输出
export.create(true, true);
}

}
</span>


  运行工具类就可以生成表,这是一种实现方式;还有第二种方案,在hibernate.cfg.xml中添加: <property name="hibernate.hbm2ddl.auto" value="create" />;