一.核心配置文件的两种配置方式
1.属性文件方式 hibernate.properties(基本不会选用
hibernate.connection.driver_class=com.mysql.jdbc.Driver
- 缺点:
不能加载映射的配置文件,需要手动编写代码去加载
2..XML文件的形式,开发基本都会选择这种方式
<property name="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
- 优点:格式化清晰,编写提示,可以加载配置文件
二.关于hibernate.cfg.xml的配置文件方式
必须要有的5大配置:
4大数据库连接信息:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
1个数据库方言:
hibernate.dialect
可选配置:
hibernate.show_sql:
true --显示SQL语句
hibernate.format_sql:
true --格式化SQL
hibernate.hbm2ddl.auto
:xx --通过映射转化为DDL语句(操作表)
create: --每次都会创建一个新的表.---测试的时候
create-drop: --每次都会创建一个新的表,当执行结束之后,将创建的这个表删除.---测试的时候
update: --如果有表,使用原来的表.没有表,创建一个新的表.同时更新表结构.
validate: --如果有表,使用原来的表.同时校验映射文件与表中字段是否一致如果不一致就会报错
加载映射:
<mapping resource="/domain/xxx.hbm.xml" />
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 先配置SessionFactory标签,一个数据库对应一个SessionFactory标签 --> <session-factory> <!-- 必须要配置的5大参数,4大参数,数据库方言 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">toor</property> <!-- 数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置 --> <!-- 在Console显示SQL语句 --> <property name="hibernate.show_sql">true</property> <!-- 格式化显示SQL语句 --> <property name="hibernate.format_sql">true</property> <!-- 通过映射转化为DDL语句(对表的操作) --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 映射配置文件,需要引入的映射配置文件 --> <mapping resource="com/hibernateday1/domain/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>