java中的事务(二)

package com.leqi.manage.service;

import com.leqi.manage.model.Customer;
import com.dakun.jianzhong.utils.AbstractService;
import com.leqi.manage.mapper.CustomerMapper;
import com.leqi.manage.model.CustomerNature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

/**
 * Created by liuqiulan on Fri Jun 08 17:29:27 CST 2018.
 * @author liuqiulan
 */
@Service
@Transactional(rollbackFor = Exception.class) //每一个方法都是一个事务
public class CustomerService extends AbstractService<Customer> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private CustomerNatureService customerNatureService;

    /**
     * 编辑客户信息
     */
    public Customer saveOrUpdate(Customer customer) {//但是包裹在这个方法里了,就包装成一个事务了,任意错误发生都会回滚每一个原子事务
        //客户信息更新
        this.update(customer);//原子事务
        //中间表信息更新
        //客户性质ids
        String[] ids = customer.getCompanyNatures().split(",");
        CustomerNature customerNature = new CustomerNature();
        customerNature.setCustomerId(customer.getId());
        customerNatureService.deletObj(customerNature);//原子事务
        for (String id : ids) {
            customerNature.setNature(Byte.valueOf(id));
            customerNatureService.save(customerNature);//原子事务
            customerNature.setId(null);
        }
        return customer;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值