公共字段自动填充

This commit is contained in:
bunny 2024-01-06 21:08:50 +08:00
parent 364bdecd8d
commit f3ff338a23
6 changed files with 102 additions and 26 deletions

View File

@ -0,0 +1,18 @@
package com.sky.annotation;
import com.sky.enumeration.OperationType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 自定义注解用于表示某个方法需要进行字段自动填充处理
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
// 数据库操作类型UPDATE INSERT
OperationType value();
}

View File

@ -0,0 +1,73 @@
package com.sky.aspect;
import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.LocalTime;
// 自定义切面实现公共字段自动填充
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
public void autoFillPointCut() {
}
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint) {
log.info("开始进行自动填充");
// 获取当前被拦截数据库操作
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
OperationType operationType = autoFill.value();
// 获取实体对象
Object[] args = joinPoint.getArgs();
if (args == null || args.length == 0) {
return;
}
Object entity = args[0];
// 准备赋值数据
LocalDateTime localDateTime = LocalDateTime.now();
Long id = BaseContext.getCurrentId();
// 根据当前不同的操作类型为对应属性来反射赋值
if (operationType == OperationType.INSERT) {
try {
Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
Method setUpdateTime = entity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = entity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setCreateTime.invoke(entity, localDateTime);
setCreateUser.invoke(entity, id);
setUpdateTime.invoke(entity, localDateTime);
setUpdateUser.invoke(entity, id);
} catch (Exception e) {
e.printStackTrace();
}
} else if (operationType == OperationType.UPDATE) {
try {
Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setUpdateTime.invoke(entity, localDateTime);
setUpdateUser.invoke(entity, id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -1,6 +1,7 @@
package com.sky.mapper;
import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.enumeration.OperationType;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
@ -16,6 +17,7 @@ public interface CategoryMapper {
* 插入数据
* @param category Category
*/
@AutoFill(value = OperationType.INSERT)
void insert(Category category);
/**
@ -35,6 +37,7 @@ public interface CategoryMapper {
* 根据id修改分类
* @param category Category
*/
@AutoFill(value = OperationType.UPDATE)
void update(Category category);
/**

View File

@ -1,8 +1,10 @@
package com.sky.mapper;
import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@ -23,6 +25,7 @@ public interface EmployeeMapper {
*
* @param employee 员工
*/
@AutoFill(value = OperationType.INSERT)
void insert(Employee employee);
/**
@ -38,6 +41,7 @@ public interface EmployeeMapper {
*
* @param employee Employee
*/
@AutoFill(value = OperationType.UPDATE)
void update(Employee employee);
/**

View File

@ -48,10 +48,10 @@ public class CategoryServiceImpl implements CategoryService {
category.setStatus(StatusConstant.DISABLE);
//设置创建时间修改时间创建人修改人
category.setCreateTime(LocalDateTime.now());
category.setUpdateTime(LocalDateTime.now());
category.setCreateUser(BaseContext.getCurrentId());
category.setUpdateUser(BaseContext.getCurrentId());
// category.setCreateTime(LocalDateTime.now());
// category.setUpdateTime(LocalDateTime.now());
// category.setCreateUser(BaseContext.getCurrentId());
// category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.insert(category);
}
@ -99,10 +99,6 @@ public class CategoryServiceImpl implements CategoryService {
Category category = new Category();
BeanUtils.copyProperties(categoryDTO,category);
//设置修改时间修改人
category.setUpdateTime(LocalDateTime.now());
category.setUpdateUser(BaseContext.getCurrentId());
categoryMapper.update(category);
}
@ -115,8 +111,6 @@ public class CategoryServiceImpl implements CategoryService {
Category category = Category.builder()
.id(id)
.status(status)
.updateTime(LocalDateTime.now())
.updateUser(BaseContext.getCurrentId())
.build();
categoryMapper.update(category);
}

View File

@ -85,14 +85,6 @@ public class EmployeeServiceImpl implements EmployeeService {
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
employee.setIdNumber(employeeDTO.getIdNumber());
// 设置当前时间
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
// 设置当前记录创建人id
employee.setCreateUser(BaseContext.getCurrentId());
employee.setUpdateUser(BaseContext.getCurrentId());
employeeMapper.insert(employee);
BaseContext.removeCurrentId();
}
@ -120,11 +112,6 @@ public class EmployeeServiceImpl implements EmployeeService {
*/
@Override
public void startOrStop(Integer status, Long id) {
// 常见对象第一种方式
// Employee employee = new Employee();
// employee.setStatus(status);
// employee.setId(id);
// 创建对象第二种方式
Employee employee = Employee.builder().status(status).id(id).build();
employeeMapper.update(employee);
@ -151,9 +138,6 @@ public class EmployeeServiceImpl implements EmployeeService {
Employee employee = new Employee();
BeanUtils.copyProperties(employeeDTO, employee);
employee.setUpdateTime(LocalDateTime.now());
employee.setUpdateUser(BaseContext.getCurrentId());
employeeMapper.update(employee);
}
}