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;
}
相关推荐
剑锋所指,所向披靡!9 分钟前
MySQL数据的增删改查
java·数据库·mysql
Villiam_AY13 分钟前
一次 DNS 端口引发的代理网络和公司内网冲突问题
java·服务器·数据库
dgvri20 分钟前
比较Spring AOP和AspectJ
java
eggwyw23 分钟前
springboot和springframework版本依赖关系
java·spring boot·后端
韩立学长25 分钟前
Springboot奶茶加盟信息管理系统m307m786(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
开longlong了吗?1 小时前
Luan Takeaway——大模型驱动的智能外卖管理系统( Spring Cloud、Langchain4j )
后端·spring·spring cloud·langchain
于先生吖2 小时前
国际版JAVA婚恋交友系统源码:多语言适配,可商用的跨境婚恋解决方案
java·大数据·交友
开longlong了吗?2 小时前
Luan Takeaway System:基于Spring Boot + Spring Cloud的外卖业务系统
spring boot·后端·spring cloud
fengci.2 小时前
ctfshow(web入门)279-286
java·开发语言·学习