【Hibernate】Hibernate.cfg.xml 主配置

Hibernate.cfg.xml 主配置

1、Hibernate.cfg.xml

主配置文件中主要配置:数据库连接信息、其他参数、映射信息!

 

2、常用配置查看源码:

hibernate-distribution-3.6.0.Final\project\etc\hibernate.properties

 

3、数据库连接参数配置

例如:

## MySQL

#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

 

4、自动建表

Hibernate.properties

 

#hibernate.hbm2ddl.auto create-drop 

每次在创建sessionFactory时候执行创建表;当调用sesisonFactory的close方法的时候,删除表!

#hibernate.hbm2ddl.auto create   

每次都重新建表; 如果表已经存在就先删除再创建

#hibernate.hbm2ddl.auto update  

如果表不存在就创建; 表存在就不创建;

#hibernate.hbm2ddl.auto validate  

(生成环境时候) 执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!

 

5、代码自动建表

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);
}
}


 

 

Bernate.cfg.xml配置文件:

<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表  -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="cn/lfsenior/a_hello/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值