一、Hibernate.cfg.xml 主配置(主要包括:数据库连接信息、其他参数、映射信息)
1、常用配置查看源码:
hibernate-distribution-3.6.0.Final\project\etc\hibernate.properties
2、数据库连接参数配置
2.1数据库方言配置
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
2.2自动建表的配置
#hibernate.hbm2ddl.auto create-drop 每次在创建sessionFactory时候执行创建表;
当调用sesisonFactory的close方法的时候,删除表!
#hibernate.hbm2ddl.auto create 每次都重新建表; 如果表已经存在就先删除再创建
#hibernate.hbm2ddl.auto update 如果表不存在就创建; 表存在就不创建;
#hibernate.hbm2ddl.auto validate (生成环境时候) 执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!
代码自动建表:
public class App_ddl { // 自动建表 @Test public void testCreate() throws Exception { // 创建配置管理类对象 Configuration config = new Configuration(); // 加载主配置文件 config.configure(); // 创建工具类对象 SchemaExport export = new SchemaExport(config); // 建表 // 第一个参数: 是否在控制台打印建表语句 // 第二个参数: 是否执行脚本 export.create(true, true); } }
原文:http://www.cnblogs.com/h-g-f-s123/p/6380209.html