tlias管理系统--事务管理-事务进阶-事务传播行为-propagation属性

This commit is contained in:
2025-12-07 16:01:21 +08:00
parent 25e5c08574
commit 8cd69726e6
5 changed files with 82 additions and 9 deletions

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -0,0 +1,9 @@
package com.inmind.service;
import com.inmind.pojo.DeptLog;
public interface DeptLogService {
void insert(DeptLog deptLog);
}

View File

@@ -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);
}
}

View File

@@ -1,8 +1,11 @@
package com.inmind.service.impl;
import com.inmind.mapper.DeptLogMapper;
import com.inmind.mapper.DeptMapper;
import com.inmind.mapper.EmpMapper;
import com.inmind.pojo.Dept;
import com.inmind.pojo.DeptLog;
import com.inmind.service.DeptLogService;
import com.inmind.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -21,6 +24,9 @@ public class DeptServiceImpl implements DeptService {
@Autowired
private EmpMapper empMapper;
@Autowired
private DeptLogService deptLogService;
/**
* 查询所有部门信息
* @return
@@ -39,17 +45,23 @@ public class DeptServiceImpl implements DeptService {
@Transactional(rollbackFor = Exception.class)//将当前的删除功能交给事务管理,达到全成功或全失败的业务功能
@Override
public void delete(Integer id) throws Exception {
deptMapper.delete(id);
//todo 可能存在大量的业务处理代码,而代码可能会出现业务异常
try {
deptMapper.delete(id);
// int a = 1/0;
if (true) {
throw new Exception();//模拟抛出一个编译时异常
if (true) {
throw new Exception();//模拟抛出一个编译时异常
}
//当删除部门时,也要把相关的员工删除掉
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);
}
//当删除部门时,也要把相关的员工删除掉
empMapper.deleteByDeptId(id);
}
@Override