MyBatis-Plus(二、常用注解)
1、@TableName
@TableName
用法:映射数据库的表名,如果数据库表名与实体类名不一致,用@TableName来映射。
package com.example.mybatisplus.entiy;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("mp") //表名
public class User {
private String id;
private String name;
private String age;
}

当数据库表名与实体类名不一致,报错如下:

2、@TableId
@TableId
设置主键映射
1)value
- value 映射主键字段名

2)type
- 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 | 数据库自增 |
| NONE | MP set 主键,雪花算法实现 |
| INPUT | 需要开发者手动赋值 |
| ASSIGN_ID | MP 分配 ID,Long、Integer、String |
| ASSIGN_UUID | 分配 UUID,Strinig |
INPUT 如果开发者没有手动赋值,则数据库通过自增的方式给主键赋值,如果开发者手动赋值,则存入该值。
AUTO 默认就是数据库自增,开发者无需赋值。
ASSIGN_ID MP 自动赋值,雪花算法。
ASSIGN_UUID 主键的数据类型必须是 String ,自动生成 UUID 进行赋值。
例如:

编写测试类
@Test
void save(){
User user = new User();
user.setName("张三");
user.setAge(20);
userMapper.insert(user);
}
Id成功按策略插入成功

3、@TableField
@TableField
映射非主键字段,value 映射字段名
例如:
当字段名与数据库字段名不一致时,报错如下:

正确写法:

1)exist
- exist 表示是否为数据库字段 false,如果实体类中的成员变量在数据库中没有对应的字段,则可以使用 exist,VO、DTO
当添加数据库没有的字段时,报错如下:

加上@TableField(exist = false),表示在数据库中没有对应的字段,即可查询成功。

2)select
- select 表示是否查询该字段

3)fill
- fill 表示是否自动填充,如create_time、update_time
1、增加字段

2、扩充实体类
@TableField(fill = FieldFill.INSERT) //填充字段
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE) //填充字段
private Date updateTime;
3、MetaObjectHandler
创建MyMetaObjectHandler类 implements MetaObjectHandler,重写两个方法。
package com.example.mybatisplus.handle;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", System.currentTimeMillis(), metaObject); // 创建时间 或者new Date()
this.setFieldValByName("updateTime", System.currentTimeMillis(), metaObject); // 更新时间
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", System.currentTimeMillis(), metaObject); // 更新时间
}
}
4、Test

4、@Version
@Version
标记乐观锁,通过 version 字段来保证数据的安全性,当修改数据的时候,会以 version 作为条件,当条件成立的时候才会修改成功。
1、数据库表添加 version 字段,默认值为 1

2、实体类添加 version 成员变量,并且添加 @Version
@Version
private Integer version; //乐观锁
3、配置类
package com.example.mybatisplus.config;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // 声明为配置类
public class MyConfig {
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor(); // 创建一个 OptimisticLockerInterceptor 对象
}
}
测试:version 1-->2

3、@EnumValue
@EnumValue
1)枚举类注解实现
通用枚举类注解,将数据库字段映射成实体类的枚举类型成员变量
1、设计表

2、实体类添加字段
private StatusEnum status; //状态 枚举类型
3、创建枚举类
package com.example.mybatisplus.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
public enum StatusEnum { //枚举类 enum类型的类
//奖励1 惩罚0
reward(1,"奖励"),punishment(0,"惩罚");
@EnumValue
private Integer code;
private String msg;
StatusEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
4、application.yml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志实现类 -->打印sql
type-enums-package:
com.example.mybatisplus.enums # 实现类所在的包 这里
5、测试
查询id为1、2,其对应status分别为1、0

2)接口实现
import com.baomidou.mybatisplus.core.enums.IEnum;
public enum SexEnum implements IEnum<Integer> {
ONE(1,"男"),
TWO(0,"女"),
private Integer code;
private String msg;
AgeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public Integer getValue() {
return this.code;
}
}
6、@TableLogic
@TableLogic
映射逻辑删除
1、数据表添加 identification字段

