Hibernate.cfg.xml
主配置文件中主要配置:数据库连接信息、其他参数、映射信息!
常用配置查看源码:
hibernate-distribution-3.6.0.Final\project\etc\hibernate.properties
数据库连接参数配置
例如:
## MySQL
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialectorg.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialectorg.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_classcom.mysql.jdbc.Driver
#hibernate.connection.urljdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
自动建表
Hibernate.properties
#hibernate.hbm2ddl.auto create-drop 每次在创建sessionFactory时候执行创建表;
当调用sesisonFactory的close方法的时候,删除表!
#hibernate.hbm2ddl.auto create 每次都重新建表; 如果表已经存在就先删除再创建
#hibernate.hbm2ddl.auto update 如果表不存在就创建; 表存在就不创建;
#hibernate.hbm2ddl.auto validate (生成环境时候) 执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<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">123456</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">create</property>
<!-- 3.加载所有映射 -->
<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>