Spring MVC事务配置
要了解事务配置的所有方法,请看一下《Spring事务配置的5种方法》
本文介绍两种配置方法:
以下所使用环境为Spring4.0.3、Hibernate4.3.5
一、 XML,使用tx标签配置拦截器实现事务
Entity类User.java,持久化类,对应数据库表user
packagecom.lei.demo.entity;import javax.persistence.*;
@Entity(name="users")public classUsers {publicUsers(){super();
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")privateInteger id;
@Column(name="user_name",length=32)privateString user_name;
@Column(name="age")privateInteger age;
@Column(name="nice_name",length=32)privateString nice_name;//属性实现......
}
UserDAO.javar,表user的一些操作,其中属性sessionFactory应该由Spring注入,如下:
packagecom.lei.demo.dao;importjava.util.List;importjavax.annotation.Resource;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.springframework.stereotype.Repository;importcom.lei.demo.entity.Users;public classUsersDAO {privateSessionFactory sessionFactory;public voidsetSessionFactory(SessionFactory sessionFactory) {this.sessionFactory =sessionFactory;
}publicSessionFactory getSessionFactory() {returnsessionFactory;
}public ListgetAllUser(){
String hsql="from users";
Session session=sessionFactory.getCurrentSession();
Query query=session.createQuery(hsql);returnquery.list();
}
}
UserService.java,业务实现类,如下
packagecom.lei.demo.service;importjavax.annotation.Resource;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Isolation;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;import com.lei.demo.dao.*;public classUserService {privateUsersDAO userDao;public intuserCount(){returnuserDao.getAllUser().size();
}publicUsersDAO getUserDao() {returnuserDao;
}public voidsetUserDao(UsersDAO userDao) {this.userDao =userDao;
}
}
首先看一下xml配置,spring-hibernate.xml如下:
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
com.lei.demo.entity
${hibernate.hbm2ddl.auto}
${hibernate.dialect}
${hibernate.show_sql}
其中主要配置中是tx:advice和aop:config两个配置节,以Spring AOP的方式实现事务管理。
tx:advice配置了事务的管理者是transactionManager,同时tx:method也规定了如果方法名匹配“add*”和“get*”方法时使用事务,propagation是设定事务的传播级别。除了“add*”和“get*”方法,其他的方法的事务是只读的(典型地,对于只执行查询的事务你会将该属性设为true,如果出现了更新、插入或是删除语句时只读事务就会失败)
aop:config指定了一个aop:pointcut去引用上边的advice。
这样就通过AOP的拦截机制实现了事务,当然你还要用Spring的方式自己配置UserDAO和UserService。
二、 Annotation方式
第一步,首先看一下web.xml,如下:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
Archetype Created Web Application
contextConfigLocation
classpath:/spring-*.xml
org.springframework.web.context.ContextLoaderListener
lei-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:/lei-dispatcher-servlet.xml
1
lei-dispatcher
/
第二步,spring-hibernate配置,见以下spring-hibernate.xml配置
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
com.lei.demo.entity
${hibernate.hbm2ddl.auto}
${hibernate.dialect}
${hibernate.show_sql}
第一节中xml配置事务中需要通过配置tx:advice和aop:config来增加事务的功能。此处采用全注释方法,这两个配置节就不需要了。
相应的需要在视图解析配置中启用注释,如下lei-dispatcher-servlet.xml
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/spring-context-3.0.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
">
/WEB-INF/user/
.jsp
UserDAO如下
packagecom.lei.demo.dao;importjava.util.List;importjavax.annotation.Resource;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.springframework.stereotype.Repository;importcom.lei.demo.entity.Users;
@Repositorypublic classUsersDAO {
@Resource(name="sessionFactory")privateSessionFactory sessionFactory;public voidsetSessionFactory(SessionFactory sessionFactory) {this.sessionFactory =sessionFactory;
}publicSessionFactory getSessionFactory() {returnsessionFactory;
}public ListgetAllUser(){
String hsql="from users";
Session session=sessionFactory.getCurrentSession();
Query query=session.createQuery(hsql);returnquery.list();
}
}
UserService.java如下
packagecom.lei.demo.service;importjavax.annotation.Resource;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Isolation;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;import com.lei.demo.dao.*;
@Service("userService")public classUserService {
@ResourceprivateUsersDAO userDao;
@Transactionalpublic intuserCount(){returnuserDao.getAllUser().size();
}publicUsersDAO getUserDao() {returnuserDao;
}public voidsetUserDao(UsersDAO userDao) {this.userDao =userDao;
}
}
这里,方法名userCount上加入@Transactional,说明这个方法要启用事务。如果类名UserService上加入@Transactional,则表明这个类中的所有方法都会启用事务。
如果配有多个transactionManager,例如配置有transactionManager1,和transactionManager2,则可以通过@Transactional(“transactionManager1”),的方式指定使用哪个数据源的事务。
源代码下载:
友情赞助
如果您喜欢此文,感觉对您工作有帮助,预期领导会给您涨工资,不妨小额赞助一下,让我有动力继续努力。
赞助方式1:打开支付宝App,使用“扫一扫”付款,付款码见下图,别忘了付款留言哦!
赞助方式2:支付宝扫一下,领取我的的大红包,并花掉,双方受益
赞助方式3:如果您有微信钱包,请打开微信,使用“扫一扫”付款,付款码见下图
转载请注明出处!