需求:为业务层添加声明式事务控制的前提下,像用户表中添加一条用户信息
UserMapper.java
UserMapper.xml
UserService.java
UserServiceImpl.java
applicantionContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
10 http://www.springframework.org/schema/tx
11 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
14
15 <!--配置数据源 -->
16 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
17 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
18 <property name="url"
19 value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8"></property>
20 <property name="username" value="root"></property>
21 <property name="password" value="root"></property>
22
23 </bean>
24 <!--配置SqlSessionFactoryBean -->
25 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
26 <!--获取到你的数据源 -->
27 <property name="dataSource" ref="dataSource"></property>
28 <!--获取到mybatis的配置文件 注意这里使用的是value属性 -->
29 <property name="configLocation" value="classpath:mybatis-config.xml"></property>
30 <!--使用下面这种方式获取sql文件 -->
31 <property name="mapperLocations">
32 <list>
33 <value>classpath:cn/smbms/dao/**/*.xml</value>
34 </list>
35 </property>
36 </bean>
37
38
39
40 <!-- 如果在真实的项目中,映射器比较多的情况下,我们可以使用MapperScannerConfigure扫描基准包 -->
41 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
42 <property name="basePackage" value="cn.smbms.dao"></property>
43 </bean>
44
45 <!--然后改造一下的我们的service层 -->
46 <!--扫描注解定义 -->
47 <context:component-scan base-package="cn.smbms.service"></context:component-scan>
48 <!--事务管理器 -->
49 <bean id="txManager"
50 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
51 <property name="dataSource" ref="dataSource"></property>
52 </bean>
53 <!--事务增强 -->
54 <tx:advice id="txAdvice" transaction-manager="txManager">
55 <tx:attributes>
56 <!-- 表示所有的以find开头的方法 timeout="-1"表示永不超时 timeout="1000" 表示1000毫秒之后将超时 -->
57 <tx:method name="find*" timeout="1000" />
58 <tx:method name="*" propagation="REQUIRED" />
59 </tx:attributes>
60 </tx:advice>
61 <!-- 配置切面 -->
62 <aop:config>
63 <!--首先,配置切入点 -->
64 <aop:pointcut expression="execution(* cn.smbms.service..*.*(..))"
65 id="myPoint" />
66 <!--将切入点和配置的事务增强组合起来 -->
67 <aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint" />
68 </aop:config>
69
70 </beans>
测试方法:
1 @Test
2 public void testAdd(){
3 logger.debug("testAdd !===================");
4
5 try {
6 ApplicationContext ctx=new
7 ClassPathXmlApplicationContext("applicationContext.xml");
8 UserService userService=(UserService)ctx.getBean("userService");
9 User user = new User();
10 user.setUserCode("test001");
11 user.setUserName("测试用户001");
12 user.setUserPassword("1234567");
13 Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("1984-12-12");
14 user.setBirthday(birthday);
15 user.setCreationDate(new Date());
16 user.setAddress("地址测试");
17 user.setGender(1);
18 user.setPhone("13688783697");
19 user.setUserRole(1);
20 user.setCreatedBy(1);
21 user.setCreationDate(new Date());
22 boolean flag = userService.addNewUser(user);
23 logger.info("添加用户的结果:"+flag);
24
25 } catch (Exception e) {
26 // TODO: handle exception
27 e.printStackTrace();
28
29 }finally{
30
31 }
32
33 }
运行结果:
Spring整合Mybatis框架-为业务层添加声明式事务控制
原文:https://www.cnblogs.com/dongyaotou/p/12152877.html