tlias管理系统-spring事务管理&异常回滚robackfor&事务传播行为&部门日志记录案例的功能实现

This commit is contained in:
2025-10-27 16:22:44 +08:00
parent 54d17225d4
commit 54bb1effca
9 changed files with 105 additions and 4 deletions

View File

@@ -38,7 +38,7 @@ public class DeptController {
删除部门 删除部门
*/ */
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id){ public Result delete(@PathVariable Integer id) throws Exception {
log.info("删除部门:{}",id); log.info("删除部门:{}",id);
deptService.delete(id); deptService.delete(id);
return Result.success(); return Result.success();

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

@@ -1,6 +1,7 @@
package com.inmind.mapper; package com.inmind.mapper;
import com.inmind.pojo.Emp; import com.inmind.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
@@ -43,4 +44,7 @@ public interface EmpMapper {
@Select("select * from emp where username =#{username} and password = #{password} ") @Select("select * from emp where username =#{username} and password = #{password} ")
Emp getEmpByUserNameAndPassWord(Emp emp); Emp getEmpByUserNameAndPassWord(Emp emp);
@Delete("delete from emp where dept_id = #{deptId}")
void deleteByDeptId(Integer deptId);
} }

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

@@ -1,6 +1,7 @@
package com.inmind.service; package com.inmind.service;
import com.inmind.pojo.Dept; import com.inmind.pojo.Dept;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
@@ -9,7 +10,7 @@ public interface DeptService {
List<Dept> list(); List<Dept> list();
//根据id删除部门 //根据id删除部门
void delete(Integer id); void delete(Integer id) throws Exception;
//新增部门 //新增部门
void add(Dept dept); void add(Dept dept);

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,10 +1,14 @@
package com.inmind.service.impl; package com.inmind.service.impl;
import com.inmind.mapper.DeptMapper; import com.inmind.mapper.DeptMapper;
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.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@@ -15,6 +19,12 @@ public class DeptServiceImpl implements DeptService {
@Autowired @Autowired
private DeptMapper deptMapper; private DeptMapper deptMapper;
//注入一个员工Mapper操作员工数据
@Autowired
private EmpMapper empMapper;
@Autowired
private DeptLogService deptLogService;
//查询部门列表 @Override //查询部门列表 @Override
public List<Dept> list() { public List<Dept> list() {
//调用deptMapper获取部门列表数据 //调用deptMapper获取部门列表数据
@@ -23,9 +33,29 @@ public class DeptServiceImpl implements DeptService {
} }
//根据id删除部门 //根据id删除部门
@Transactional(rollbackFor = Exception.class)//让当前方法处于事务管理下在方法调用前执行start transaction
@Override @Override
public void delete(Integer id) { public void delete(Integer id) throws Exception {
deptMapper.delete(id); try {
//不仅删除对应部门数据
deptMapper.delete(id);
//进行一些业务逻辑代码,假设业务出现了异常
// int a = 1/0;
//抛出一个编译时异常
if (true) {
throw new Exception("出错了...");
}
//还要删除该部门下的员工数据(根据部门ID删除相关的员工)
empMapper.deleteByDeptId(id);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
//调用部门日志记录业务的插入功能,进行部门日志记录操作
DeptLog deptLog = new DeptLog();
deptLog.setCreateTime(LocalDateTime.now());
deptLog.setDescription("执行了解散部门的操作,此次解散的是"+ id+"号部门");
deptLogService.insert(deptLog);
}
} }
//新增部门 //新增部门

View File

@@ -27,3 +27,8 @@ aliyun:
endpoint: https://oss-cn-hangzhou.aliyuncs.com endpoint: https://oss-cn-hangzhou.aliyuncs.com
bucketName: inmind-test1 bucketName: inmind-test1
region: cn-hangzhou region: cn-hangzhou
#spring事务管理日志
logging:
level:
org.springframework.jdbc.support.JdbcTransactionManager: debug