Java代码Util

package com.jyd.bms.tool;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @category 创建数据库
 * @author yemao
 *
 */
public class TestUtil {
	public static void main(String[] args) {
		Configuration conf = null;
		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction tx = null;
		try {
			conf = new Configuration().configure("/config/hibernate.cfg.xml");
			SchemaExport schemaExport = new SchemaExport(conf);
			schemaExport.create(false, true);// 创建数据库
			/*
			 * sessionFactory = conf.buildSessionFactory(); session =
			 * sessionFactory.openSession(); tx = session.beginTransaction();
			 * String sql = ""; Query query = session.createQuery(sql); //
			 * tx.commit(); tx.rollback();
			 */
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
			sessionFactory.close();
		}
	}

}

xml配置文件

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC   "-//Hibernate/Hibernate Configuration DTD//EN"   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<!-- property 元素用于配置Hibernate中的属性
            键:值 
          -->
          <!-- hibernate.connection.driver_class : 连接数据库的驱动  -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>


          <!-- hibernate.connection.username : 连接数据库的用户名 -->
        <property name="hibernate.connection.username">root</property>


          <!-- hibernate.connection.password : 连接数据库的密码 -->
        <property name="hibernate.connection.password">root</property>


          <!-- hibernate.connection.url : 连接数据库的地址,路径 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bms</property>


        <!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
        <property name="show_sql">true</property>


        <!-- format_sql: 打印sql语句前,会将sql语句先格式化  -->
        <property name="format_sql">true</property>


        <!-- hbm2ddl.auto: 生成表结构的策略配置
             update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构. 
                     如果存在表结构,并且表结构与实体一致,那么不做修改
                     如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.
             create(很少):无论是否存在表结构.每次启动Hibernate都会重新创建表结构.(数据会丢失)
             create-drop(极少): 无论是否存在表结构.每次启动Hibernate都会重新创建表结构.每次Hibernate运行结束时,删除表结构.
             validate(很少):不会自动创建表结构.也不会自动维护表结构.Hibernate只校验表结构. 如果表结构不一致将会抛出异常.
          -->
        <property name="hbm2ddl.auto">create</property>




        <!-- 数据库方言配置 
         org.hibernate.dialect.MySQLDialect (选择最短的)
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-- hibernate.connection.autocommit: 事务自动提交  -->
        <property name="hibernate.connection.autocommit">true</property>




        <!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->
        <property name="hibernate.current_session_context_class">thread</property>




        <!-- 引入ORM 映射文件 
            填写src之后的路径
         -->
        <mapping resource="hbm/ContactInfo.hbm.xml"/>
        <mapping resource="hbm/CustomerInfo.hbm.xml"/>
        <mapping resource="hbm/Department.hbm.xml"/>
        <mapping resource="hbm/DutyType.hbm.xml"/>
        <mapping resource="hbm/EducationType.hbm.xml"/>
        <mapping resource="hbm/GpsCostType.hbm.xml"/>
        <mapping resource="hbm/Group.hbm.xml"/>
        <mapping resource="hbm/GroupMenu.hbm.xml"/>
        <mapping resource="hbm/GroupUser.hbm.xml"/>
        <mapping resource="hbm/Language.hbm.xml"/>
        <mapping resource="hbm/Lender.hbm.xml"/>
        <mapping resource="hbm/LoanType.hbm.xml"/>
        <mapping resource="hbm/Menu.hbm.xml"/>
        <mapping resource="hbm/Mortgager.hbm.xml"/>
        <mapping resource="hbm/PositionType.hbm.xml"/>
        <mapping resource="hbm/RepaymentStage.hbm.xml"/>
        <mapping resource="hbm/RepaymentType.hbm.xml"/>
        <mapping resource="hbm/Staff.hbm.xml"/>
        <mapping resource="hbm/UploadFileType.hbm.xml"/>
        <mapping resource="hbm/User.hbm.xml"/>
        <mapping resource="hbm/UserMenu.hbm.xml"/>
        <mapping resource="hbm/VehicleInfo.hbm.xml"/>
        <mapping resource="hbm/VehicleType.hbm.xml"/>
	</session-factory>
</hibernate-configuration>