From 8cd69726e63513265e3eb4c7603910437eaa63dd Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sun, 7 Dec 2025 16:01:21 +0800 Subject: [PATCH] =?UTF-8?q?tlias=E7=AE=A1=E7=90=86=E7=B3=BB=E7=BB=9F--?= =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E7=AE=A1=E7=90=86-=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E8=BF=9B=E9=98=B6-=E4=BA=8B=E5=8A=A1=E4=BC=A0=E6=92=AD?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA-propagation=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/inmind/mapper/DeptLogMapper.java | 13 ++++++++ .../main/java/com/inmind/pojo/DeptLog.java | 16 ++++++++++ .../com/inmind/service/DeptLogService.java | 9 ++++++ .../service/impl/DeptLogServiceImpl.java | 23 ++++++++++++++ .../inmind/service/impl/DeptServiceImpl.java | 30 +++++++++++++------ 5 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 tlias-web-management/src/main/java/com/inmind/mapper/DeptLogMapper.java create mode 100644 tlias-web-management/src/main/java/com/inmind/pojo/DeptLog.java create mode 100644 tlias-web-management/src/main/java/com/inmind/service/DeptLogService.java create mode 100644 tlias-web-management/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java diff --git a/tlias-web-management/src/main/java/com/inmind/mapper/DeptLogMapper.java b/tlias-web-management/src/main/java/com/inmind/mapper/DeptLogMapper.java new file mode 100644 index 0000000..547a916 --- /dev/null +++ b/tlias-web-management/src/main/java/com/inmind/mapper/DeptLogMapper.java @@ -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); + +} diff --git a/tlias-web-management/src/main/java/com/inmind/pojo/DeptLog.java b/tlias-web-management/src/main/java/com/inmind/pojo/DeptLog.java new file mode 100644 index 0000000..8799f28 --- /dev/null +++ b/tlias-web-management/src/main/java/com/inmind/pojo/DeptLog.java @@ -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; +} diff --git a/tlias-web-management/src/main/java/com/inmind/service/DeptLogService.java b/tlias-web-management/src/main/java/com/inmind/service/DeptLogService.java new file mode 100644 index 0000000..4ad8db1 --- /dev/null +++ b/tlias-web-management/src/main/java/com/inmind/service/DeptLogService.java @@ -0,0 +1,9 @@ +package com.inmind.service; + + +import com.inmind.pojo.DeptLog; + +public interface DeptLogService { + + void insert(DeptLog deptLog); +} diff --git a/tlias-web-management/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java b/tlias-web-management/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java new file mode 100644 index 0000000..4f01d02 --- /dev/null +++ b/tlias-web-management/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java @@ -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); + } +} diff --git a/tlias-web-management/src/main/java/com/inmind/service/impl/DeptServiceImpl.java b/tlias-web-management/src/main/java/com/inmind/service/impl/DeptServiceImpl.java index ca976dc..d0ac069 100644 --- a/tlias-web-management/src/main/java/com/inmind/service/impl/DeptServiceImpl.java +++ b/tlias-web-management/src/main/java/com/inmind/service/impl/DeptServiceImpl.java @@ -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