javaee之hibernate的schemaExport

通过hibernate的学习可以很方便的创建数据库的表和一些其他信息

关于SchemaExport的用法,可以根据配置文件来生成表结构。


一、需要有一个hibernate.cfg.xml的主配置文件


<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	
<hibernate-configuration>
<session-factory>
<span style="white-space:pre">	</span>//数据库的方言
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>//数据库的一些配置,包括连接的驱动,用户的密码和名称
	<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="hibernate.connection.url">
		jdbc:mysql://localhost:3306/hibernate_example_01
	</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">123</property>

<span style="white-space:pre">	</span>//hibernate的其他配置,是否显示sql语句和格式化sql语句
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>
	
<span style="white-space:pre">	<!</span><span style="font-family: Arial, Helvetica, sans-serif;">--</span><span style="white-space:pre">
<span style="white-space:pre">	</span>create:先删除,再创建
<span style="white-space:pre">	</span>update:如果表不存在就创建,不一样就更新,一样就什么都不做。
<span style="white-space:pre">	</span>create-drop:初始化时创建表,SessionFactory执行close()时删除表。
<span style="white-space:pre">	</span>validate:验证表结构是否一致,如果不一致,就抛异常。
<span style="white-space:pre">	</span>--></span>
	<property name="hbm2ddl.auto">update</property>
	 
<span style="white-space:pre">	</span>//导入实体类的映射文件
<span style="white-space:pre">	</span><mapping resource="hibernate_a_helloword/User.hbm.xml" />
 	
</session-factory>
</hibernate-configuration>

二、需要一个实体类User和User的映射文件

package hibernate_a_helloword;

public class User {

	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
	
}
<?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 package="hibernate_a_helloword">
        	<class name="User" table="t_user">
        		<id name="id" column="id" type="int">
        			<generator class="native"></generator>
        		</id>
        		
        		<property name="name" column="name" length="20" not-null="true" type="string" />
        	</class>
        </hibernate-mapping>

三、最后是一个实现的方法:

只是需要读取配置文件后调用一个SchemaExport方法即可

package hibernate_a_helloword;

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

public class CreateSchema {

	/**
	 * 根据配置生成结构表文件
	 */
	
	@Test
	public void test(){
		Configuration cfg = new Configuration().configure();
		SchemaExport ex = new SchemaExport(cfg);
		ex.create(true, true);<span style="white-space:pre">	</span>//第一个参数表示是否显示语句到控制台,第二个参数表示是否导入脚本文件到数据库
	}
}

通过SchemaExport的方法可以很方便的创建一个数据库表的结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值