(9)Spring和Hibernate整合-創(chuàng)新互聯(lián)

Spring與Hibernate整合的關(guān)鍵點(diǎn):

成都創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元江城做網(wǎng)站,已為上家服務(wù),為江城各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

1) Hibernate的SessionFactory對(duì)象交給Spring創(chuàng)建;

2) Hibernate事務(wù)交給Spring的聲明式事務(wù)管理。

Spring和Hibernate整合的步驟:

1)引入jar包

2)配置:hibernate.cfg.xml、*.hbm.xml、applicationContext.xml

3)搭建環(huán)境、單獨(dú)測(cè)試

1、引入jar包

hibernate3相關(guān)jar包

hibernate3.jar

antlr-2.7.6.jar (required)

commons-collections-3.1.jar (required)

dom4j-1.6.1.jar (required)

javassist-3.12.0.GA.jar (required)

jta-1.1.jar (required)

slf4j-api-1.6.1.jar (required)

hibernate-jpa-2.0-api-1.0.0.Final.jar (jpa)

spring-core相關(guān)jar包

commons-logging-1.2.jar

spring-beans-3.2.5.RELEASE.jar

spring-context-3.2.5.RELEASE.jar

spring-core-3.2.5.RELEASE.jar

spring-expression-3.2.5.RELEASE.jar

spring-aop相關(guān)jar包

aopalliance-.jar

aspectjrt.jar

aspectjweaver.jar

spring-aop-3.2.5.RELEASE.jar

spring-jdbc相關(guān)jar包

spring-jdbc-3.2.5.RELEASE.jar

spring-tx-3.2.5.RELEASE.jar  【事務(wù)相關(guān)】

spring-orm相關(guān)jar包

spring-orm-3.2.5.RELEASE.jar  【spring對(duì)hibernate的支持】

c3p0相關(guān)jar包

c3p0-0.9.1.2.jar

mysql相關(guān)jar包

mysql-connector-java-5.1.38-bin.jar

2、測(cè)試Hibernate

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>     <!-- 通常,一個(gè)session-factory節(jié)點(diǎn)代表一個(gè)數(shù)據(jù)庫(kù) -->     <session-factory>         <!-- 1. 數(shù)據(jù)庫(kù)連接配置 -->         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>         <property name="hibernate.connection.url">jdbc:mysql:///test</property>         <property name="hibernate.connection.username">root</property>         <property name="hibernate.connection.password">root</property> <!--  數(shù)據(jù)庫(kù)方言配置, hibernate在運(yùn)行的時(shí)候,會(huì)根據(jù)不同的方言生成符合當(dāng)前數(shù)據(jù)庫(kù)語(yǔ)法的sql  -->         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>                  <!-- 2. 其他相關(guān)配置 --> <!-- 2.1 顯示hibernate在運(yùn)行時(shí)候執(zhí)行的sql語(yǔ)句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">false</property> <!-- 2.3 自動(dòng)建表  --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置session的創(chuàng)建方式:線程方式創(chuàng)建session對(duì)象 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 3. 加載所有映射--> <mapping resource="com/rk/entity/Dept.hbm.xml"/>     </session-factory> </hibernate-configuration>

Dept.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.entity" auto-import="true"> <class name="Dept" table="T_Department"> <id name="deptId" column="id"> <generator class="native"></generator> </id> <version name="deptVersion" column="dept_version"></version> <property name="deptName" column="name"></property> </class> </hibernate-mapping>

Dept.java

