1. 程式人生 > >spring整合hibernate中的配置檔案hibernate.cfg.xml的詳解總結

spring整合hibernate中的配置檔案hibernate.cfg.xml的詳解總結

applicationContext.xml配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- 建立一個用於連線的基礎bean  BasicDataSource  用於Spring自動裝載-->
	<bean id="dataSource"
		class="org.apache.commons.dbcp2.BasicDataSource">
		<!-- 配置資料庫的驅動程式,Hibernate在連線資料庫時,需要用到資料庫的驅動程式 -->
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<!-- 設定資料庫的連線url:jdbc:mysql://127.0.0.1:3306/hibernate,其中127.0.0.1表示mysql伺服器名稱,此處為本機,    hibernate是資料庫名 -->
		<property name="url"
			value="jdbc:mysql://127.0.0.1:3306/hibernate">
		</property>
		<!-- 連線資料庫的使用者名稱 -->
		<property name="username" value="root"></property>
		<!-- 連線資料庫的密碼 -->
		<property name="password" value="root"></property>
	</bean>
	
	<!-- 建立sessionFactory的 bean,用於Spring自動裝載 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 屬性dataSource ,引用上面的dataSource Bean 使用者獲取與資料庫的連線資訊 -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<!-- 指定hibernate核心配置檔案的路徑 -->
		<property name="configLocation">
		<value>classpath:hibernate.cfg.xml</value>
		</property>
<!-- 用於配置hibernate裡的一些功能屬性 --> <property name="hibernateProperties"> <props> <!-- 以標準sql格式列印輸出sql語句 --> <prop key="hibernate.format_sql">true</prop> <!-- 設定是否列印sql語句,建議開發期間開啟該功能,便於除錯程式 --> <prop key="hibernate.show_sql">true</prop> <!-- 設定資料庫的方言 --> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <!-- 主要用於:自動建立|更新|驗證資料庫表結構 create: 每次載入hibernate時都會刪除上一次的生成的表,然後根據你的model類再重新來生成新表, 哪怕兩次沒有任何改變也要這樣執行,這就是導致資料庫表資料丟失的一個重要原因。 create-drop : 每次載入hibernate時根據model類生成表,但是sessionFactory一關閉,表就自動刪除。 update: 最常用的屬性,第一次載入hibernate時根據model類會自動建立起表的結構(前提是先建立好資料庫), 以後載入hibernate時根據 model類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。 要注意的是當部署到伺服器後,表結構是不會被馬上建立起來的,是要等 應用第一次執行起來後才會。 validate : 每次載入hibernate時,驗證建立資料庫表結構,只會和資料庫中的表進行比較,不會建立新表,但是會插入新值。 --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> </beans>

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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 	<property name="hbm2ddl.auto">update</property>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="connection.url">
		jdbc:mysql://127.0.0.1:3306/hibernate
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">root</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="myeclipse.connection.profile">
		com.mysql.jdbc.Driver
	</property>
	<property name="format_sql">true</property>
	<property name="show_sql">true</property>
	<property name="javax.persistence.validation.mode">none</property> 
	<mapping resource="com/imooc/config/Address.hbm.xml" />
	<mapping resource="com/imooc/config/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>