Hibernate实战——映射数据库对象

北城余情 提交于 2019-11-29 21:54:30

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate-configuration是配置文件的根元素 -->
<hibernate-configuration>
     <session-factory>
           <!-- 指定连接数据库所用的驱动 -->
           <property  name="connection.driver_class">com.mysql.jdbc.Driver</property>
           <!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
           <property  name="connection.url">jdbc:mysql://localhost/hibernate</property>
           <!-- 指定连接数据库的用户名 -->
           <property name="connection.username">root</property>
           <!-- 指定连接数据库的密码 -->
           <property name="connection.password">32147</property>
           <!-- 指定连接池里最大连接数 -->
           <property  name="hibernate.c3p0.max_size">20</property>
           <!-- 指定连接池里最小连接数 -->
           <property name="hibernate.c3p0.min_size">1</property>
           <!-- 指定连接池里连接的超时时长 -->
           <property  name="hibernate.c3p0.timeout">5000</property>
           <!-- 指定连接池里最大缓存多少个Statement对象 -->
           <property  name="hibernate.c3p0.max_statements">100</property>
           <property  name="hibernate.c3p0.idle_test_period">3000</property>
           <property  name="hibernate.c3p0.acquire_increment">2</property>
           <property  name="hibernate.c3p0.validate">true</property>
           <!-- 指定数据库方言 -->
           <property  name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
           <!-- 根据需要自动创建数据库 -->
           <property name="hbm2ddl.auto">update</property>
           <!-- 显示Hibernate持久化操作所生成的SQL -->
           <property name="show_sql">true</property>
           <!-- 将SQL脚本进行格式化后再输出 -->
           <property name="hibernate.format_sql">true</property>
           <!-- 罗列所有持久化类的类名 -->
           <mapping  resource="org/crazyit/app/domain/News.hbm.xml"/>
     </session-factory>
</hibernate-configuration>

二 创建数据库对象

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- hibernate-mapping是映射文件的根元素 -->
<hibernate-mapping>
     <!-- 使用data-object元素定义数据库对象 -->
     <database-object>
           <!-- 定义创建数据库对象的语句 -->
           <create>create table test(t_name  varchar(255));</create>
           <!-- 让drop元素为空,不删除任何对象 -->
           <drop></drop>
           <!-- 指定仅对MySQL数据库有效 -->
           <dialect-scope  name="org.hibernate.dialect.MySQLDialect"/>
           <dialect-scope  name="org.hibernate.dialect.MySQL5InnoDBDialect"/>
     </database-object>   
</hibernate-mapping>

三 测试

package lee;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.tool.hbm2ddl.*;
import org.hibernate.service.*;
import org.hibernate.boot.registry.*;

public class NewsManager
{
    public static void main(String[] args) throws Exception
    {
        // 实例化Configuration,configure()方法默认加载hibernate.cfg.xml文件
        Configuration conf = new Configuration().configure();
        // 以Configuration实例创建SessionFactory实例
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(conf.getProperties()).build();
//        conf.buildSessionFactory(serviceRegistry);
        // 创建SchemaExport对象
        SchemaExport se = new SchemaExport(conf);
        // 设置输出格式良好的SQL脚本
        se.setFormat(true)
            // 设置保存SQL脚本的文件名
            .setOutputFile("new.sql")
            // 输出SQL脚本,并执行SQL脚本
            .create(true, true);
    }
}

四 测试

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!