2、实体类添加注解
@TableLogic
private Integer identification; //是否删除标识
3、application.yml 添加配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志实现类 -->打印sql
type-enums-package:
com.example.mybatisplus.enums # 实现类所在的包
global-config:
db-config:
logic-not-delete-value: 0 # 逻辑删除的值
logic-delete-value: 1 # 逻辑删除的值
4、测试
删除id为2的User,identification标识 0-->1

再次查询数据库,查不到id为2的User,逻辑删除实现。

MyBatis-Plus(二、常用注解)的更多相关文章
- <SpringMvc>入门二 常用注解
1.@RequestMapping @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME ...
- Mybatis 常用注解
Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...
- spring(二、bean生命周期、用到的设计模式、常用注解)
spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...
- SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置
一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...
- spring,spring mvc,mybatis 常用注解
文章来源:https://www.cnblogs.com/hello-tl/p/9209063.html 0.在spring,soring mvc, mybistis 中的常用注解有一下 <! ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- MyBatis笔记二:配置
MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...
- springmvc常用注解与类型转换
springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...
- SpringMVC常用注解,返回方式,路径匹配形式,验证
常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...
随机推荐
- 关于『进击的Markdown』:第四弹
关于『进击的Markdown』:第四弹 建议缩放90%食用 美人鱼(Mermaid)悄悄的来,又悄悄的走,挥一挥匕首,不留一个活口 又是漫漫画图路... 女士们先生们,大家好! 我们要接受Markd ...
- 合宙AIR105(二): 时钟设置和延迟函数
目录 合宙AIR105(一): Keil MDK开发环境, DAP-Link 烧录和调试 合宙AIR105(二): 时钟设置和延迟函数 Air105 的时钟 高频振荡源 芯片支持使用内部振荡源, 或使 ...
- python基础知识-day8(函数实战)
1 def out(): 2 username=input("请输入用户名:\n") 3 password=input("请输入密码:\n") 4 return ...
- Java开发学习(八)----IOC/DI配置管理第三方bean、加载properties文件
前面的博客都是基于我们自己写的类,现在如果有需求让我们去管理第三方jar包中的类,该如何管理? 一.案例:数据源对象管理 本次案例将使用数据源Druid和C3P0来配置学习下. 1.1 环境准备 学习 ...
- RS485通信电路
RS485由RS232和RS422发展而来,弥补了抗干扰能力差.通信距离短.速率低的缺点,增加了多点.双向通信能力,即允许多个发送器连接在同一条主线上,同时增加了发送器的驱动能力和冲突保护特性,扩展了 ...
- 【小程序自动化Minium】三、元素定位- WXSS 选择器的使用
最近更新略疲,主要是业余时间多了几个变化.比如忙活自己的模拟赛车驾舱升级.还跟朋友筹备一个小程序项目.另外早上的时间留给背单词了... 上一章中讲到Page接口的get_element()与get_e ...
- VT-x is not available (VERR_VMX_NO_VMX).无法打开虚拟机,无法新建64位虚拟机
管理员身份打开cmd bcdedit /set hypervisorlaunchtype off 重启生效
- 减省 Java 小半内存,Solon v1.9.2 发布
相对于 Spring Boot 和 Spring Cloud 的项目: 启动快 5 - 10 倍. (更快) qps 高 2- 3 倍. (更高) 运行时内存节省 1/3 ~ 1/2. (更少) 打包 ...
- Note -「0/1 Fractional Programming」
What is that? Let us pay attention to a common problem that we often meet in daily life: There are \ ...
- Centos7 安装mysql服务器并开启远程访问功能
大二的暑假,波波老师送了一个华为云的服务器给我作测试用,这是我程序员生涯里第一次以root身份拥有一台真实的云服务器 而之前学习的linux知识在这时也派上了用场,自己的物理机用的是ubuntu系统, ...