From 5aeca5f5a73a635637ab4d7bf8e56c93ec4c82eb Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Mon, 2 Nov 2026 17:28:21 +0800 Subject: [PATCH] =?UTF-8?q?javaEEday11-tlias=E7=AE=A1=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F-spring=E4=BA=8B=E5=8A=A1=E7=AE=A1=E7=90=86&=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=9B=9E=E6=BB=9Arobackfor&=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BC=A0=E6=92=AD=E8=A1=8C=E4=B8=BA&=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E6=A1=88=E4=BE=8B=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/inmind/controller/DeptController.java | 2 +- .../com/inmind/exception/GlobalException.java | 19 +++++++ .../java/com/inmind/mapper/DeptLogMapper.java | 13 +++++ .../java/com/inmind/mapper/EmpMapper.java | 5 ++ .../main/java/com/inmind/pojo/DeptLog.java | 16 ++++++ .../com/inmind/service/DeptLogService.java | 9 +++ .../java/com/inmind/service/DeptService.java | 2 +- .../service/impl/DeptLogServiceImpl.java | 23 ++++++++ .../inmind/service/impl/DeptServiceImpl.java | 55 +++++++++++++------ .../main/java/com/inmind/utils/JwtUtils.java | 4 +- tlias-web/src/main/resources/application.yml | 5 +- 11 files changed, 131 insertions(+), 22 deletions(-) create mode 100644 tlias-web/src/main/java/com/inmind/exception/GlobalException.java create mode 100644 tlias-web/src/main/java/com/inmind/mapper/DeptLogMapper.java create mode 100644 tlias-web/src/main/java/com/inmind/pojo/DeptLog.java create mode 100644 tlias-web/src/main/java/com/inmind/service/DeptLogService.java create mode 100644 tlias-web/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java diff --git a/tlias-web/src/main/java/com/inmind/controller/DeptController.java b/tlias-web/src/main/java/com/inmind/controller/DeptController.java index 42f4634..297404d 100644 --- a/tlias-web/src/main/java/com/inmind/controller/DeptController.java +++ b/tlias-web/src/main/java/com/inmind/controller/DeptController.java @@ -35,7 +35,7 @@ public class DeptController { // @DeleteMapping("/depts/{id}") @DeleteMapping("/{id}") //PathVariable:路径参数的注解,用来将url地址上的id参数赋值给形参id - public Result deleteDept(@PathVariable Integer id){ + public Result deleteDept(@PathVariable Integer id) throws Exception { log.info("删除部门:{}",id); //调用业务层的删除部门功能 diff --git a/tlias-web/src/main/java/com/inmind/exception/GlobalException.java b/tlias-web/src/main/java/com/inmind/exception/GlobalException.java new file mode 100644 index 0000000..0179a32 --- /dev/null +++ b/tlias-web/src/main/java/com/inmind/exception/GlobalException.java @@ -0,0 +1,19 @@ +package com.inmind.exception; + +import com.inmind.pojo.Result; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice//配置为全局异常处理器 +public class GlobalException { + + /* + 定义一个全局异常处理的方法:只要出现异常就会被ex方法处理 + */ + @ExceptionHandler(Exception.class) + public Result ex(Exception e){ + e.printStackTrace(); + //日志框架logback,把异常信息保存到日志文件 + return Result.error("对不起,服务器报错,请联系管理员"); + } +} diff --git a/tlias-web/src/main/java/com/inmind/mapper/DeptLogMapper.java b/tlias-web/src/main/java/com/inmind/mapper/DeptLogMapper.java new file mode 100644 index 0000000..547a916 --- /dev/null +++ b/tlias-web/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/src/main/java/com/inmind/mapper/EmpMapper.java b/tlias-web/src/main/java/com/inmind/mapper/EmpMapper.java index 3ce6a3e..bee1819 100644 --- a/tlias-web/src/main/java/com/inmind/mapper/EmpMapper.java +++ b/tlias-web/src/main/java/com/inmind/mapper/EmpMapper.java @@ -1,6 +1,7 @@ package com.inmind.mapper; import com.inmind.pojo.Emp; +import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @@ -47,4 +48,8 @@ public interface EmpMapper { void updateEmp(Emp emp); @Select("select * from emp where username = #{username} and password = #{password};") Emp getEmpByUserAndPwd(Emp emp); + + + @Delete("delete from emp where dept_id = #{deptId}") + void deleteByDeptId(Integer deptId); } diff --git a/tlias-web/src/main/java/com/inmind/pojo/DeptLog.java b/tlias-web/src/main/java/com/inmind/pojo/DeptLog.java new file mode 100644 index 0000000..8799f28 --- /dev/null +++ b/tlias-web/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/src/main/java/com/inmind/service/DeptLogService.java b/tlias-web/src/main/java/com/inmind/service/DeptLogService.java new file mode 100644 index 0000000..4ad8db1 --- /dev/null +++ b/tlias-web/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/src/main/java/com/inmind/service/DeptService.java b/tlias-web/src/main/java/com/inmind/service/DeptService.java index 47a3636..284807c 100644 --- a/tlias-web/src/main/java/com/inmind/service/DeptService.java +++ b/tlias-web/src/main/java/com/inmind/service/DeptService.java @@ -16,7 +16,7 @@ public interface DeptService { * 删除部门 * @param id 部门id */ - void deleteDept(Integer id); + void deleteDept(Integer id) throws Exception; /** * 新增部门 diff --git a/tlias-web/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java b/tlias-web/src/main/java/com/inmind/service/impl/DeptLogServiceImpl.java new file mode 100644 index 0000000..ee2b396 --- /dev/null +++ b/tlias-web/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/src/main/java/com/inmind/service/impl/DeptServiceImpl.java b/tlias-web/src/main/java/com/inmind/service/impl/DeptServiceImpl.java index 581a31f..f1c92ee 100644 --- a/tlias-web/src/main/java/com/inmind/service/impl/DeptServiceImpl.java +++ b/tlias-web/src/main/java/com/inmind/service/impl/DeptServiceImpl.java @@ -1,52 +1,73 @@ package com.inmind.service.impl; 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.Service; +import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.List; -@Service //将当前类加载IOC容器(spring容器)进行管理 +@Service public class DeptServiceImpl implements DeptService { @Autowired private DeptMapper deptMapper; - //查询所有部门 + //注入一个员工Mapper,操作员工数据 + @Autowired + private EmpMapper empMapper; + @Autowired + private DeptLogService deptLogService; + + //查询部门列表 @Override public List deptList() { - //调用mapper中功能,获取部门数据 + //调用deptMapper获取部门列表数据 List deptList = deptMapper.deptList(); return deptList; } - /** - * 删除部门 - * @param id 部门id - */ + //根据id删除部门 + @Transactional(rollbackFor = Exception.class)//让当前方法,处于事务管理下(在方法调用前,执行start transaction) @Override - public void deleteDept(Integer id) { - //调用dao,执行删除部门操作 - deptMapper.deleteDept(id); + public void deleteDept(Integer id) throws Exception { + try { + //不仅删除对应部门数据 + deptMapper.deleteDept(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); + } } - /** - * 新增部门功能 - * @param dept 部门信息 - */ + //新增部门 @Override public void addDept(Dept dept) { - //1.补充更新时间和创建时间 + //补充基础属性,创建时间和更新时间 dept.setCreateTime(LocalDateTime.now()); dept.setUpdateTime(LocalDateTime.now()); - //2.调用mapper的新增功能 deptMapper.addDept(dept); } - @Override public Dept getDeptById(Integer id) { //调用Mapper查询部门数据即可 diff --git a/tlias-web/src/main/java/com/inmind/utils/JwtUtils.java b/tlias-web/src/main/java/com/inmind/utils/JwtUtils.java index 6537939..2976bd7 100644 --- a/tlias-web/src/main/java/com/inmind/utils/JwtUtils.java +++ b/tlias-web/src/main/java/com/inmind/utils/JwtUtils.java @@ -9,8 +9,8 @@ import java.util.Map; public class JwtUtils { private static String signKey = "inmind"; -// private static Long expire = 43200000L; - private static Long expire = 10000L; + private static Long expire = 43200000L; +// private static Long expire = 10000L; /** * 生成JWT令牌 diff --git a/tlias-web/src/main/resources/application.yml b/tlias-web/src/main/resources/application.yml index cebbac6..395f814 100644 --- a/tlias-web/src/main/resources/application.yml +++ b/tlias-web/src/main/resources/application.yml @@ -23,7 +23,10 @@ aliyun: bucketName: inmind-test1 region: cn-hangzhou - +#spring事务管理日志 +logging: + level: + org.springframework.jdbc.support.JdbcTransactionManager: debug #定义对象/map集合