【Hibernate】——SchemaExport自动生成数据库表

           以自动生成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

/SchemaExport.html

          

            控制台输出结果:

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))
             当然,数据库中的表也创建好了!

     

         

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值