基于注解方式的声明式事务

资源下载链接

基于注解方式的声明式事务

一、需求分析

        掌握基于注解方式的声明式事务实现

二、编码实现

1、数据库环境

        mybatis数据库,t_employee表

2、新建项目

        引入pom.xml文件,导入applicationContext.xml、db.properties、log4j.properties、mybatisConfig.xml

3、数据封装类

        com.sw.pojo包,Employee类

@Data
public class Employee {
    private int id;
    private String name;
    private int age;
    private String position;
}
4、mapper层

        com.sw.mapper包,EmployeeMapper接口

public interface EmployeeMapper {
    int insertOne(Employee employee);
}

        com/sw/mapper目录,EmployeeMapper.xml文件

    <insert id="insertOne" parameterType="com.sw.pojo.Employee">
        insert into t_employee set name=#{name},age=#{age},position=#{position}
    </insert>
5、service层

        com.sw.service包,EmployeeService接口

public interface EmployeeService {
    int insertList(List<Employee> employeeList);
}

        com.sw.service.impl包,EmployeeServiceImpl类

@Service
public class EmployeeServiceImpl implements EmployeeService {
​
    @Resource
    private EmployeeMapper employeeMapper;
​
    public int insertList(List<Employee> employeeList) {
        if (employeeList==null){
            return 0;
        }
        if (employeeList.size()==0){
            return 0;
        }
        for (int i = 0; i < employeeList.size(); i++) {
            if (i==1){
                throw new RuntimeException("模拟运行时异常");
            }
            employeeMapper.insertOne(employeeList.get(i));
        }
        return employeeList.size();
    }
}
6、配置Spring

        resources目录,applicationContext.xml

1)配置事务管理器
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
2)配置事务注解
    <!--配置事务注解-->
    <tx:annotation-driven/>
7、添加事物注解

        com.sw.service.impl包,EmployeeServiceImpl类的insertList方法

    @Transactional
    public int insertList(List<Employee> list)
8、测试

        com.sw.service包,EmployeeService接口的insertList方法,右键→Generate→Test

    private EmployeeService employeeService;

    @Before
    public void setUp() throws Exception {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        employeeService = applicationContext.getBean("employeeServiceImpl", EmployeeService.class);
    }

    @Test
    public void insertList() {
        List<Employee> employeeList = new ArrayList<Employee>();
        Employee employee1 = new Employee();
        employee1.setName("a1");
        employee1.setAge(11);
        employee1.setPosition("员工");
        Employee employee2 = new Employee();
        employee2.setName("a2");
        employee2.setAge(22);
        employee2.setPosition("员工");
        Employee employee3 = new Employee();
        employee3.setName("a3");
        employee3.setAge(33);
        employee3.setPosition("员工");
        employeeList.add(employee1);
        employeeList.add(employee2);
        employeeList.add(employee3);
        int res = employeeService.insertList(employeeList);
        if (res>0){
            System.out.println("批量插入成功,共插入"+res+"条数据");
        }else {
            System.out.println("批量插入失败");
        }
    }

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇 士 Teacher

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值