Mybatisplus 常用注解

一、Mybatisplus 注解

@TableName 

表名注解,标识实体类对应的表

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface TableName {
    // 表名
    String value() default "";

    // schema
    String schema() default "";

    // 是否保持使用全局的 tablePrefix 的值(当全局 tablePrefix 生效时)
    boolean keepGlobalPrefix() default false;
    
    // xml 中 resultMap 的 id(用于满足特定类型的实体类对象绑定)
    String resultMap() default "";
    
    // 是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建与注入)
    boolean autoResultMap() default false;
    
    // 需要排除的属性名
    String[] excludeProperty() default {};
}

@TableId

主键注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface TableId {
    // 主键字段名
    String value() default "";
    
    // 指定主键类型
    IdType type() default IdType.NONE;
}

public enum IdType {
    // AUTO	数据库 ID 自增
    AUTO(0),

    // NONE	无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
    NONE(1),

    // INPUT insert 前自行 set 主键值
    INPUT(2),

    // ASSIGN_ID 分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
    ASSIGN_ID(3),

    // ASSIGN_UUID 分配 UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认 default 方法)
    ASSIGN_UUID(4);

    private final int key;

    private IdType(int key) {
        this.key = key;
    }

    public int getKey() {
        return this.key;
    }
}

@TableField 

字段注解(非主键)

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface TableField {
    // 数据库字段名
    String value() default "";
    
    // 是否为数据库表字段
    boolean exist() default true;

    // 字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s}    
    String condition() default "";

    // 字段 update set 部分注入,例如:当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性)
    String update() default "";

    // FieldStrategy
    FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
    
    // FieldStrategy
    FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;

    // FieldStrategy
    FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;

    // 字段自动填充策略
    FieldFill fill() default FieldFill.DEFAULT;

    // 是否进行 select 查询
    boolean select() default true;

    // 是否保持使用全局的 format 进行处理
    boolean keepGlobalFormat() default false;

    
    String property() default "";

    // JDBC 类型 (该默认值不代表会按照该值生效)
    JdbcType jdbcType() default JdbcType.UNDEFINED;

    // 类型处理器 (该默认值不代表会按照该值生效)
    Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;

   
    boolean javaType() default false;

    // 指定小数点后保留的位数
    String numericScale() default "";
}

public enum FieldStrategy {
    // 忽略判断
    IGNORED,
    // 非 NULL 判断
    NOT_NULL,
    // 非空判断(只对字符串类型字段,其他类型字段依然为非 NULL 判断)
    NOT_EMPTY,
    // 追随全局配置
    DEFAULT,
    // 不加入SQL
    NEVER;

    private FieldStrategy() {
    }
}


public enum FieldFill {
    // 默认不处理
    DEFAULT,
    // 插入时填充字段
    INSERT,
    // 更新时填充字段
    UPDATE,
    // 插入和更新时填充字段
    INSERT_UPDATE;

    private FieldFill() {
    }
}

@Version

乐观锁注解、标记 @Version 在字段上

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface Version {
}

@EnumValue

普通枚举类注解(注解在枚举字段上)

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface EnumValue {
}

@TableLogic

表字段逻辑处理注解(逻辑删除)

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface TableLogic {
    // 逻辑未删除值
    String value() default "";
    // 逻辑删除值
    String delval() default "";
}

@OrderBy

内置 SQL 默认指定排序,优先级低于 wrapper 条件查询

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface OrderBy {
    // 是否倒序查询
    boolean asc() default false;

    /** @deprecated */
    @Deprecated
    boolean isDesc() default true;
    
    // 数字越小越靠前
    short sort() default 32767;
}

二、Mybatits注解
 

@Select

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Repeatable(Select.List.class)
public @interface Select {
    String[] value();

    String databaseId() default "";

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface List {
        Select[] value();
    }
}

@Delete

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Repeatable(Delete.List.class)
public @interface Delete {
    String[] value();

    String databaseId() default "";

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface List {
        Delete[] value();
    }
}

@Insert

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Repeatable(Insert.List.class)
public @interface Insert {
    String[] value();

    String databaseId() default "";

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface List {
        Insert[] value();
    }
}

@Update

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Repeatable(Update.List.class)
public @interface Update {
    String[] value();

    String databaseId() default "";

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface List {
        Update[] value();
    }
}

@Mapper

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface Mapper {
}


MyBatis-Plus 官网地址

MyBatis中文网

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值