【Hibernate】自动生成数据库表

本文介绍如何使用Hibernate框架自动生成数据库表,包括定义User类、配置User.hbm.xml映射文件、设置hibernate.cfg.xml及使用ExportDB工具类。

  虽说整体上对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" />;

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值