javaee之hibernate的schemaExport

来源:互联网 发布:淘宝网拖鞋批发 编辑:程序博客网 时间:2024/05/09 08:17

通过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;}@Overridepublic 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 {/** * 根据配置生成结构表文件 */@Testpublic 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
原创粉丝点击