Hibernate框架搭建準備工作

  1. 導包
  2. 創建數據庫
  3. 書寫對象與表的映射文件;一般命名爲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>
  1. 書寫主配置文件
    主配置文件放在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&amp;characterEncoding=UTF8&amp;useServerPrepStmts=true&amp;prepStmtCacheSqlLimit=256&amp;cachePrepStmts=true&amp;prepStmtCacheSize=256&amp;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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章