spring事务管理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring事务管理相关的知识,希望对你有一定的参考价值。
spring事务管理依赖的包 spring-tx.jar spring-aop.jar
目录结构:(springmvc-servlet.xml是springmvc配置文件,先不要管)
这里事务管理整合了mybatis,spring整合mybatis上上篇文章有介绍,这里不详细介绍
applicationContext.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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 "> <!-- 引入配置文件 --> <context:property-placeholder location="jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件--> <property name="mapperLocations" value="classpath:cn/ys/dao/**/*.xml" /> </bean> <!--mybatis spring sqlSessionTemplate,使用时直接让spring注入即可 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="myTransaction"> <tx:attributes> <tx:method name="transfer*"/> </tx:attributes> </tx:advice> <aop:config > <aop:pointcut id="myCut" expression="execution(* cn.ys.service.*.*.*(..))"></aop:pointcut> <aop:advisor advice-ref="myTransaction" pointcut-ref="myCut"></aop:advisor> </aop:config> <context:component-scan base-package="cn.ys.service.userService"></context:component-scan> </beans>
//========sqlMapConfig.xml=============================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
//========jdbc.properties(用你自己的数据库配置信息)==============
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3307/xiangmu1?useUnicode=true&characterEncoding=UTF-8
jdbc.username = root
jdbc.password = 123456
//========UserDao和UserDao.xml文件=======================
@MapperScan("userDao")
public interface UserDao{
User findUserByName(@Param("name")String name);
void ageHigher();
void ageLower();
User findUserByLogin(@Param("name") String name,@Param("password") String password);
List<User> findUserNameLike(@Param("name") String name);
}
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.ys.dao.UserDao.UserDao"> <select id="findUserByName" resultType="cn.ys.model.User"> select * from user where name=#{name} </select> <update id="ageHigher" > update user set age = age-5 where name=‘赵日天‘ </update> <update id="ageLower" > update user set age = age+5 where name=‘赵松‘ </update> </mapper>
//======BaseService (提供sqlSessionTemplate给子类)=========
@Service//注册
public class BaseService {
private SqlSessionTemplate sqlSessionTemplate;
@Autowired
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
}
//====UserService和UserServiceImpl========================
public interface UserService {
void transfer();
User findUserByName(String name);
List<User> findUserNameLike(String name);
}
================================================
@Service("userService")//注册
public class UserServiceImpl extends BaseService implements UserService {
public void transfer() {
UserDao userDao = getSqlSessionTemplate().getMapper(UserDao.class);
userDao.ageHigher();
// System.out.println(1/0);
userDao.ageLower();
}
public User findUserByName(String name){
UserDao userDao = getSqlSessionTemplate().getMapper(UserDao.class);
return userDao.findUserByName(name);
}
public List<User> findUserNameLike(String name) {
UserDao userDao = getSqlSessionTemplate().getMapper(UserDao.class);
return userDao.findUserNameLike(name);
}
}
//======最后测试=====================================
(改动的UserServiceImpl 的transfer()方法,使其出现异常,继续测试)
(改动applicationContext的aop:config中exepress,使其事务失效,测试)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class Mytest1 {
@Autowired
private UserService userService;
@Test
public void test1(){
userService.transfer();
User user1 = userService.findUserByName("赵日天");
User user2 = userService.findUserByName("赵松");
System.out.println(user1.toString()+"\n"+user2.toString());
}
}
以上是关于spring事务管理的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
Spring boot:thymeleaf 没有正确渲染片段
What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段