MyBatis Plus常用注解

本文详细介绍了MyBatisPlus中的关键注解,如@TableId、@TableField、@Version、@TableLogic等,以及它们在数据库映射、主键生成策略、字段填充、乐观锁和逻辑删除等方面的应用。同时,展示了如何配置和测试这些功能,帮助开发者更好地理解和使用MyBatisPlus进行数据操作。
摘要由CSDN通过智能技术生成

@TableName

映射数据库的表名

@Data
// 映射数据库的表名
@TableName(value = "employee")
public class Employee {
    // 设置主键映射
    private Long id;
    private String name;
    private Integer gender;
    private String pwd;
    private String email;
    private String company;
}

@TableId

设置主键映射,value 映射主键字段名,type 设置主键类型,主键的⽣成策略

AUTO(0),
NONE(1),
INPUT(2),
ASSIGN_ID(3),
ASSIGN_UUID(4),
/** @deprecated */
@Deprecated
ID_WORKER(3),
/** @deprecated */
@Deprecated
ID_WORKER_STR(3),
/** @deprecated */
@Deprecated
UUID(4);
描述
AUTO数据库⾃增
NONEMP set 主键,雪花算法实现
INPUT需要开发者⼿动赋值
ASSIGN_IDMP 分配 ID,Long、Integer、String
ASSIGN_UUID分配 UUID,Strinig

INPUT 如果开发者没有⼿动赋值,则数据库通过⾃增的
⽅式给主键赋值,如果开发者⼿动赋值,则存⼊该值。
AUTO 默认就是数据库⾃增,开发者⽆需赋值。
ASSIGN_ID MP ⾃动赋值,雪花算法。
ASSIGN_UUID 主键的数据类型必须是 String,⾃动⽣成 UUID 进⾏赋值

@Data
// 映射数据库的表名
@TableName(value = "employee")
public class Employee {
    // 设置主键映射
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    private String name;
    private Integer gender;
    private String pwd;
    private String email;
    private String company;
}

@TableField

映射⾮主键字段,value 映射字段名
exist 表示是否为数据库字段 false,如果实体类中的成员变量在数据库中没有对应的字段,则可以使⽤ exist,VO、DTO
select 表示是否查询该字段
fill 表示是否⾃动填充,将对象存⼊数据库的时候,由 MyBatis Plus ⾃动给某些字段赋值,create_time、update_time
1、给表添加 create_time、update_time 字段
2、实体类中添加成员变量

@Data
// 映射数据库的表名
@TableName(value = "employee")
public class Employee {
    // 设置主键映射
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @TableField(value = "name")
    private String name;
    private Integer gender;
    // 查询的时候忽略该字段
    @TableField(select = false)
    private String pwd;
    private String email;
    private String company;

    // 成员遍历在数据库中没有对应的字段
    @TableField(exist = false)
    private String profile;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
}

@Version

标记乐观锁,通过 version 字段来保证数据的安全性,当修改数据的时候,会以 version 作为条件,当条件成⽴的时候才会修改成功。
version = 2
线程1:update … set version = 2 where version = 1
线程2:update … set version = 2 where version = 1
1、数据库表添加 version 字段,默认值为 1
2、实体类添加 version 成员变量,并且添加 @Version

@Data
// 映射数据库的表名
@TableName(value = "employee")
public class Employee {
    // 设置主键映射
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @TableField(value = "name")
    private String name;
    private Integer gender;
    // 查询的时候忽略该字段
    @TableField(select = false)
    private String pwd;
    private String email;
    private String company;

    // 成员遍历在数据库中没有对应的字段
    @TableField(exist = false)
    private String profile;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    // 乐观锁
    @Version
    private Integer version;
}

3、注册配置类

@Configuration
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}

@EnumValue

1、通⽤枚举类注解,将数据库字段映射成实体类的枚举类型成员变量

package com.jq.mybatisplus.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

public enum StatusEnum {
    WORK(1, "上班"),
    REST(0, "休息");

    @EnumValue
    private Integer code;
    private String msg;

    StatusEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}
@Data
// 映射数据库的表名
@TableName(value = "employee")
public class Employee {
    // 设置主键映射
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @TableField(value = "name")
    private String name;
    private Integer gender;
    // 查询的时候忽略该字段
    @TableField(select = false)
    private String pwd;
    private String email;
    private String company;

    // 成员遍历在数据库中没有对应的字段
    @TableField(exist = false)
    private String profile;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    // 乐观锁
    @Version
    private Integer version;

    private StatusEnum status;
}

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/qt?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root

#打印日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-enums-package: com.jq.mybatisplus.enums

2、测试接⼝

@SpringBootTest
class EmployeeMapperTest {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void test() {
        employeeMapper.selectList(null).forEach(System.out::println);
    }
}

@TableLogic

映射逻辑删除
1、数据表添加 deleted 字段
2、实体类添加注解

@Data
// 映射数据库的表名
@TableName(value = "employee")
public class Employee {
    // 设置主键映射
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;
    @TableField(value = "name")
    private String name;
    private Integer gender;
    // 查询的时候忽略该字段
    @TableField(select = false)
    private String pwd;
    private String email;
    private String company;

    // 成员遍历在数据库中没有对应的字段
    @TableField(exist = false)
    private String profile;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    // 乐观锁
    @Version
    private Integer version;

    private StatusEnum status;

    // 逻辑删除
    @TableLogic
    private Integer deleted;
}

3、application.yml 添加配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/qt?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root

#打印日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-enums-package: com.jq.mybatisplus.enums
  global-config:
    db-config:
      logic-not-delete-value: 0
      logic-delete-value: 1
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值