Reference code Download GITHUB:HTTPS://GITHUB.COM/CHANGWENSIR/JAVA-EE/TREE/MASTER/SPRING4
Spring supports most popular ORM frameworks, including Hibernatejdo, TopLink, Ibatis, and JPA. Spring support for these ORM frameworks is consistent, so you can apply Hibernate integration techniques to other ORM frameworks. Spring 2.0 supports both hibernate2.x and 3.x. But Spring2.5 only supports Hibernate3.1 or later configuration in Spring sessionfactory
• For Hibernate, you must build Sessionfactory from the native Hibernateapi. In addition, the application cannot take advantage of the data storage mechanism provided by spring (for example, the transaction management mechanism of spring) Spring provides the corresponding factory Bean, which can be used as a single instance to create an Sessionfactory instance in the IOC container .
Hibernate.cfg.xml, it's best to keep this file when it's actually developed.
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<! DOCTYPE hibernate-configuration public
"-//hibernate/hibernate configuration dtd//en"
"/HTTP/ Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ">
Application.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xs i: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 > <!--Configure automatic scanning of packages--<context:component-scan base-package= "Spring4_hibernate"/> < !--Import resource Files--<context:property-placeholder location= "Classpath:spring4_hibernaTe/db.properties "/> <!--configuration c3p0 data Source-<bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPoole Ddatasource "> <property name=" User "value=" ${jdbc.user} "/> <property name=" Passwor
D "value=" ${jdbc.password} "/> <property name=" Driverclass "value=" ${jdbc.driverclass} "/> <property name= "Jdbcurl" value= "${jdbc.jdbcurl}"/> <property name= "initialpoolsize" value= "${j Dbc.initpoolsize} "/> <property name=" maxpoolsize "value=" ${jdbc.maxpoolsize} "/> </bean>
; <!--Configure Sessionfactory instances of Hibernate: configured with Spring-provided Localsessionfactorybean-<bean id= "SESSIONFAC
Tory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "> <!--1, configuring data source Properties--
<property name= "DataSource" ref= "DataSource"/> <!--2, configuration utils configuration file location and name-- <properTy name= "configlocation" value= "Classpath:spring4_hibernate/hibernate.cfg.xml"/> <!--using HibernatePro
Perties Zodiac To configure Hibernate native Properties-<!--<property name= "Hibernateproperties" >--> <!--<props>--> <!--<prop key= "Utils.dialect" >org.utils.dialect.mys ql5innodbdialect</prop>--> <!--<prop key= "Utils.show_sql" >true</prop>-
<!--<prop key= "Utils.format_sql" >true</prop>--> <!--<prop key= "Utils.hbm2ddl.auto" >update</prop>--> <!--</PROPS>--&G
T <!--</property>--> <!--3, configure the location and name of the utils mapping file, you can use the wildcard character--<property name
= "Mappinglocations" value= "Classpath:spring4_hibernate/*.hbm.xml"/> </bean> <!--Configure Spring's declarative transactions-<!--1. Configure transaction Manager-<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate4.HibernateTransactionMana Ger "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <!--2 . Configuring transaction properties requires transaction manager--<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" > <t x:attributes> <tx:method name= "get*" read-only= "true"/> <!--the propagation behavior of the transaction,
Such a successful purchase will not rollback--<tx:method name= "purchase" propagation= "Requires_new"/> <tx:method name= "*"/> </tx:attributes> </tx:advice> <!--3. Configure the transaction pointcut and associate the Tangency and transaction Properties-<aop:config> <aop:pointcut expression= "Execution (* spring4_hibe
Rnate.service.*.* (..)) " Id= "Txpointcut"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txpointcut"/> </aop:config> </beans>