beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring 读取db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<context:component-scan base-package="com.liuyun"></context:component-scan>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- spring-jdbc内置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 声明式事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
关键代码:
配合注解的使用:UserServiceImpl.java
/*
* Copyright (c) 2019, wenwenliuyun@163.com All Rights Reserved.
*/
package com.liuyun.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.liuyun.dao.IUserDao;
import com.liuyun.domain.User;
import com.liuyun.service.IUserService;
/**
* Function: 业务实现类 <br>
*
* @author liuyun
* @version
* @since 2019年8月29日下午7:43:54
*/
@Service("userService")
@Transactional // 默认值propagation = Propagation.REQUIRED, readOnly = false
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public long count() throws Exception {
System.out.println("Service的count执行");
return userDao.count();
}
@Override
public int deleteById(Integer id) throws Exception {
return userDao.deleteById(id);
}
@Override
public int insert(User record) throws Exception {
int count = userDao.insert(record);
int i = 1/0;// 验证事务回滚
return count;
}
@Override
public int update(Integer id, User record) throws Exception {
if (id == null || record == null) {
throw new RuntimeException("update input params cannot be null...");
}
record.setId(id);
return userDao.update(record);
}
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public List<User> findAll() throws Exception {
return userDao.findAll();
}
@Override
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public User findById(Integer id) throws Exception {
return userDao.findById(id);
}
}
关键代码:修饰类和方法,指定事务控制
完整代码:https://github.com/liuyunHappy/spring-tx-xml-annotation.git