package com.rk.entity; public class Dept { private int deptId; private String deptName; private int deptVersion; public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public int getDeptVersion() { return deptVersion; } public void setDeptVersion(int deptVersion) { this.deptVersion = deptVersion; } @Override public String toString() { return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptVersion=" + deptVersion + "]"; } }

DeptDao.java

package com.rk.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.rk.entity.Dept; public class DeptDao { private static SessionFactory sf; static { sf = new Configuration().configure().buildSessionFactory(); } public Dept findById(int id) { Session session = sf.getCurrentSession(); session.beginTransaction(); Dept dept = (Dept) session.get(Dept.class, id); session.getTransaction().commit(); return dept; } public void save(Dept dept) { Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(dept); session.getTransaction().commit(); } }

DeptService.java

package com.rk.service; import com.rk.dao.DeptDao; import com.rk.entity.Dept; public class DeptService { private DeptDao deptDao = new DeptDao(); public Dept findById(int id) { return deptDao.findById(id); } public void save(Dept dept) { deptDao.save(dept); } }

App.java

package com.rk.test; import org.junit.Test; import com.rk.entity.Dept; import com.rk.service.DeptService; public class App { @Test public void test() { DeptService deptService = new DeptService(); // Dept dept = deptService.findById(3); // System.out.println(dept); Dept dept = new Dept(); dept.setDeptName("HelloWorld"); deptService.save(dept); } }

 

3、測(cè)試Spring環(huán)境

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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dept" class="com.rk.entity.Dept"></bean> </beans>

App.java (修改)

@Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Dept dept = (Dept) ac.getBean("dept"); System.out.println(dept); }

 

4、Spring和Hibernate整合

4.1、Hibernate的SessionFactory交給Spring創(chuàng)建

其中,hibernate.cfg.xml、Dept.hbm.xmlDept.java三個(gè)文件沒(méi)有發(fā)生變化。

DeptDao.java (修改)

package com.rk.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.rk.entity.Dept; public class DeptDao { private SessionFactory sf; public void setSf(SessionFactory sf) { this.sf = sf; } public Dept findById(int id) { Session session = sf.getCurrentSession(); session.beginTransaction(); Dept dept = (Dept) session.get(Dept.class, id); session.getTransaction().commit(); return dept; } public void save(Dept dept) { Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(dept); session.getTransaction().commit(); } }

DeptService.java (修改)

package com.rk.service; import com.rk.dao.DeptDao; import com.rk.entity.Dept; public class DeptService { private DeptDao deptDao; public void setDeptDao(DeptDao deptDao) { this.deptDao = deptDao; } public Dept findById(int id) { return deptDao.findById(id); } public void save(Dept dept) { deptDao.save(dept); } }

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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- entity 實(shí)例 --> <bean id="dept" class="com.rk.entity.Dept" scope="prototype"></bean> <!-- dao 實(shí)例 --> <bean id="deptDao" class="com.rk.dao.DeptDao"> <property name="sf" ref="sessionFactory"></property> </bean> <!-- service 實(shí)例 --> <bean id="deptService" class="com.rk.service.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean> <!-- SessionFactory 的實(shí)例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> </beans>

App.java (修改)

package com.rk.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.rk.entity.Dept; import com.rk.service.DeptService; public class App { @Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); DeptService deptService = (DeptService) ac.getBean("deptService"); Dept dept = deptService.findById(3); System.out.println(dept); Dept newDept = (Dept) ac.getBean("dept"); newDept.setDeptName("HelloWorld"); deptService.save(newDept); } }

4.2、Hibernate的事務(wù)交給Spring的事務(wù)控制

其中,Dept.hbm.xmlDept.java沒(méi)有發(fā)生改變。

DeptDao.java (修改)

package com.rk.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.rk.entity.Dept; public class DeptDao { private SessionFactory sf; public void setSf(SessionFactory sf) { this.sf = sf; } public Dept findById(int id) { Session session = sf.getCurrentSession(); Dept dept = (Dept) session.get(Dept.class, id); return dept; } public void save(Dept dept) { Session session = sf.getCurrentSession(); session.save(dept); } }

DeptService.java (修改)

在save方法中,加入了int i = 1/0;所以會(huì)出錯(cuò),而save方法處于事務(wù)當(dāng)中,因此兩條數(shù)據(jù)不會(huì)保存成功。如果去掉錯(cuò)誤,兩條數(shù)據(jù)就可以保存成功了。

package com.rk.service; import com.rk.dao.DeptDao; import com.rk.entity.Dept; public class DeptService { private DeptDao deptDao; public void setDeptDao(DeptDao deptDao) { this.deptDao = deptDao; } public Dept findById(int id) { return deptDao.findById(id); } public void save(Dept dept) { deptDao.save(dept); int i = 1/0; Dept newDept = new Dept(); newDept.setDeptName("HAHA_LLO"); deptDao.save(newDept); } }

hibernate.cfg.xml (修改)

 修改的只有一行:刪除或注釋下面的配置

