Reference article: http://www.cnblogs.com/leiOOlei/p/3725911.html
Own personal experience, source problem this.sessionFactory.getCurrentSession (). Save (obj); Cause when a transaction is configured in SPRINGMVC, the transaction annotations should be followed by the class scan annotations, and then to the method that corresponds to the DAO layer is added @transaction
The full annotations are configured as follows:
The first step is to take a look at Web. XML, as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "http ://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3 .0 "> <display-name>archetype Created Web application</display-name> <context-param> <param-n Ame>contextconfiglocation</param-name> <param-value>classpath:/spring-*.xml</param-value> </context-param> <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>lei-dispatcher</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:/lei-dispatcher-servlet.xml</param-value> </init-param> &L t;load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> Lei-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></ Web-app>
The second step, spring-hibernate configuration, see the following spring-hibernate.xml configuration
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:ao p= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPR Ing-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/sprin G-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0 . xsd Http://www.springframework.org/schemA/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!--configuration data source--><bean id=" DataSource "Class=" Org.springframework.jdbc.datasource.DriverManagerDataSource "><property name=" Driverclassname " Value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "Jdbc:mysql://localhost:3306/test? Characterencoding=utf-8 "/><property name=" username "value=" root "/><property name=" password "value=" Root "/></bean><!--configuration Sessionfactory--><bean id=" Sessionfactory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/ ><property name= "hibernateproperties" ><props><prop key= "Hibernate.dialect" > Org.hibernate.dialect.mysqldialect</prop><prop key= "Hibernate.hbm2ddl.auto" >update</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hiberante.format_sql" >TRUE</PR Op> <pROP key= "Current_session_context_class" >thread</prop></props></property><property name= " Configlocations "><list><value>classpath*:hibernate.cfg.test.xml</value></list></ Property></bean><bean id= "TransactionManager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "><property name=" sessionfactory "ref=" Sessionfactory "></property></bean></beans>
Step three: Springmvc configuration
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context htt P://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/mvc http ://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www. Springframework.org/schema/tx/spring-tx-4.0.xsd "><!--Note Scan package--><context:component-scan base-package=" Com.example.* "/> <tx:annotation-driven TranSaction-manager= "TransactionManager"/><!--the introduction of annotation class Two can be annotated--><mvc:annotation-driven/><!--< Bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>--><!-- <bean class= "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>--><! --File Upload configuration--><bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "> <property name=" defaultencoding "value = "Utf-8"/> <property name= "maxuploadsize" value= "10485760000"/> <property name= "Maxinmemorysize" V Alue= "40960"/></bean><!--static resource access configuration--><mvc:resources location= "/img/" mapping= "/img/**"/>< Mvc:resources location= "/topui/" mapping= "/topui/**"/><!--View resolver--><bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" prefix "value="/"> </property><property NAMe= "suffix" value= ". jsp" ></property></bean> </beans>
To this configuration, the corresponding DAO layer is as follows:
Package Com.example.dao.impl;import Java.util.list;import Javax.annotation.resource;import Org.hibernate.sessionfactory;import Org.springframework.stereotype.repository;import Org.springframework.transaction.annotation.transactional;import Com.example.dao.ifilesdao;import Com.example.entity.Files, @Repositorypublic class Filesdao implements ifilesdao{@Resourceprivate sessionfactory sessionfactory; @SuppressWarnings ("Unchecked") @Overridepublic list<files> findfileslist (String ID) {//TODO Auto-generated method Stubreturn this.sessionFactory.getCurrentSession (). CreateQuery ("from Files where id= '" + id+ "'") . List ();} @Transactional @overridepublic void DeleteFile (Files file) {this.sessionFactory.getCurrentSession (). Delete (file);} @Transactional @overridepublic void Updatefile (Files file) {//TODO auto-generated method Stubthis.sessionFactory.getCurrentSession (). Update (file);} @Transactional @overridepublic void SaveFiles (Files file) {//TODO auto-generated method stubSystem.out.println (file.GetId ()); This.sessionFactory.getCurrentSession (). Save (file); System.out.println (File.getid ());}}
The details are shown below, but there are other ways to configure the spring transaction configuration. See 5 ways to access the spring transaction configuration
SPRINGMVC Transaction configuration (Problem Source: Why the data is not saved)