SpringBoot中使用事务
首先在启动类似添加注解
@EnableTransactionManagement
然后在service的实现方法上添加注解
@Transactional(rollbackFor = CustomException.class)
这里使用自定义异常进行处理,抛自定义异常进行数据的回滚
package com.ruoyi.common.exception;
/**
* 自定义异常
*
* @author ruoyi
*/
public class CustomException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Integer code;
private String message;
public CustomException(String message) {
this.message = message;
}
public CustomException(String message, Integer code) {
this.message = message;
this.code = code;
}
public CustomException(String message, Throwable e) {
super(message, e);
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public Integer getCode() {
return code;
}
}
具体的service方法
/**
* 添加流程
* @param flowVo
* @return
*/
@Override
@Transactional(rollbackFor = CustomException.class)
public AjaxResult saveFlow(FlowVo flowVo) {
// 流程表中插入数据
Flow flow = new Flow();
if (!StringUtils.isNull(flowVo)) {
// 流程名字唯一
if (!getFlowName(flowVo.getFlowName())) {
flow.setFlowName(flowVo.getFlowName());
// 判断流程次数
if (flowVo.getCyclicTimes() == null) {
flow.setCyclicTimes(1L);
} else {
if (flowVo.getCyclicTimes().toString().trim().length() == 0) {
flow.setCyclicTimes(1L);
} else {
flow.setCyclicTimes(flowVo.getCyclicTimes());
}
}
save(flow);
} else {
throw new CustomException("流程名已存在");
}
// 循环遍历任务列表
List<FlowTask> taskList = flowVo.getTaskList();
if (StringUtils.isNotEmpty(taskList)) {
for (int i = 0; i < taskList.size(); i++) {
FlowTask flowTask = taskList.get(i);
// 验证
if (StringUtils.isEmpty(flowTask.getTaskId())) {
throw new CustomException("任务id不能为空或空格");
} else {
// 判断任务id是否存在
if (!taskService.checkTaskId(flowTask.getTaskId())) {
throw new CustomException("任务id不存在");
}
}
if (StringUtils.isEmpty(flowTask.getTaskName())) {
throw new CustomException("任务名称不能为空或空格");
} else {
// 判断任务名称是否存在
if (!taskService.checkTaskName(flowTask.getTaskName())) {
throw new CustomException("任务名称不存在");
} else {
// 判断任务名称和任务id是否匹配
if (!taskService.checkTaskIdAndName(flowTask.getTaskId(), flowTask.getTaskName())) {
throw new CustomException("任务id和任务名称不匹配");
}
}
}
if (StringUtils.isEmpty(flowTask.getDeviceType())) {
throw new CustomException("设备类型不能为空或空格");
} else {
if (("1").equals(flowTask.getDeviceType()) || ("0").equals(flowTask.getDeviceType())) {
// 任务id和任务名称以及和类型都应该保持一致
if (!taskService.checkTaskIdAndNameAndType(flowTask.getTaskId(), flowTask.getTaskName(), flowTask.getDeviceType())) {
throw new CustomException("设备类型与任务名称不匹配");
}
} else {
throw new CustomException("设备类型错误");
}
}
if (flowTask.getParam1().length() > 50 || flowTask.getParam2().length() > 50) {
throw new CustomException("参数超长错误");
}
if (StringUtils.isNull(flowTask.getTaskStart())) {
throw new CustomException("启动次数为空");
}
if (flowTask.getTaskStart() < 1 || flowTask.getTaskStart() > 999999) {
throw new CustomException("启动次数错误");
}
if (StringUtils.isEmpty(flowTask.getIsCyclic())) {
throw new CustomException("循环不能为空或空格");
} else {
if (("1").equals(flowTask.getIsCyclic()) || ("0").equals(flowTask.getIsCyclic())) {
} else {
throw new CustomException("循环类型错误");
}
}
flowTask.setFlowId(flow.getFlowId());
flowTask.setTaskSort(i);
flowTaskService.save(flowTask);
}
}
}
return null;
}
在controller中进行调用即可,并且捕获自定义的异常,将信息给前端展示
@PostMapping("/add")
public AjaxResult add(@Validated @RequestBody FlowVo flowVo) {
try {
flowService.saveFlow(flowVo);
}catch (CustomException e){
return AjaxResult.error(e.getMessage());
}
return AjaxResult.success("添加成功");
}
作者:山丘!
-------------------------------------------
你闻讯而来,我大喜过望,我在这等你,你又在哪呢?喜欢的话加一个“关注”呗!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!