使用AOP切面编程,实现功能增强,完成公共字段自动填充功能,公共字段eg:createTime、createUser、updateTime、updateUser
1.创建自定义注解,用于标记需要进行公共字段填充的方法
2.创建切面类,实现公共字段自动填充业务逻辑
3.给Mapper层的接口方法加上自定义注解,表示为切入点
/**
* 自定义注解,用于标识需要进行公共字段注入的方法
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//OperationType:数据库操作类型,UPDATE,INSERT
OperationType value();
}
/**
* 数据库操作类型
*/
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}
/**
* 自定义切面,用于实现公共字段自动注入处理逻辑
*/
@Aspect
@Slf4j
@Component
public class AutoFillAspect {
/**
* 切入点
*/
@Pointcut("@annotation(com.sky.annotation.AutoFill)&&execution(* com.sky.mapper.*.*(..))")
public void autoFillPointCut() {
}
/**
* 前置通知,在通知中进行公共字段赋值
*
* @param joinPoint
*/
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint) {
log.info("开始进行公共字段自动注入...");
//获取方法签名
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//获取方法上的自定义注解
AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);
//获取数据库操作类型
OperationType operationType = autoFill.value();
//获取当前被拦截方法的参数--实体对象
Object[] args = joinPoint.getArgs();
if (args.length == 0 || args == null) {
return;
}
Object obj = args[0];
//准备注入数据
LocalDateTime now = LocalDateTime.now();
Long currentId = BaseContext.getCurrentId();
if (operationType == OperationType.INSERT) {
try {
//获取赋值方法
Method setCreateTime = obj.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
Method setUpdateTime = obj.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setCreateUser = obj.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
Method setUpdateUser = obj.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
//注入
setUpdateTime.invoke(obj, now);
setCreateTime.invoke(obj, now);
setCreateUser.invoke(obj, currentId);
setUpdateUser.invoke(obj, currentId);
} catch (Exception e) {
e.printStackTrace();
}
} else if (operationType == OperationType.UPDATE) {
try {
//获取赋值方法
Method setUpdateTime = obj.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = obj.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
//注入
setUpdateTime.invoke(obj, now);
setUpdateUser.invoke(obj, currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Mapper
public interface CategoryMapper {
/**
* 插入数据
* @param category
*/
@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);
/**
* 根据id修改分类
* @param category
*/
@AutoFill(value = OperationType.UPDATE)
void update(Category category);
}
sky:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
host: localhost
port: 3306
database: sky_take_out
username: root
password: root
alioss:
endpoint: oss-cn-hangzhou.aliyuncs.com
accessKeyId: ************************
accessKeySecret: ************************
bucketName: web-tilas
spring:
profiles:
active: dev
sky:
#自定义阿里云OSS配置信息
alioss:
endpoint: ${sky.alioss.endpoint}
access-key-id: ${sky.alioss.access-key-id}
access-key-secret: ${sky.alioss.access-key-secret}
bucket-name: ${sky.alioss.bucket-name}
向菜品口味表中批量插入多条口味数据
/**
* 批量添加口味
* @param flavors
*/
void insertBatch(List<DishFlavor> flavors);
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.DishFlavorMapper">
<insert id="insertBatch">
insert into dish_flavor(dish_id, name, value)
values
<foreach collection="flavors" item="flavor" separator=",">
(#{flavor.dishId},#{flavor.name},#{flavor.value})
</foreach>
</insert>
</mapper>