javaEEday11-tlias管理系统-spring事务管理&异常回滚robackfor&事务传播行为&部门日志记录案例的功能实现
This commit is contained in:
@@ -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);
|
||||
|
||||
//调用业务层的删除部门功能
|
||||
|
||||
@@ -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("对不起,服务器报错,请联系管理员");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public interface DeptService {
|
||||
* 删除部门
|
||||
* @param id 部门id
|
||||
*/
|
||||
void deleteDept(Integer id);
|
||||
void deleteDept(Integer id) throws Exception;
|
||||
|
||||
/**
|
||||
* 新增部门
|
||||
|
||||
@@ -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,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<Dept> deptList() {
|
||||
//调用mapper中功能,获取部门数据
|
||||
//调用deptMapper获取部门列表数据
|
||||
List<Dept> deptList = deptMapper.deptList();
|
||||
return deptList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除部门
|
||||
* @param id 部门id
|
||||
*/
|
||||
//根据id删除部门
|
||||
@Transactional(rollbackFor = Exception.class)//让当前方法,处于事务管理下(在方法调用前,执行start transaction)
|
||||
@Override
|
||||
public void deleteDept(Integer id) {
|
||||
//调用dao,执行删除部门操作
|
||||
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查询部门数据即可
|
||||
|
||||
@@ -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令牌
|
||||
|
||||
@@ -23,7 +23,10 @@ aliyun:
|
||||
bucketName: inmind-test1
|
||||
region: cn-hangzhou
|
||||
|
||||
|
||||
#spring事务管理日志
|
||||
logging:
|
||||
level:
|
||||
org.springframework.jdbc.support.JdbcTransactionManager: debug
|
||||
|
||||
|
||||
#定义对象/map集合
|
||||
|
||||
Reference in New Issue
Block a user