<!-- 配置session的創(chuàng)建方式:線程方式創(chuàng)建session對(duì)象 --> <property name="hibernate.current_session_context_class">thread</property>

否則,會(huì)報(bào)錯(cuò)誤:org.hibernate.HibernateException: 方法 is not valid without active transaction。

<?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>     <!-- 通常,一個(gè)session-factory節(jié)點(diǎn)代表一個(gè)數(shù)據(jù)庫(kù) -->     <session-factory>         <!-- 1. 數(shù)據(jù)庫(kù)連接配置 -->         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>         <property name="hibernate.connection.url">jdbc:mysql:///test</property>         <property name="hibernate.connection.username">root</property>         <property name="hibernate.connection.password">root</property> <!--  數(shù)據(jù)庫(kù)方言配置, hibernate在運(yùn)行的時(shí)候,會(huì)根據(jù)不同的方言生成符合當(dāng)前數(shù)據(jù)庫(kù)語(yǔ)法的sql  -->         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>                  <!-- 2. 其他相關(guān)配置 --> <!-- 2.1 顯示hibernate在運(yùn)行時(shí)候執(zhí)行的sql語(yǔ)句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">false</property> <!-- 2.3 自動(dòng)建表  --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置session的創(chuàng)建方式:線程方式創(chuàng)建session對(duì)象 --> <!-- <property name="hibernate.current_session_context_class">thread</property> --> <!-- 3. 加載所有映射--> <mapping resource="com/rk/entity/Dept.hbm.xml"/>     </session-factory> </hibernate-configuration>

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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- entity 實(shí)例 --> <bean id="dept" class="com.rk.entity.Dept" scope="prototype"></bean> <!-- dao 實(shí)例 --> <bean id="deptDao" class="com.rk.dao.DeptDao"> <property name="sf" ref="sessionFactory"></property> </bean> <!-- service 實(shí)例 --> <bean id="deptService" class="com.rk.service.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean> <!-- SessionFactory 的實(shí)例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 1.事務(wù)管理器類 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <constructor-arg name="sessionFactory" ref="sessionFactory"></constructor-arg> </bean> <!-- 2.事務(wù)策略配置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 3.事務(wù)策略應(yīng)用 --> <aop:config> <aop:pointcut expression="execution(* com.rk.service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>

App.java (沒(méi)有變化,進(jìn)行測(cè)試)

package com.rk.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.rk.entity.Dept; import com.rk.service.DeptService; public class App { @Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); DeptService deptService = (DeptService) ac.getBean("deptService"); Dept dept = deptService.findById(3); System.out.println(dept); Dept newDept = (Dept) ac.getBean("dept"); newDept.setDeptName("HelloWorld"); deptService.save(newDept); } }

4.3、Hibernate的數(shù)據(jù)源交給Spring創(chuàng)建

這里只涉及到修改hibernate.cfg.xml和applicationContext.xml

hibernate.cfg.xml 中刪除以下內(nèi)容:

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>         <property name="hibernate.connection.url">jdbc:mysql:///test</property>         <property name="hibernate.connection.username">root</property>         <property name="hibernate.connection.password">root</property>

applicationContext.xml中添加dataSource和修改sessionFactory

<!-- 數(shù)據(jù)源:c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property><!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name="jdbcUrl" value="jdbc:mysql:///test"></property><!-- 數(shù)據(jù)庫(kù)連接URL --> <property name="user" value="root"></property><!-- 用戶名 --> <property name="password" value="root"></property><!-- 密碼 --> <property name="minPoolSize" value="2"></property><!-- Pool最小連接數(shù) --> <property name="maxPoolSize" value="10"></property><!-- Pool大連接數(shù) --> <property name="acquireIncrement" value="2"></property><!-- Pool每次增長(zhǎng)連接數(shù) --> <property name="maxStatements" value="100"></property><!--  --> </bean> <!-- SessionFactory 的實(shí)例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>

