完成公共字段自动填充功能
This commit is contained in:
parent
e9b0915564
commit
ee83d06372
18
sky-server/src/main/java/com/sky/annotation/AutoFill.java
Normal file
18
sky-server/src/main/java/com/sky/annotation/AutoFill.java
Normal 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(value = {ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AutoFill {
|
||||
//数据库操作属性:update\insert
|
||||
OperationType value();
|
||||
}
|
87
sky-server/src/main/java/com/sky/aspect/AutoFillAspect.java
Normal file
87
sky-server/src/main/java/com/sky/aspect/AutoFillAspect.java
Normal file
@ -0,0 +1,87 @@
|
||||
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.Builder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 自定义切面,实现公共字段自动填充
|
||||
*/
|
||||
@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 methodSignature = (MethodSignature) joinPoint.getSignature();//获取方法签名对象
|
||||
AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);//获取方法上的注解对象
|
||||
OperationType operateType = autoFill.value();//获取注解中的操作类型
|
||||
|
||||
//获取被拦截方法的参数列表
|
||||
Object[] args = joinPoint.getArgs();
|
||||
if (args.length == 0 || args[0] == null) {
|
||||
return;
|
||||
}
|
||||
Object entity = args[0];
|
||||
|
||||
//准备赋值的数据
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Long currentId = BaseContext.getCurrentId();
|
||||
|
||||
//根据不同操作类型,为对应的属性通过反射来赋值
|
||||
if (operateType == 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().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
|
||||
Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
|
||||
|
||||
//通过反射为字段赋值
|
||||
setCreateTime.invoke(entity, now);
|
||||
setCreateUser.invoke(entity, currentId);
|
||||
setUpdateTime.invoke(entity, now);
|
||||
setUpdateUser.invoke(entity, currentId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (operateType == 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, now);
|
||||
setUpdateUser.invoke(entity, currentId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
@ -19,6 +20,7 @@ public interface CategoryMapper {
|
||||
@Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
|
||||
" VALUES" +
|
||||
" (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
|
||||
@AutoFill(value = OperationType.INSERT)
|
||||
void insert(Category category);
|
||||
|
||||
/**
|
||||
@ -39,6 +41,7 @@ public interface CategoryMapper {
|
||||
* 根据id修改分类
|
||||
* @param category
|
||||
*/
|
||||
@AutoFill(value = OperationType.UPDATE)
|
||||
void update(Category category);
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
@ -26,6 +28,7 @@ public interface EmployeeMapper {
|
||||
"create_time, update_time, create_user, update_user) " +
|
||||
"VALUES (#{name}, #{username}, #{password}, #{phone}, #{sex}, #{idNumber}, " +
|
||||
"#{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
|
||||
@AutoFill(value = OperationType.INSERT)
|
||||
void insert(Employee employee);
|
||||
|
||||
|
||||
@ -40,6 +43,7 @@ public interface EmployeeMapper {
|
||||
* 根据id动态更新员工
|
||||
* @param employee
|
||||
*/
|
||||
@AutoFill(value = OperationType.UPDATE)
|
||||
void update(Employee employee);
|
||||
|
||||
/**
|
||||
|
@ -47,11 +47,11 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
//分类状态默认为禁用状态0
|
||||
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,9 +99,9 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
Category category = new Category();
|
||||
BeanUtils.copyProperties(categoryDTO,category);
|
||||
|
||||
//设置修改时间、修改人
|
||||
category.setUpdateTime(LocalDateTime.now());
|
||||
category.setUpdateUser(BaseContext.getCurrentId());
|
||||
// //设置修改时间、修改人
|
||||
// category.setUpdateTime(LocalDateTime.now());
|
||||
// category.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
categoryMapper.update(category);
|
||||
}
|
||||
@ -115,8 +115,8 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
Category category = Category.builder()
|
||||
.id(id)
|
||||
.status(status)
|
||||
.updateTime(LocalDateTime.now())
|
||||
.updateUser(BaseContext.getCurrentId())
|
||||
// .updateTime(LocalDateTime.now())
|
||||
// .updateUser(BaseContext.getCurrentId())
|
||||
.build();
|
||||
categoryMapper.update(category);
|
||||
}
|
||||
|
@ -90,13 +90,13 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
//设置默认密码
|
||||
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
|
||||
|
||||
//设置创建时间和修改时间
|
||||
employee.setCreateTime(LocalDateTime.now());
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
//设置当前记录创建人和修改人id
|
||||
employee.setCreateUser(BaseContext.getCurrentId());
|
||||
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
// //设置创建时间和修改时间
|
||||
// employee.setCreateTime(LocalDateTime.now());
|
||||
// employee.setUpdateTime(LocalDateTime.now());
|
||||
//
|
||||
// //设置当前记录创建人和修改人id
|
||||
// employee.setCreateUser(BaseContext.getCurrentId());
|
||||
// employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
employeeMapper.insert(employee);
|
||||
}
|
||||
@ -152,11 +152,11 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
//对象属性拷贝
|
||||
BeanUtils.copyProperties(employeeDTO, employee);
|
||||
|
||||
//设置修改时间
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
//设置当前记录修改人id
|
||||
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
// //设置修改时间
|
||||
// employee.setUpdateTime(LocalDateTime.now());
|
||||
//
|
||||
// //设置当前记录修改人id
|
||||
// employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
employeeMapper.update(employee);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user