詳解Spring+Hiernate整合
一、整合目標(biāo)
1.由IoC容器管理Hibernate的SessionFactory
2.讓Hibernate使用Spring的聲明式事務(wù)
二、整合步驟
先加入Hibernat,再加入Spring,再進行整合。
第一步:
配置Hibernate
1.加入Hibernate相關(guān)的包
Hibernate的必需包
c3p0包和數(shù)據(jù)庫驅(qū)動包
AspectJWeaver.jar
數(shù)據(jù)庫驅(qū)動包
2.添加Hibernate的配置文件hibernate.cfg.xml
a.Hibernate的數(shù)據(jù)源配置可以拿到Spring中去配置,所以無需在hibernate.cfg.xml中配置。
b.關(guān)聯(lián)的.hbm.xml文件也可以在Spring配置文件中配置SessionFactory時進行配置。
c.在hibernate.cfg.xml中可以配置sql方言,sql顯示,自動生成表,二級緩存等內(nèi)容
3.編寫實體類和對應(yīng)的hbm.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> <!-- 數(shù)據(jù)庫連接用Spring配置 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mydb</property> <property name="hibernate.connection.username">root</property> --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <!-- 類映射也可用Spring來配置 <mapping resource="com/itnba/maya/entities/Family.hbm.xml"/> <mapping resource="com/itnba/maya/entities/Info.hbm.xml"/> <mapping resource="com/itnba/maya/entities/Nation.hbm.xml"/> <mapping resource="com/itnba/maya/entities/Title.hbm.xml"/> <mapping resource="com/itnba/maya/entities/Work.hbm.xml"/> --> </session-factory> </hibernate-configuration>
第二步:加入Spring
1.加入Spring包。
Spring的jar包
aspectjweaver.jar
2.加入Spring的配置文件。
配置數(shù)據(jù)源
1)建立db.properties的資源文件,配置數(shù)據(jù)源的連接信息。
driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/mydb user=root password= minPoolSize=5 maxPoolSize=20 initialPoolSize=5
在Spring配置文件中導(dǎo)入db.properties <context:property-placehoder/>
配置實體化c3p0的數(shù)據(jù)源ComboPooledDataSource
(測試數(shù)據(jù)源配置成功)
<!--加載資源對象 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 實例化c3p0數(shù)據(jù)源 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="minPoolSize" value="${minPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property> </bean>
2)配置Hibernate的SessionFactory——通過Spring提供的LocalSessionFactoryBean來配置
<!-- 配置Hibernate的SessionFactory --> <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="factory"> <!--配置數(shù)據(jù)源屬性--> <property name="dataSource" ref="dataSource"></property> <!--配置Hibernate配置文件的位置--> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!--配置Hibernate映射文件的位置,可以使用通配符--> <property name="mappingLocations" value="com/itnba/maya/entities/*.hbm.xml"></property> </bean>
3)配置Spring的聲明式事務(wù)
配置事務(wù)管理器 -- HibernateTransactionManager
<!-- 配置spring的事務(wù)管理器 --> <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager"><!-- 要根據(jù)hibernate的版本配置 --> <property name="sessionFactory" ref="factory"></property> </bean>
配置事務(wù)屬性 -- 導(dǎo)入tx命名空間
<!-- 配置事務(wù)屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice>
配置事務(wù)切點,并把切點和事務(wù)屬性關(guān)聯(lián)起來。--導(dǎo)入aop命名空間
<!-- 配置事務(wù)切入點 --> <aop:config> <aop:pointcut expression="execution(* com.itnba.maya.entities.*.*(..))" id="pointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config>
第三步:編寫代碼
1.在Spring配置文件中配置自動掃描的包
<!-- 自動掃描 --> <context:component-scan base-package="com.itnba.maya.entities"></context:component-scan>
package com.itnba.maya.entities; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository//自動掃描 public class InfoDao { @Autowired//自動掃描 private SessionFactory factory; public Session getSession(){ return factory.getCurrentSession(); } public void select() { Info data = getSession().get(Info.class, "p005"); System.out.println(data.getName()); } }
用 main函數(shù)執(zhí)行
package com.itnba.maya.entities; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) throws SQLException { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); InfoDao data=(InfoDao) context.getBean(InfoDao.class); data.select(); } }
結(jié)果:
完整的Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd" > <!-- 自動掃描 --> <context:component-scan base-package="com.itnba.maya.entities"></context:component-scan> <!--加載資源對象 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 實例化c3p0對象 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="minPoolSize" value="${minPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property> </bean> <!-- 配置Hibernate的SessionFactory --> <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="factory"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="com/itnba/maya/entities/*.hbm.xml"></property> </bean> <!-- 配置spring的聲明性事務(wù) --> <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager"><!-- 要根據(jù)hibernate的版本配置 --> <property name="sessionFactory" ref="factory"></property> </bean> <!-- 配置事務(wù)屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事務(wù)切入點 --> <aop:config> <aop:pointcut expression="execution(* com.itnba.maya.entities.*.*(..))" id="pointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> </beans>
另外:
Spring整合Hibernate,也可以不使用 Hibernate的配置文件,把Hibernate配置文件中的內(nèi)容放在Spring的配置文件中。(一般不這么用)
<property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> .... </props> </property>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SSH整合中 hibernate托管給Spring得到SessionFactory
- 從最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate
- 基于spring+hibernate+JQuery開發(fā)之電子相冊(附源碼下載)
- Hibernate+Spring+Struts擴展Struts
- Maven 搭建SpringMVC+Hibernate項目詳解
- spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開發(fā)環(huán)境搭建圖文教程
- Spring 整合 Hibernate 時啟用二級緩存實例詳解
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- 淺談SpringMVC+Spring3+Hibernate4開發(fā)環(huán)境搭建
- Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解
相關(guān)文章
淺談collection標(biāo)簽的oftype屬性能否為java.util.Map
這篇文章主要介紹了collection標(biāo)簽的oftype屬性能否為java.util.Map,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Jenkins系統(tǒng)如何進行數(shù)據(jù)備份
隨著我們的長期使用,Jenkins系統(tǒng)中的內(nèi)容會越來越多,特別是一些配置相關(guān)的東西,不能有任何丟失。這個時候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進行數(shù)據(jù)備份2021-06-06Java 中Json中既有對象又有數(shù)組的參數(shù)如何轉(zhuǎn)化成對象(推薦)
Gson庫是一個功能強大、易于使用的Java序列化/反序列化庫,它提供了豐富的API來支持Java對象和JSON之間的轉(zhuǎn)換,這篇文章主要介紹了Java 中Json中既有對象又有數(shù)組的參數(shù)如何轉(zhuǎn)化成對象,需要的朋友可以參考下2024-07-07使用Java和PostgreSQL存儲向量數(shù)據(jù)的實現(xiàn)指南
在當(dāng)今的數(shù)字化時代,數(shù)據(jù)存儲的方式和技術(shù)正變得越來越復(fù)雜和多樣化,隨著機器學(xué)習(xí)和數(shù)據(jù)科學(xué)的發(fā)展,向量數(shù)據(jù)的存儲和管理變得尤為重要,本文將詳細介紹如何使用 Java 和 PostgreSQL 數(shù)據(jù)庫來存儲向量數(shù)據(jù),需要的朋友可以參考下2024-09-09Java實現(xiàn)多線程斷點下載實例代碼(下載過程中可以暫停)
線程可以理解為下載的通道,一個線程就是一個文件的下載通道,多線程也就是同時開啟好幾個下載通道,Java實現(xiàn)多線程斷點下載實例代碼(下載過程中可以暫停),有興趣的可以了解一下。2016-12-12JUC循環(huán)屏障CyclicBarrier與CountDownLatch區(qū)別詳解
這篇文章主要為大家介紹了JUC循環(huán)屏障CyclicBarrier與CountDownLatch區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12SpringBoot實現(xiàn)支付寶沙箱支付的完整步驟
沙箱支付是一種用于模擬真實支付環(huán)境的測試工具,它提供了一個安全的測試環(huán)境,供開發(fā)者在不影響真實交易的情況下進行支付功能的開發(fā)和測試,這篇文章給大家介紹了SpringBoot實現(xiàn)支付寶沙箱支付的完整步驟,需要的朋友可以參考下2024-04-04關(guān)于mybatis傳入?yún)?shù)一直為null的問題
這篇文章主要介紹了關(guān)于mybatis傳入?yún)?shù)一直為null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07