完整的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>     <!-- 通常,一個(gè)session-factory節(jié)點(diǎn)代表一個(gè)數(shù)據(jù)庫(kù) -->     <session-factory>         <!-- 1. 數(shù)據(jù)庫(kù)連接配置 --> <!--  數(shù)據(jù)庫(kù)方言配置, hibernate在運(yùn)行的時(shí)候,會(huì)根據(jù)不同的方言生成符合當(dāng)前數(shù)據(jù)庫(kù)語(yǔ)法的sql  -->         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>                  <!-- 2. 其他相關(guān)配置 --> <!-- 2.1 顯示hibernate在運(yùn)行時(shí)候執(zhí)行的sql語(yǔ)句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">false</property> <!-- 2.3 自動(dòng)建表  --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置session的創(chuàng)建方式:線程方式創(chuàng)建session對(duì)象 --> <!-- <property name="hibernate.current_session_context_class">thread</property> --> <!-- 3. 加載所有映射--> <mapping resource="com/rk/entity/Dept.hbm.xml"/>     </session-factory> </hibernate-configuration>

完整的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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- entity 實(shí)例 --> <bean id="dept" class="com.rk.entity.Dept" scope="prototype"></bean> <!-- dao 實(shí)例 --> <bean id="deptDao" class="com.rk.dao.DeptDao"> <property name="sf" ref="sessionFactory"></property> </bean> <!-- service 實(shí)例 --> <bean id="deptService" class="com.rk.service.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean> <!-- 數(shù)據(jù)源:c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property><!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name="jdbcUrl" value="jdbc:mysql:///test"></property><!-- 數(shù)據(jù)庫(kù)連接URL --> <property name="user" value="root"></property><!-- 用戶名 --> <property name="password" value="root"></property><!-- 密碼 --> <property name="minPoolSize" value="2"></property><!-- Pool最小連接數(shù) --> <property name="maxPoolSize" value="10"></property><!-- Pool大連接數(shù) --> <property name="acquireIncrement" value="2"></property><!-- Pool每次增長(zhǎng)連接數(shù) --> <property name="maxStatements" value="100"></property><!--  --> </bean> <!-- SessionFactory 的實(shí)例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 1.事務(wù)管理器類 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <constructor-arg name="sessionFactory" ref="sessionFactory"></constructor-arg> </bean> <!-- 2.事務(wù)策略配置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 3.事務(wù)策略應(yīng)用 --> <aop:config> <aop:pointcut expression="execution(* com.rk.service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>

4.4、Hibernate的配置全部寫到Spring的配置當(dāng)中

換句話說(shuō),就是刪除hibernate.cfg.xml,只保留applicationContext.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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- entity 實(shí)例 --> <bean id="dept" class="com.rk.entity.Dept" scope="prototype"></bean> <!-- dao 實(shí)例 --> <bean id="deptDao" class="com.rk.dao.DeptDao"> <property name="sf" ref="sessionFactory"></property> </bean> <!-- service 實(shí)例 --> <bean id="deptService" class="com.rk.service.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean> <!-- 數(shù)據(jù)源:c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property><!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name="jdbcUrl" value="jdbc:mysql:///test"></property><!-- 數(shù)據(jù)庫(kù)連接URL --> <property name="user" value="root"></property><!-- 用戶名 --> <property name="password" value="root"></property><!-- 密碼 --> <property name="minPoolSize" value="2"></property><!-- Pool最小連接數(shù) --> <property name="maxPoolSize" value="10"></property><!-- Pool大連接數(shù) --> <property name="acquireIncrement" value="2"></property><!-- Pool每次增長(zhǎng)連接數(shù) --> <property name="maxStatements" value="100"></property><!--  --> </bean> <!-- 【推薦】方式: 所有的配置全部都在Spring配置文件中完成 --> <!-- SessionFactory 的實(shí)例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入連接池對(duì)象 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate常用配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- hibernate映射配置 :兩種寫法 --> <!--  <property name="mappingLocations"> <list> <value>classpath:com/rk/entity/*.hbm.xml</value> </list> </property>  --> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/rk/entity/</value> </list> </property> </bean> <!-- 1.事務(wù)管理器類 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <constructor-arg name="sessionFactory" ref="sessionFactory"></constructor-arg> </bean> <!-- 2.事務(wù)策略配置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 3.事務(wù)策略應(yīng)用 --> <aop:config> <aop:pointcut expression="execution(* com.rk.service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>

