MybatisPlus——常用注解

MybatisPlus------常用注解

MybatisPlus通过扫描实体类,并基于反射获取实体类信息作为数据库表信息

BaseMapper后的指向的是User实体类

java 复制代码
package com.example.mybatisplus02.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus02.entity.User;

public interface UserMapper extends BaseMapper<User> {
}

对应到表的表的结构遵循一下规则:

  1. 类名驼峰转下划线座位表名
  2. 名为id的字段作为主键
  3. 变量名驼峰转下划线作为表的字段名
java 复制代码
package com.example.mybatisplus02.entity;

import lombok.Data;

@Data
public class User {
    private Integer userId;
    private String userName;
    private String password;
}

MybatisPlus------常用注解

  • @TableName:用来指定表名
  • @TableId:用来指定表中的主键字段信息
  • @TableField:用来指定表中普通字段信息

1、如果表的名称与实体类的名称不符,需要用到@TableName注解

java 复制代码
@Data
@TableName("t_user")
public class User {
    private Integer id;
    private String userName;
    private String password;
}

与数据库中的t_user表对应

2、定义主键id,在实体类中建议都要用注解来定义上id,防止找不到id而报错

value:表示要指向的数据库的表名称

type:IdType:

AUTO:数据库自增长

INPUT:通过set方法自行输入

ASSIGN_ID:接口IdentifierGenerator的方法nextId来生成ID

默认实现类为:DefaultIdentifierGenerator雪花算法

java 复制代码
@Data
@TableName("t_user")
public class User {
    @TableId(value = "user_id" ,type = IdType.AUTO)
    private Integer id;
    private String userName;
    private String password;
}

3、 如果表的字段与实体类的中不符,需要用到@TableField注解

使用@TableField注解的常见场景

  • 成员变量名与数据库字段名不一致
  • 成员变量名以is开头,而且是布尔值
  • 成员变量名与数据库关键字冲突
  • 成员变量不是数据库字段(@TableField(exist = fales) )
java 复制代码
@Data
@TableName("t_user")
public class User {
    @TableId(value = "user_id" ,type = IdType.AUTO)
    private Integer id;
    private String userName;
    @TableField(value = "password")
    private String passWord;
}
相关推荐
go__Ahead5 分钟前
【Java】线程池源码解析
java·juc
wyhwust14 分钟前
数组----插入一个数到有序数列中
java·数据结构·算法
专注于大数据技术栈18 分钟前
java学习--final
java·开发语言·学习
努力发光的程序员1 小时前
互联网大厂Java面试:从Spring Boot到微服务架构
spring boot·缓存·微服务·消息队列·rabbitmq·spring security·安全框架
天殇凉1 小时前
AC自动机学习笔记
java·笔记·学习
TechTrek1 小时前
Spring Boot 4.0正式发布了
java·spring boot·后端·spring boot 4.0
飞梦工作室2 小时前
企业级 Spring Boot 邮件系统开发指南:从基础到高可用架构设计
java·spring boot·后端
haiyu柠檬2 小时前
在Spring Boot中实现Azure的SSO+VUE3前端配置
java·spring boot·后端
q***72192 小时前
springBoot 和springCloud 版本对应关系
spring boot·后端·spring cloud
百***81272 小时前
【SpringBoot】SpringBoot中分页插件(PageHelper)的使用
java·spring boot·后端