tlias管理系统--事务管理-事务进阶-事务传播行为-propagation属性
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind.mapper;
|
||||||
|
|
||||||
|
import com.inmind.pojo.DeptLog;
|
||||||
|
import org.apache.ibatis.annotations.Insert;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface DeptLogMapper {
|
||||||
|
|
||||||
|
@Insert("insert into dept_log(create_time,description) values(#{createTime},#{description})")
|
||||||
|
void insert(DeptLog log);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.pojo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DeptLog {
|
||||||
|
private Integer id;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
private String description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.inmind.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.inmind.pojo.DeptLog;
|
||||||
|
|
||||||
|
public interface DeptLogService {
|
||||||
|
|
||||||
|
void insert(DeptLog deptLog);
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
|
import com.inmind.mapper.DeptLogMapper;
|
||||||
|
import com.inmind.pojo.DeptLog;
|
||||||
|
import com.inmind.service.DeptLogService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DeptLogServiceImpl implements DeptLogService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DeptLogMapper deptLogMapper;
|
||||||
|
|
||||||
|
//设置当前部门日志插入操作为,必须是一个单独的新事务,不受其他事务的影响
|
||||||
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
|
@Override
|
||||||
|
public void insert(DeptLog deptLog) {
|
||||||
|
deptLogMapper.insert(deptLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
package com.inmind.service.impl;
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
|
import com.inmind.mapper.DeptLogMapper;
|
||||||
import com.inmind.mapper.DeptMapper;
|
import com.inmind.mapper.DeptMapper;
|
||||||
import com.inmind.mapper.EmpMapper;
|
import com.inmind.mapper.EmpMapper;
|
||||||
import com.inmind.pojo.Dept;
|
import com.inmind.pojo.Dept;
|
||||||
|
import com.inmind.pojo.DeptLog;
|
||||||
|
import com.inmind.service.DeptLogService;
|
||||||
import com.inmind.service.DeptService;
|
import com.inmind.service.DeptService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -21,6 +24,9 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private EmpMapper empMapper;
|
private EmpMapper empMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DeptLogService deptLogService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询所有部门信息
|
* 查询所有部门信息
|
||||||
* @return
|
* @return
|
||||||
@@ -39,17 +45,23 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
@Transactional(rollbackFor = Exception.class)//将当前的删除功能交给事务管理,达到全成功或全失败的业务功能
|
@Transactional(rollbackFor = Exception.class)//将当前的删除功能交给事务管理,达到全成功或全失败的业务功能
|
||||||
@Override
|
@Override
|
||||||
public void delete(Integer id) throws Exception {
|
public void delete(Integer id) throws Exception {
|
||||||
|
try {
|
||||||
deptMapper.delete(id);
|
deptMapper.delete(id);
|
||||||
|
|
||||||
//todo 可能存在大量的业务处理代码,而代码可能会出现业务异常
|
|
||||||
// int a = 1/0;
|
// int a = 1/0;
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
throw new Exception();//模拟抛出一个编译时异常
|
throw new Exception();//模拟抛出一个编译时异常
|
||||||
}
|
}
|
||||||
|
|
||||||
//当删除部门时,也要把相关的员工删除掉
|
//当删除部门时,也要把相关的员工删除掉
|
||||||
empMapper.deleteByDeptId(id);
|
empMapper.deleteByDeptId(id);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
//无论是否出现异常,一定会执行finally中的代码
|
||||||
|
DeptLog deptLog = new DeptLog();
|
||||||
|
deptLog.setCreateTime(LocalDateTime.now());
|
||||||
|
deptLog.setDescription("执行了部门删除操作,删除的部门号是:"+id);
|
||||||
|
deptLogService.insert(deptLog);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user