- 導包
- 創建數據庫
- 書寫對象與表的映射文件;一般命名爲
xxx.hbm.xml
導入xml約束
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
書寫對象與表映射文件,其中class
標籤的name屬性是實體類的完整類名,table
屬性爲數據庫表名,dynamic-update
屬性是每次更新自動生成的sql語句是否爲表的全部列,dynamic-insert
同理,每個實體類對象映射文件都要有一個ID
標籤來唯一標識記錄是否存在,id
不可省略,
id
標籤與property
標籤中的name
屬性對應實體類中的屬性名,嚴格區分大小寫,column
屬性對應表中的列名,不區分大小寫,type
屬性爲屬性類型;id
標籤下的generator
標籤標識id
的增長類型:其中native
是數據庫主鍵自動增長,increment
是框架主鍵自動增長,assigned
是手動指定id值
<hibernate-mapping >
<class name="cn.entity.Xuesheng" table="xueshengbiao" dynamic-update="true" dynamic-insert="true">
<cache usage="read-write"/>
<id name="xuehao" column="xuehao" type="int">
<generator class="native"></generator>
</id>
<property name="xingming" column="xingming" type="java.lang.String"></property>
<property name="xingbie" column="xingbie" type="int"></property>
<property name="nianling" column="nianling" type="int"></property>
<set name="chengji" cascade="save-update">
<key column="xuehao"></key>
<one-to-many class="cn.entity.Chengji"/>
</set>
<set name="kecheng" inverse="true" table="chengjibiao" lazy="false" fetch="subselect">
<key column="xuehao"></key>
<many-to-many class="cn.entity.Kecheng" column="kechenghao">
</many-to-many>
</set>
</class>
</hibernate-mapping>
- 書寫主配置文件
主配置文件放在src
下,名爲hibernate.cfg.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>
<session-factory>
<!-- 數據庫設置-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/xuesheng?useUnicode=true&characterEncoding=UTF8&useServerPrepStmts=true&prepStmtCacheSqlLimit=256&cachePrepStmts=true&prepStmtCacheSize=256&rewriteBatchedStatements=true</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- 數據庫類型 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 啓用Hibernate的自動會話上下文管理-->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- 禁用二級緩存 -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->
<!-- 查看自動生成的sql語句 -->
<property name="show_sql">true</property>
<!-- 格式化自動生成的sql語句 -->
<property name="format_sql">true</property>
<!-- 二級緩存配置 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 查詢緩存配置 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- 自動建表 -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<!-- 注入實體類與與表的映射文件-->
<mapping resource="cn/entity/xuesheng.hbm.xml" />
<mapping resource="cn/entity/chengji.hbm.xml" />
<mapping resource="cn/entity/kecheng.hbm.xml" />
</session-factory>
</hibernate-configuration>