目录
一、事务管理
1.1 事务回顾
1.2 Spring事务管理
1.2.1 案例
1.2.1.1 EmpMapper新增deleteByDeptId方法
1.2.1.2 DeptServiceImpl
1.2.1.3 启动服务-测试
1.2.2 模拟异常
1.2.3 分析问题
1.2.4 Spring事务管理(一般用在类似多次delete)
1.2.4.1 开启事务开关application.yml
1.2.4.2 DeptServiceImpl
1.2.4.3 启动服务-测试
1.3 事务进阶
1.3.1 rollbackFor 回滚
1.3.2 propagation 传播行为
1.3.2 案例
1.3.2.1 创建部门日志表dept_log
1.3.2.2 新建实体类DeptLog
1.3.2.3 DeptLogService接口
1.3.2.4 DeptLogServiceImpl实现类
1.3.2.5 DeptLogMapper
1.3.2.6 DeptServiceImpl
1.3.2.7 启动服务-测试
1.3.3 总结
1.4 扩展插件Grep Console
二、AOP基础
2.1 AOP概述
2.2 AOP 快速入门
2.2.1 AOP依赖
2.2.2 TimeAspect 类
2.2.3 启动服务-测试
2.2.4 AOP场景&优势
2.3 核心概念
2.4 AOP 执行流程
2.5 总结
三、AOP 进阶
3.1 通知类型
3.2 通知顺序
3.3 切入表达式
3.3.1 execution表达式
3.3.2 annotation表达式
3.3.3 总结
3.4 连接点
四、AOP案例-记录操作日志
4.1 思路
4.2 配置起步依赖AOP
4.3 创建数据库表operate_log
4.4 实体类OperateLog
4.5 接口OperateLogMapper
4.6 新建一个自定义的Log注解
4.7 定义一个切面类LogAspect
4.8 Deptcontroller 为新增、修改、删除添加@Log
4.9 EmpController 为新增、修改、删除添加@Log
4.10 启动服务-测试
前言:Spring 事务管理是 Spring Framework 提供的一种方便的事务管理方式,它主要解决了应用程序中多数据源跨库操作时的事务管理问题。Spring 事务管理的原理是使用代理模式,为目标对象动态地创建代理对象,代理对象拦截目标对象的方法,实现事务的开启、提交和回滚等操作。在 Spring 中,使用 @Transactional 注解来标记需要进行事务管理的方法,通过配置数据源和事务管理器,Spring 可以自动为这些方法创建事务并进行管理。在配置事务时,可以指定事务的隔离级别、传播行为、只读属性等选项,以满足不同的业务需求。Spring 事务管理的好处是简化了事务管理的流程,减少了代码的重复性,提高了代码的可读性和可维护性,并且在多线程环境下可以保证数据的一致性。
package com.runa.mapper;
import com.runa.pojo.Emp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理
*/
@Mapper
public interface EmpMapper {
/**
* 查询总记录数
* @return
*/
// @Select("select count(*) from emp")
// public Long count();
/**
* 分页查询 获取列表数据
* @param start
* @param pageSize
* @return
*/
// @Select("select * from emp limit #{start}, #{pageSize}")
// public List page(Integer start, Integer pageSize);
/**
* 使用pagehelper的员工信息查询
* @return
*/
// @Select("select * from emp")
// public List list();
/**
* 使用pagehelper的员工信息查询(带条件)--动态sql
* 使用xml注解sql
* @return
*/
public List list(String name, Short gender, LocalDate begin, LocalDate end);
/**
* 批量删除员工
* @param ids
*/
void delete(List ids);
/**
* 新增员工
* @param emp
*/
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
" values(#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
void insert(Emp emp);
/**
* 根据id查询员工
* @param id
* @return
*/
@Select("select * from emp where id = #{id}")
Emp getByID(Integer id);
/**
* 修改员工
* @param emp
*/
void update(Emp emp);
/**
* 登录
* @param emp
* @return
*/
@Select("select * from emp where username = #{username} and password = #{password}")
Emp getByUsernameAndPassword(Emp emp);
/**
* 根据部门dept_id删除该部门下的员工数据
*/
@Select("delete from emp where dept_id = #{deptId}")
void deleteByDeptId(Integer deptId);
}
package com.runa.service.impl;
import com.runa.mapper.DeptMapper;
import com.runa.mapper.EmpMapper;
import com.runa.pojo.Dept;
import com.runa.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Autowired
private EmpMapper empMapper;
/**
* 查询部门
* @return
*/
@Override
public List list() {
return deptMapper.list();
}
/**
* 根据ID删除部门
* @param id
*/
@Override
public void delete(Integer id) {
// 1 根据ID 删除部门
deptMapper.deleteById(id);
//2 根据部门id删除该部门下的员工
empMapper.deleteByDeptId(id);
}
/**
* 新增部门
* @param dept
*/
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
/**
* 根据id查询部门
* @param id
* @return
*/
@Override
public Dept getById(Integer id) {
return deptMapper.getById(id);
}
/**
* 编辑部门
* @param dept
*/
@Override
public void update(Dept dept) {
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
#spring事务管理日志
logging:
level:
org.springframework.jdbc.support.JdbcTransactionManager: debug
package com.runa.service.impl;
import com.runa.mapper.DeptMapper;
import com.runa.mapper.EmpMapper;
import com.runa.pojo.Dept;
import com.runa.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
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Autowired
private EmpMapper empMapper;
/**
* 查询部门
* @return
*/
@Override
public List list() {
return deptMapper.list();
}
/**
* 根据ID删除部门
* @param id
*/
@Transactional //spring 事务管理
@Override
public void delete(Integer id) {
// 1 根据ID 删除部门
deptMapper.deleteById(id);
//2 根据部门id删除该部门下的员工
empMapper.deleteByDeptId(id);
}
/**
* 新增部门
* @param dept
*/
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
/**
* 根据id查询部门
* @param id
* @return
*/
@Override
public Dept getById(Integer id) {
return deptMapper.getById(id);
}
/**
* 编辑部门
* @param dept
*/
@Override
public void update(Dept dept) {
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
package com.runa.service.impl;
import com.runa.mapper.DeptMapper;
import com.runa.mapper.EmpMapper;
import com.runa.pojo.Dept;
import com.runa.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
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Autowired
private EmpMapper empMapper;
/**
* 查询部门
* @return
*/
@Override
public List list() {
return deptMapper.list();
}
/**
* 根据ID删除部门
* @param id
*/
@Transactional(rollbackFor = Exception.class) //spring 事务管理 .所有异常回滚:rollbackFor = Exception.class
@Override
public void delete(Integer id) {
// 1 根据ID 删除部门
deptMapper.deleteById(id);
//2 根据部门id删除该部门下的员工
empMapper.deleteByDeptId(id);
}
/**
* 新增部门
* @param dept
*/
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
/**
* 根据id查询部门
* @param id
* @return
*/
@Override
public Dept getById(Integer id) {
return deptMapper.getById(id);
}
/**
* 编辑部门
* @param dept
*/
@Override
public void update(Dept dept) {
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
create table dept_log(
id int auto_increment comment '主键ID' primary key,
create_time datetime null comment '操作时间',
description varchar(300) null comment '操作描述'
)comment '部门操作日志表';
package com.runa.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;
}
package com.runa.service;
import com.runa.pojo.DeptLog;
public interface DeptLogService {
void insert(DeptLog deptLog);
}
package com.runa.service.impl;
import com.runa.mapper.DeptLogMapper;
import com.runa.mapper.DeptLogMapper;
import com.runa.pojo.DeptLog;
import com.runa.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);
}
}
package com.runa.mapper;
import com.runa.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);
}
package com.runa.service.impl;
import com.runa.mapper.DeptLogMapper;
import com.runa.mapper.DeptMapper;
import com.runa.mapper.EmpMapper;
import com.runa.pojo.Dept;
import com.runa.pojo.DeptLog;
import com.runa.service.DeptLogService;
import com.runa.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
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Autowired
private EmpMapper empMapper;
// 没有使用下面的使用上面的是因为事务的原因
// @Autowired
// private DeptLogMapper deptLogMapper;
@Autowired
private DeptLogService deptLogService;
/**
* 查询部门
* @return
*/
@Override
public List list() {
return deptMapper.list();
}
/**
* 根据ID删除部门
* @param id
*/
@Transactional(rollbackFor = Exception.class) //spring 事务管理 .所有异常回滚:rollbackFor = Exception.class
@Override
public void delete(Integer id) {
try {
// 1 根据ID 删除部门
deptMapper.deleteById(id);
// 模拟异常
int i = 1/0;
//2 根据部门id删除该部门下的员工
empMapper.deleteByDeptId(id);
} finally {
DeptLog deptLog = new DeptLog();
deptLog.setCreateTime(LocalDateTime.now());
deptLog.setDescription("执行了解散部门的操作,此次解散的是" + id + "号部门");
deptLogService.insert(deptLog);
//没有使用下面的使用上面的是因为事务的原因
// deptLogMapper.insert(deptLog);
}
}
/**
* 新增部门
* @param dept
*/
@Override
public void add(Dept dept) {
dept.setCreateTime(LocalDateTime.now());
dept.setUpdateTime(LocalDateTime.now());
deptMapper.insert(dept);
}
/**
* 根据id查询部门
* @param id
* @return
*/
@Override
public Dept getById(Integer id) {
return deptMapper.getById(id);
}
/**
* 编辑部门
* @param dept
*/
@Override
public void update(Dept dept) {
dept.setUpdateTime(LocalDateTime.now());
deptMapper.update(dept);
}
}
可以为日志设置颜色
org.springframework.boot
spring-boot-starter-aop
3.1.2
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect //AOP类
public class TimeAspect {
//@Around("execution(* com.runa.service.impl.DeptServiceImpl.*(..))") //切入点表达式
// @Around("com.runa.aop.MyAspect1.pt()")
@Around("execution(* com.runa.service.*.*(..))") //切入点表达式
public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
//1. 记录开始时间
long begin = System.currentTimeMillis();
//2. 调用原始方法运行
Object result = joinPoint.proceed();
//3. 记录结束时间, 计算方法执行耗时
long end = System.currentTimeMillis();
log.info(joinPoint.getSignature()+"方法执行耗时: {}ms", end-begin);
return result;
}
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect //demo,运行项目要注释
public class MyAspect1Delete {
@Pointcut("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void pt(){}
@Before("pt()")
public void before(){
log.info("before ...");
}
@Around("pt()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("around before ...");
//调用目标对象的原始方法执行
Object result = proceedingJoinPoint.proceed();
log.info("around after ...");
return result;
}
@After("pt()")
public void after(){
log.info("after ...");
}
@AfterReturning("pt()")
public void afterReturning(){
log.info("afterReturning ...");
}
@AfterThrowing("pt()")
public void afterThrowing(){
log.info("afterThrowing ...");
}
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(2)
@Slf4j
@Component
@Aspect //为了实验,实际是要注释的
public class MyAspect2Delete {
@Before("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void before(){
log.info("before ...2");
}
@After("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void after(){
log.info("after ...2");
}
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(3)
@Slf4j
@Component
@Aspect //为了实验,实际是要注释的
public class MyAspect3Delete {
@Before("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void before(){
log.info("before ...3");
}
@After("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void after(){
log.info("after ...3");
}
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Slf4j
@Component
@Aspect //为了实验,实际是要注释的
public class MyAspect4Delete {
@Before("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void before(){
log.info("before ...4");
}
@After("execution(* com.runa.service.impl.DeptServiceImpl.*(..))")
public void after(){
log.info("after ...4");
}
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
//切面类
@Slf4j
@Aspect //项目运行注释掉
@Component
public class MyAspect6Delete {
//@Pointcut("execution(public void com.runa.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
//@Pointcut("execution(void com.runa.service.impl.DeptServiceImpl.delete(java.lang.Integer))")
//@Pointcut("execution(void delete(java.lang.Integer))") //包名.类名不建议省略
//@Pointcut("execution(void com.runa.service.DeptService.delete(java.lang.Integer))")
//@Pointcut("execution(void com.runa.service.DeptService.*(java.lang.Integer))")
//@Pointcut("execution(* com.*.service.DeptService.*(*))")
//@Pointcut("execution(* com.runa.service.*Service.delete*(*))")
//@Pointcut("execution(* com.runa.service.DeptService.*(..))")
//@Pointcut("execution(* com..DeptService.*(..))")
//@Pointcut("execution(* com..*.*(..))")
//@Pointcut("execution(* *(..))") //慎用
@Pointcut("execution(* com.runa.service.DeptService.list()) || " +
"execution(* com.runa.service.DeptService.delete(java.lang.Integer))")
private void pt(){}
@Before("pt()")
public void before(){
log.info("MyAspect6 ... before ...");
}
}
package com.runa.aop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) //运行有效
@Target(ElementType.METHOD)
public @interface MyDeleteLog {
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
//切面类
@Slf4j
@Aspect //为了实验,实际是要注释的
@Component
public class MyAspect7Delete {
//匹配DeptServiceImpl中的 list() 和 delete(Integer id)方法
//@Pointcut("execution(* com.runa.service.DeptService.list()) || execution(* com.itheima.service.DeptService.delete(java.lang.Integer))")
@Pointcut("@annotation(com.runa.aop.MyDeleteLog)")
private void pt(){}
@Before("pt()")
public void before(){
log.info("MyAspect7 ... before ...");
}
}
package com.runa.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.util.Arrays;
//切面类
@Slf4j
//@Aspect //为了实验,实际是要注释的
@Component
public class MyAspect8Delete {
@Pointcut("execution(* com.runa.service.DeptService.*(..))")
private void pt(){}
@Before("pt()")
public void before(JoinPoint joinPoint){
log.info("MyAspect8 ... before ...");
}
@Around("pt()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("MyAspect8 around before ...");
//1. 获取 目标对象的类名 .
String className = joinPoint.getTarget().getClass().getName();
log.info("目标对象的类名:{}", className);
//2. 获取 目标方法的方法名 .
String methodName = joinPoint.getSignature().getName();
log.info("目标方法的方法名: {}",methodName);
//3. 获取 目标方法运行时传入的参数 .
Object[] args = joinPoint.getArgs();
log.info("目标方法运行时传入的参数: {}", Arrays.toString(args));
//4. 放行 目标方法执行 .
Object result = joinPoint.proceed();
//5. 获取 目标方法运行的返回值 .
log.info("目标方法运行的返回值: {}",result);
log.info("MyAspect8 around after ...");
return result;
}
}
略
-- 操作日志表
create table operate_log(
id int unsigned primary key auto_increment comment 'ID',
operate_user int unsigned comment '操作人ID',
operate_time datetime comment '操作时间',
class_name varchar(100) comment '操作的类名',
method_name varchar(100) comment '操作的方法名',
method_params varchar(1000) comment '方法参数',
return_value varchar(2000) comment '返回值',
cost_time bigint comment '方法执行耗时, 单位:ms'
) comment '操作日志表';
package com.runa.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OperateLog {
private Integer id; //ID
private Integer operateUser; //操作人ID
private LocalDateTime operateTime; //操作时间
private String className; //操作类名
private String methodName; //操作方法名
private String methodParams; //操作方法参数
private String returnValue; //操作方法返回值
private Long costTime; //操作耗时
}
package com.runa.mapper;
import com.runa.pojo.OperateLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface OperateLogMapper {
//插入日志数据
@Insert("insert into operate_log (operate_user, operate_time, class_name, method_name, method_params, return_value, cost_time) " +
"values (#{operateUser}, #{operateTime}, #{className}, #{methodName}, #{methodParams}, #{returnValue}, #{costTime});")
public void insert(OperateLog log);
}
package com.runa.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) //运行时生效
@Target(ElementType.METHOD) //作用在方法上
public @interface Log {
}
package com.runa.aop;
import com.alibaba.fastjson.JSONObject;
import com.runa.mapper.OperateLogMapper;
import com.runa.pojo.OperateLog;
import com.runa.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;
@Slf4j
@Component
@Aspect //切面类
public class LogAspect {
@Autowired
private HttpServletRequest request;
@Autowired //注入
private OperateLogMapper operateLogMapper;
@Around("@annotation(com.runa.anno.Log)") //环绕通知
public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {
// 操作人ID - 当前登录员工ID
// 获取请求头中的jwt令牌,解析令牌
String jwt = request.getHeader("token");
Claims claims = JwtUtils.parseJWT(jwt);
Integer operateUser = (Integer) claims.get("id");
// 操作时间
LocalDateTime operateTime = LocalDateTime.now();
// 操作类名
String className = joinPoint.getTarget().getClass().getName();
// 操作方法名
String methodName = joinPoint.getSignature().getName();
// 操作方法参数
Object[] args = joinPoint.getArgs();
String methodParams = Arrays.toString(args);
long begin = System.currentTimeMillis();
//调用原始目标方法运行
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
// 方法返回值
String returnValue = JSONObject.toJSONString(result);
// 操作耗时
Long costTime = end - begin;
// 记录操作日志
OperateLog operateLog = new OperateLog(null, operateUser, operateTime, className, methodName, methodParams, returnValue, costTime);
operateLogMapper.insert(operateLog);
log.info("AOP 记录操作日志:{}",operateLog);
return result;
}
}
package com.runa.controller;
import com.runa.anno.Log;
import com.runa.pojo.Dept;
import com.runa.pojo.Result;
import com.runa.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 部门管理Controller
*/
@Slf4j
@RestController
@RequestMapping("/depts")
public class Deptcontroller {
@Autowired
private DeptService deptService;
/**
* 查询部门数据
* @return
*/
// private static Logger log = LoggerFactory.getLogger(Deptcontroller.class);
// @RequestMapping(value = "/depts", method = RequestMethod.GET) //指定请求方式为GET
@GetMapping //
public Result list(){
log.info("查询全部部门数据");
// 调用 service查询部门数据
List deptList = deptService.list();
return Result.success(deptList);
}
/**
* 删除部门
* @return
*/
@Log
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id){
log.info("根据id删除部门:{}", id);
// 调用service删除部门
deptService.delete(id);
return Result.success();
}
/**
* 添加部门
* @return
*/
@Log
@PostMapping
public Result add(@RequestBody Dept dept){
log.info("新增部门:{}",dept);
// // 调用service新增部门
deptService.add(dept);
return Result.success();
}
/**
* 根据ID查询部门
* @return
*/
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
log.info("根据id查询部门:{}", id);
// 调用service查询部门
Dept dept = deptService.getById(id);
return Result.success(dept);
}
/**
* 编辑部门
* @return
*/
@Log
@PutMapping
public Result update(@RequestBody Dept dept){
log.info("编辑部门:{}==={}", dept.getId(),dept.getName());
// 调用service编辑部门
deptService.update(dept);
return Result.success();
}
}
package com.runa.controller;
import com.runa.anno.Log;
import com.runa.pojo.Emp;
import com.runa.pojo.PageBean;
import com.runa.pojo.Result;
import com.runa.service.EmpService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
/**
* 员工管理Controller
*/
@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
@Autowired
private EmpService empService;
/**
* 员工数据查询列表 , 不带条件
* @param page
* @param pageSize
* @return
*/
// @GetMapping
// public Result page(@RequestParam(defaultValue = "1") Integer page,
// @RequestParam(defaultValue = "10") Integer pageSize){
// log.info("分页查询,参数:开始页 {}, 每页显示:{}", page, pageSize);
// // 调用Service方法进行查询
// PageBean pageBean = empService.page(page,pageSize);
//
// return Result.success(pageBean);
// }
/**
* 员工数据查询列表 , 带条件组合查询
* @param page
* @param pageSize
* @param name
* @param gender
* @param begin
* @param end
* @return
*/
@GetMapping
public Result page(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,
String name, Short gender,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("分页查询,参数:开始页 {}, 每页显示:{},姓名:{},性别:{}, 开始日期:{},结束日期:{}", page, pageSize, name, gender, begin, end);
// 调用Service方法进行查询
PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
return Result.success(pageBean);
}
/**
* 批量删除员工
* @param ids
* @return
*/
@Log
@DeleteMapping("/{ids}")
public Result delete(@PathVariable List ids){
log.info("批量删除的操作,删除对象为:{}",ids);
empService.delete(ids);
return Result.success();
}
/**
* 新增员工
* @param emp
* @return
*/
@Log
@PostMapping
public Result save(@RequestBody Emp emp){
log.info("新增员工emp: {}",emp);
empService.save(emp);
return Result.success();
}
/**
* 根据id查询员工
* @param id
* @return
*/
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
log.info("根据ID查询员工信息:{}",id);
Emp emp = empService.getById(id);
return Result.success(emp);
}
/**
* 修改员工
* @param emp
* @return
*/
@Log
@PutMapping
public Result update(@RequestBody Emp emp){
log.info("更新员工: {}",emp);
empService.update(emp);
return Result.success();
}
}