1. 创立数据库实体类以及 mapper
1.1 数据库字段
数据库表字段如下所示:
1.2 创立对应实体类
package com.aries.jc.dciTest.modules.entity.local;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("tb_test")
public class TbTest {@TableField("region_name")
private String regionName;
@TableField("region_code")
private String regionCode;
@TableField("region_config")
private Integer regionConfig;
}
1.3 创立对应 mapper
package com.aries.jc.dciTest.modules.mapper.local;
import com.aries.jc.dciTest.modules.entity.local.TbTest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TbTestMapper extends BaseMapper<TbTest> {
}
1.4 创立 service 层接口:
package com.aries.jc.dciTest.modules.service;
import com.aries.jc.dciTest.modules.entity.local.TbTest;
import com.hikvision.ga.common.BaseResult;
public interface TbTestService {BaseResult insertTbTest1(TbTest tbTest);
BaseResult insertTbTest2(TbTest tbTest);
BaseResult insertTbTest3(TbTest tbTest);
}
1.5 创立 service 层接口实现类
办法 insertTbTest1 执行失常的数据库增加操作
办法 insertTbTest2 执行数据库增加操作后,会通过 int l = 2/0;
触发一个异样
办法 insertTbTest3 执行数据库增加操作后,会通过 int l = 2/0;
触发一个异样,但在该办法上增加了注解
@Transactional(rollbackFor = Exception.class)
来执行事务回滚
package com.aries.jc.dciTest.modules.service.impl;
import com.aries.jc.dciTest.modules.entity.local.TbTest;
import com.aries.jc.dciTest.modules.mapper.local.TbTestMapper;
import com.aries.jc.dciTest.modules.service.TbTestService;
import com.hikvision.ga.common.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TbTestServiceImpl implements TbTestService {private static final Logger LOGGER = LoggerFactory.getLogger(TbTestServiceImpl.class);
@Autowired
private TbTestMapper tbTestMapper;
/**
* 向数据库增加操作,不触发异样
* @param tbTest
* @return
*/
@Override
public BaseResult insertTbTest1(TbTest tbTest) {BaseResult baseResult = new BaseResult();
// 向数据库增加操作
int i = tbTestMapper.insert(tbTest);
if (i != 0){LOGGER.info("增加胜利");
}
baseResult.setMsg("增加胜利");
baseResult.setCode("0");
return baseResult;
}
/**
* 向数据库增加操作,增加后触发异样,但不执行事务回滚
* @param tbTest
* @return
*/
@Override
public BaseResult insertTbTest2(TbTest tbTest) {BaseResult baseResult = new BaseResult();
// 向数据库增加操作
int i = tbTestMapper.insert(tbTest);
if (i != 0){LOGGER.info("增加胜利");
}
// 触发异样
int l = 2/0;
baseResult.setMsg("增加胜利");
baseResult.setCode("0");
return baseResult;
}
/**
* 向数据库增加操作,增加后触发异样,执行事务回滚
* @param tbTest
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public BaseResult insertTbTest3(TbTest tbTest) {BaseResult baseResult = new BaseResult();
// 向数据库增加操作
int i = tbTestMapper.insert(tbTest);
if (i != 0){LOGGER.info("增加胜利");
}
// 触发异样
int l = 2/0;
baseResult.setMsg("增加胜利");
baseResult.setCode("0");
return baseResult;
}
}
1.6 创立 controller 层
package com.aries.jc.dciTest.modules.controller;
import com.aries.jc.dciTest.modules.entity.local.PointConfig;
import com.aries.jc.dciTest.modules.entity.local.TbTest;
import com.aries.jc.dciTest.modules.service.TbTestService;
import com.hikvision.ga.common.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Api(tags = "测试")
@RequestMapping("/test")
public class TbTestController {
@Autowired
private TbTestService tbTestService;
@RequestMapping(value = "/insert1", method = RequestMethod.POST)
@ApiOperation("测试事务回滚:失常增加,不触发异样")
public BaseResult insertTbTest1(@RequestBody TbTest tbTest){return tbTestService.insertTbTest1(tbTest);
}
@RequestMapping(value = "/insert2", method = RequestMethod.POST)
@ApiOperation("测试事务回滚:失常增加,增加后触发异样,不执行事务回滚")
public BaseResult insertTbTest2(@RequestBody TbTest tbTest){return tbTestService.insertTbTest2(tbTest);
}
@RequestMapping(value = "/insert3", method = RequestMethod.POST)
@ApiOperation("测试事务回滚:失常增加,增加后触发异样,执行事务回滚")
public BaseResult insertTbTest3(@RequestBody TbTest tbTest){return tbTestService.insertTbTest3(tbTest);
}
}
2.swagger 测试:
失常增加,不触发异样:办法 insertTbTest1
失常向数据库增加
失常增加,增加后触发异样,不执行事务回滚:办法 insertTbTest2
向数据库胜利增加后,触发了异样,但因为没有事务回滚,数据库中就保留了增加的对象
失常增加,增加后触发异样,执行事务回滚:办法 insertTbTest3
向数据库胜利增加后,触发了异样,但因为设置了事务回滚,数据库就回滚了该办法调用之前的状态,即在数据库中没有新增加的对象