hibernate.cfg.xml 创建表

来源:互联网 发布:网络直播运行模式 编辑:程序博客网 时间:2024/06/04 18:53

测试类

public static void main(String[] args) {

Configuration conf=new Configuration();
conf.configure("/hibernate.cfg.xml");
SchemaExport dbExport=new SchemaExport(conf);
dbExport.create(true, true);

}

hibernate.cfg.xml 配置

<?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> 
<!--url信息--> 
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<!--数据库方言信息--> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<!--用户名-->
<property name="connection.username">root</property>
<!--密码--> 
<property name="connection.password">yx</property> 
<!--数据库驱动信息--> 
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--<property name="hibernate.hbm2ddl.auto">update</property>-->
<!-- 
* validate 加载hibernate时,验证创建数据库表结构
    * create 每次加载hibernate,重新创建数据库表结构。
* create-drop 加载hibernate时创建,退出是删除表结构
* update 加载hibernate自动更新数据库结构 
 -->
<!--指定Hibernate映射文件路径--> 
<mapping resource="hbm/user/User1.hbm.xml" /> 
<mapping resource="hbm/user/User2.hbm.xml" /> 
</session-factory> 
</hibernate-configuration> 

User.hbm.xml 示例

<?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 package="com.bubulah.dao">
    <class entity-name="TCustomer" table="T_Customer" schema="test">
                <id name="id"
                        type="int"
                        column="ID">
                        <generator class="increment"/>
                </id>
                <property name="name"
                        column="NAME"
                        type="string"
                        length="20"/>
                <property name="address"
                        column="ADDRESS"
                        type="string"
                        length="20"/>
    </class>
</hibernate-mapping>

1 0