配置文件代碼模板
<?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>
</hibernate-configuration>
例子
<?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>
<session-factory>
<!-- property 元素用于配置Hibernate中的屬性
鍵:值
-->
<!-- hibernate.connection.driver_class : 連接數(shù)據(jù)庫的驅(qū)動(dòng) -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- hibernate.connection.username : 連接數(shù)據(jù)庫的用戶名 -->
<property name="hibernate.connection.username">root</property>
<!-- hibernate.connection.password : 連接數(shù)據(jù)庫的密碼 -->
<property name="hibernate.connection.password">123</property>
<!-- hibernate.connection.url : 連接數(shù)據(jù)庫的地址,路徑 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
<!-------------------------------------------------------------------------------->
<!-- show_sql: 操作數(shù)據(jù)庫時(shí),會(huì) 向控制臺(tái)打印sql語句 -->
<property name="show_sql">true</property>
<!-------------------------------------------------------------------------------->
<!-- format_sql: 打印sql語句前,會(huì)將sql語句先格式化 -->
<property name="format_sql">true</property>
<!-------------------------------------------------------------------------------->
<!-- hbm2ddl.auto: 生成表結(jié)構(gòu)的策略配置
update(最常用的取值): 如果當(dāng)前數(shù)據(jù)庫中不存在表結(jié)構(gòu),那么自動(dòng)創(chuàng)建表結(jié)構(gòu).
如果存在表結(jié)構(gòu),并且表結(jié)構(gòu)與實(shí)體一致,那么不做修改
如果存在表結(jié)構(gòu),并且表結(jié)構(gòu)與實(shí)體不一致,那么會(huì)修改表結(jié)構(gòu).會(huì)保留原有列.
create(很少):無論是否存在表結(jié)構(gòu).每次啟動(dòng)Hibernate都會(huì)重新創(chuàng)建表結(jié)構(gòu).(數(shù)據(jù)會(huì)丟失)
create-drop(極少): 無論是否存在表結(jié)構(gòu).每次啟動(dòng)Hibernate都會(huì)重新創(chuàng)建表結(jié)構(gòu).每次Hibernate運(yùn)行結(jié)束時(shí),刪除表結(jié)構(gòu).
validate(很少):不會(huì)自動(dòng)創(chuàng)建表結(jié)構(gòu).也不會(huì)自動(dòng)維護(hù)表結(jié)構(gòu).Hibernate只校驗(yàn)表結(jié)構(gòu). 如果表結(jié)構(gòu)不一致將會(huì)拋出異常.
-->
<property name="hbm2ddl.auto">update</property>
<!-------------------------------------------------------------------------------->
<!-- 數(shù)據(jù)庫方言配置
org.hibernate.dialect.MySQLDialect (選擇最短的)
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-------------------------------------------------------------------------------->
<!-- hibernate.connection.autocommit: 事務(wù)自動(dòng)提交 -->
<property name="hibernate.connection.autocommit">true</property>
<!-------------------------------------------------------------------------------->
<!-- 將Session與線程綁定=> 只有配置了該配置,才能使用getCurrentSession -->
<property name="hibernate.current_session_context_class">thread</property>
<!-------------------------------------------------------------------------------->
<!-- 引入ORM 映射文件
填寫src之后的路徑
-->
<mapping resource="com/itheima/a_hello/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>