2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Durch die Verwendung der folgenden Anmerkung zur Klasse
@TableField(fill = FieldFill.INSERT) ist eine Annotation in MyBatis-Plus, die zum automatischen Ausfüllen von Feldwerten verwendet wird. MyBatis-Plus ist ein erweitertes Tool, das auf MyBatis basiert, daher sind diese Anmerkungen und Funktionen Funktionen von MyBatis-Plus.
In MyBatis-Plus können Sie die Annotation @TableField(fill = FieldFill.INSERT) verwenden, um Feldwerte automatisch auszufüllen, z. B. um beim Einfügen von Daten automatisch die Erstellungszeit oder Aktualisierungszeit festzulegen. Um diese Funktion nutzen zu können, müssen Sie einen Prozessor zum Ausfüllen dieser Felder definieren.
package com.cky.pojo;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 分类
*/
@Data
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//类型 1 菜品分类 2 套餐分类
private Integer type;
//分类名称
private String name;
//顺序
private Integer sort;
//创建时间
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
//更新时间
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
//创建人
@TableField(fill = FieldFill.INSERT)
private Long createUser;
//修改人
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否删除
@TableField(select = false)
private Integer isDeleted;
}
Definieren Sie dann einen Metadatenprozessor
package com.cky.common;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* @ClassName MyMetaObjectHandler
* @Description TODO
* @Author lukcy
* @Date 2024/6/25 9:12
* @Version 1.0
*/
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("createUser", MythreadLocal.getCurrendId());
metaObject.setValue("updateUser",MythreadLocal.getCurrendId() );
}
@Override
public void updateFill(MetaObject metaObject) {
long id = Thread.currentThread().getId();
log.info("当前线程id{}",id);
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("updateUser",MythreadLocal.getCurrendId());
}
}
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;
/**
* @ClassName AutoFilled
* @Description 自定义填充注解
* @Author lukcy
* @Date 2024/7/7 8:58
* @Version 1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFilled {
OperationType value();
}
package com.sky.aspect;
import com.sky.annotation.AutoFilled;
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;
/**
* @ClassName AutoFilledAspect
* @Description 自定义填充切面类
* @Author lukcy
* @Date 2024/7/7 8:59
* @Version 1.0
*/
@Component
@Slf4j
@Aspect
public class AutoFilledAspect {
//切入点 定义了只有在哪些包下 以及有哪些注解才其效果
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFilled)")
public void AutoFilledPointCut(){}
//通知 前置通知 因为要在执行之前给字段赋值
@Before("AutoFilledPointCut()")
public void autoFilled(JoinPoint joinPoint){
log.info("前置通知....");
//获取方法上的注解确定是insert还是update
MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获得签名
AutoFilled annotation = signature.getMethod().getAnnotation(AutoFilled.class); //获得注解
OperationType operationType = annotation.value();
//获得参数 获得实体
Object[] args = joinPoint.getArgs();
if(args.length==0 || args==null){
return;
}
Object entity = args[0];//约定第一个参数为实体
//获取要赋值的内容
LocalDateTime now = LocalDateTime.now();
Long currentId = BaseContext.getCurrentId();
//通过反射给属性赋值
if(operationType==OperationType.INSERT){
//赋值4个
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(operationType==OperationType.UPDATE){
Method setUpdateTime = null;
try {
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();
}
}
}
}
Zum Beispiel:
package com.sky.mapper;
import com.github.pagehelper.Page;
import com.sky.annotation.AutoFilled;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
/**
* @ClassName CategoryMapper
* @Description TODO
* @Author lukcy
* @Date 2024/7/6 11:06
* @Version 1.0
*/
@Mapper
public interface CategoryMapper {
@AutoFilled(value = OperationType.INSERT)
void insert(Category category);
Page<Category> page(CategoryPageQueryDTO categoryPageQueryDTO);
@AutoFilled(value = OperationType.UPDATE)
void editstatus(Category category);
/**
* 根据id删除分类
* @param id
*/
@Delete("delete from category where id = #{id}")
void deletebyId(Long id);
}