- yml配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志
mapper-locations: classpath:mapper/*.xml # 映射
type-enums-package: com.dxc.zeno.enums # 枚举类
- 配置类
@Configuration
@MapperScan("com.dxc.zone.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
// 乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
- 填充器
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.util.Date;
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
this.strictInsertFill(metaObject, "version", Integer.class, 0);
this.setFieldValByName("created",new Date(),metaObject);
this.setFieldValByName("updated",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updated",new Date(),metaObject);
}
}
- 实体类
@Data // 生成getter和setter方法
@AllArgsConstructor // 有参构造器
@NoArgsConstructor // 无参构造器
@ToString // toString方法
@Accessors(chain = true)
@TableName("user") // 指定表名称
public class User implements Serializable {
private static final long serialVersionUID = 1L;
// 标注在id属性上,用于映射数据库中的id
@TableId(value="id")
private int id;
@TableField(value = "created", fill = FieldFill.INSERT)
private Date created;
@TableField(value = "updated", fill = FieldFill.INSERT_UPDATE)
private Date updated;
@TableField(value = "deleted", fill = FieldFill.INSERT)
@TableLogic
private Integer deleted;
@TableField(value = "version", fill = FieldFill.INSERT)
@Version
private Integer version;
// 标注在属性上,用于映射数据库中的属性
@TableField(value="bir")
private Date birthday;
//标注在属性上,表示该属性在数据库中不存在,则不会映射
@TableField(exist = false)
private String userName;
}
- 枚举类
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
@Getter
public enum SexEnum {
MALE(1, "男"),
FEMALE(2, "女");
@EnumValue
private Integer sex;
private String sexName;
SexEnum(Integer sex, String sexName) {
this.sex = sex;
this.sexName = sexName;
}
}
标签:所需,metaObject,TableField,private,Date,plus,mybatis,import,public
From: https://www.cnblogs.com/dogleftover/p/17625539.html