1.Spring and Hibernate integration
Hibernate and Bean.xml are required to be configured
1) Key points: sessionfactory create transaction to spring for springioc;session transaction processing
2) Jar Package:
Connection Pool / Database Driver Package
H ibernate related jars
Spring Core Pack (5 )
Spring AOP Package (4 )
Spring-orm-3.2.5.release.jar "Spring 's support for hibernate "
Spring-tx-3.2.5.release.jar "Transaction related"
3) Integration steps: A: Create a sessionfactory bean node; B: Add a transaction to the business method of the logical layer
A:sessionfactory three ways to integrate in spring beans:
1. All hibernate.cfg.xml introduced into the Bean.xml
2. Partial Introduction: DataSource connection pool written in spring
3. Write all in Bean.xml
B: Add transactions to the logical layer using AOP Transaction mode
4) Example:
1. Bean.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:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx="Http://www.springframework.org/schema/tx"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx.xsd "><!--DAO Instance--<bean id="Deptdao" class="DAO. Deptdao"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <!--service Instance--<bean id="Deptservice" class="Cn.itcast.service.DeptService"> <property name="Deptdao" ref="Deptdao"></property> </bean> <!--data Source configuration-<bean id="DataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="Driverclass"Value="Com.mysql.jdbc.Driver"></property> <property name="Jdbcurl"Value="Jdbc:mysql:///hib_demo"></property> <property name="User"Value="Root"></property> <property name="Password"Value="Root"></property> <property name="initialpoolsize"Value="3"></property> <property name="maxpoolsize"Value="Ten"></property> <property name="maxstatements"Value=" -"></property> <property name="acquireincrement"Value="2"></property> </bean> <!--########## #Spring与Hibernate整合 start###########--<!--Way One: the way to load hibernate.cfg.xml files directly integrates this way-<!--<bean id="sessionfactory" class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configlocation"Value="Classpath:hibernate.cfg.xml"></property> </bean> <!--two: The connection pool is given to spring to generate a "part of the configuration is written to hibernate, a part is done in spring"- <!--<bean id="sessionfactory" class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configlocation"Value="Classpath:hibernate.cfg.xml"></property> <property name="DataSource" ref="DataSource"></property> </bean> <!--recommended Three: All configurations are written to spring configuration--<bean id="sessionfactory" class="Org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--Connection pool configuration-<property name="DataSource" ref="DataSource"></property> <!--Hibernate Common configuration--<property name="hibernateproperties"> <props> <prop key="Hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="Hibernate.show_sql">true</prop> <prop key="Hibernate.hbm2ddl.auto">update</prop> </props> </property> <!--hibernate mapping configuration--< !--Hibernate mapping Configuration<property name="mappinglocations"> <list> <value>classpath:entity/*.hbm.xml</value> </list> </property>-<property name= "Mappingdi Rectorylocations "> <list> <value>classpath:entity/</value> </ List> </property> </bean> <!--########## #Spring与Hibernate整合 end###### #####-<!--transaction Configuration-<!--A. Configuring the transaction manager Class--<bean id= "Txmanager" class= "org.springframework.or M.hibernate3.hibernatetransactionmanager "> <property name=" sessionfactory "ref=" SessionFactory "></ Property> </bean> <!--b. Configuring transaction enhancement (after blocking to a method if management transaction?)--<tx:advice id= "Txadvice" transaction-manage R= "Txmanager" > <tx:attributes> <tx:method name= "*" read-only= "false"/> </tx:att Ributes> </tx:advice> <!--c. AOP configuration--<aop:config> <aop:pointcut expression= "ex Ecution (* service.*.* (..)) "ID= "pt"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "pt"/> </aop:config></beans>
View Code
2.SSH
<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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/context http://www.springframework.org/schema/ context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http:/ /www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx.xsd " >
<!--DAO instance--><bean id= "Deptdao" class= "DAO. Deptdao "><property name=" sessionfactory "ref=" Sessionfactory "></property></bean><!-- Service instance--><bean id= "Deptservice" class= "Cn.itcast.service.DeptService" ><property name= "Deptdao" ref= "Deptdao" ></property></bean><!--data Source configuration--><bean id= "DataSource" class= " Com.mchange.v2.c3p0.ComboPooledDataSource "><property name=" Driverclass "value=" Com.mysql.jdbc.Driver "> </property><property name= "Jdbcurl" value= "Jdbc:mysql:///hib_demo" ></property><property name = "User" value= "root" ></property><property name= "password" value= "root" ></property>< Property Name= "Initialpoolsize" value= "3" ></property><property name= "maxpoolsize" value= "></" Property><property name= "maxstatements" value= "></property><property name=" acquireIncrement "Value=" 2 "></property></bean><!--########## #SprinG and Hibernate integration start###########--><!--way one: Directly load hibernate.cfg.xml files in a way that integrates this way--><!--< Bean id= "sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" ><property name= "Configlocation" value= "Classpath:hibernate.cfg.xml" ></property></bean>--><!-- Mode two: The connection pool is given to spring to generate a "part of the configuration written to hibernate, a part in Spring complete"--><!--<bean id= "sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "><property name=" configlocation "value=" Classpath:hibernate.cfg.xml "></property><property name=" DataSource "ref=" DataSource "></ Property></bean>--><!--Recommended method Three: All configurations are written to the spring configuration--><bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "><!--Connection Pool configuration--><property name=" DataSource " ref= "DataSource" ></property><!--Hibernate common configuration--><property name= "Hibernateproperties" >< Props><prop key= "HibErnate.dialect ">org.hibernate.dialect.mysqldialect</prop><prop key=" Hibernate.show_sql ">true< /prop><prop key= "Hibernate.hbm2ddl.auto" >update</prop></props></property><!-- Hibernate mapping configuration--><!--Hibernate mapping configuration <property name= "Mappinglocations" ><list><value> Classpath:entity/*.hbm.xml</value></list></property>--><property name= " Mappingdirectorylocations "><list><value>classpath:entity/</value></list></ property></bean><!--########## #Spring与Hibernate整合 end###########--><!--Transaction Configuration--><!- -A. Configuring the Transaction manager Class--><bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" ><property name= "Sessionfactory" ref= "Sessionfactory" ></property></bean><!--b. Configuring transaction enhancements ( Intercept to method if management transaction?) --><tx:advice id= "Txadvice" transaction-manager= "Txmanager" ><tx:attributes><tx:method name= "*" ReAd-only= "false"/></tx:attributes></tx:advice><!--C. AOP Configuration--><aop:config> <AOP: Pointcut expression= "Execution (* service.*.* (..))" Id= "pt"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= " PT "/></aop:config></beans>
Java in-depth exploration 12-framework Integration