4.5、總結(jié):最終的文件狀態(tài)

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"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="         http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- entity 實(shí)例 --> <bean id="dept" class="com.rk.entity.Dept" scope="prototype"></bean> <!-- dao 實(shí)例 --> <bean id="deptDao" class="com.rk.dao.DeptDao"> <property name="sf" ref="sessionFactory"></property> </bean> <!-- service 實(shí)例 --> <bean id="deptService" class="com.rk.service.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean> <!-- 數(shù)據(jù)源配置:c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property><!-- 數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name="jdbcUrl" value="jdbc:mysql:///test"></property><!-- 數(shù)據(jù)庫(kù)連接URL --> <property name="user" value="root"></property><!-- 用戶名 --> <property name="password" value="root"></property><!-- 密碼 --> <property name="minPoolSize" value="2"></property><!-- Pool最小連接數(shù) --> <property name="maxPoolSize" value="10"></property><!-- Pool大連接數(shù) --> <property name="acquireIncrement" value="2"></property><!-- Pool每次增長(zhǎng)連接數(shù) --> <property name="maxStatements" value="100"></property><!--  --> </bean> <!-- ###########Spring與Hibernate整合:SessionFactory  start########### --> <!-- 【推薦】方式: 所有的配置全部都在Spring配置文件中完成 --> <!-- SessionFactory 的實(shí)例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入連接池對(duì)象 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate常用配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- hibernate映射配置 :兩種寫法 --> <!--  <property name="mappingLocations"> <list> <value>classpath:com/rk/entity/*.hbm.xml</value> </list> </property>  --> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/rk/entity/</value> </list> </property> </bean> <!-- ###########Spring與Hibernate整合:SessionFactory  end########### --> <!-- ###########Spring與Hibernate整合:事務(wù)  start########### --> <!-- a. 配置事務(wù)管理器類 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <constructor-arg name="sessionFactory" ref="sessionFactory"></constructor-arg> </bean> <!-- b. 配置事務(wù)增強(qiáng)(攔截到方法后如果管理事務(wù)?) --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- c. Aop配置(應(yīng)用事務(wù)) --> <aop:config> <aop:pointcut expression="execution(* com.rk.service.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> <!-- ###########Spring與Hibernate整合:事務(wù)  end########### --> </beans>

Dept.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC  "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.entity" auto-import="true"> <class name="Dept" table="T_Department"> <id name="deptId" column="id"> <generator class="native"></generator> </id> <version name="deptVersion" column="dept_version"></version> <property name="deptName" column="name"></property> </class> </hibernate-mapping>

Dept.java

package com.rk.entity; public class Dept { private int deptId; private String deptName; private int deptVersion; public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public int getDeptVersion() { return deptVersion; } public void setDeptVersion(int deptVersion) { this.deptVersion = deptVersion; } @Override public String toString() { return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptVersion=" + deptVersion + "]"; } }

DeptDao.java

package com.rk.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.rk.entity.Dept; public class DeptDao { private SessionFactory sf; public void setSf(SessionFactory sf) { this.sf = sf; } public Dept findById(int id) { Session session = sf.getCurrentSession(); Dept dept = (Dept) session.get(Dept.class, id); return dept; } public void save(Dept dept) { Session session = sf.getCurrentSession(); session.save(dept); } }

DeptService.java

package com.rk.service; import com.rk.dao.DeptDao; import com.rk.entity.Dept; public class DeptService { private DeptDao deptDao; public void setDeptDao(DeptDao deptDao) { this.deptDao = deptDao; } public Dept findById(int id) { return deptDao.findById(id); } public void save(Dept dept) { deptDao.save(dept); // int i = 1/0; Dept newDept = new Dept(); newDept.setDeptName("HAHA_LLO"); deptDao.save(newDept); } }

App.java

package com.rk.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.rk.entity.Dept; import com.rk.service.DeptService; public class App { @Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); DeptService deptService = (DeptService) ac.getBean("deptService"); Dept dept = deptService.findById(3); System.out.println(dept); Dept newDept = (Dept) ac.getBean("dept"); newDept.setDeptName("HelloWorld"); deptService.save(newDept); } }

 

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

分享名稱:(9)Spring和Hibernate整合-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article26/hccjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、定制開(kāi)發(fā)網(wǎng)站營(yíng)銷、外貿(mào)網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)