当前位置 博文首页 > 努力充实,远方可期:mybatis-plus简单笔记
https://www.bilibili.com/video/BV1yA411t782
国产的开源框架,基于 MyBatis
核心功能就是简化 MyBatis 的开发,提高效率。
Spring Boot(2.3.0) + MyBatis Plus(国产的开源框架,并没有接入到 Spring 官方孵化器中)
1、创建 Maven 工程
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-08Cb1Y7w-1612972152936)(/Users/southwind/Library/Application Support/typora-user-images/image-20200516201427232.png)]
2、pom.xml 引入 MyBatis Plus 的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
3、创建实体类
package com.southwind.mybatisplus.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
4、创建 Mapper 接口
package com.southwind.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.southwind.mybatisplus.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
5、application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6、启动类需要添加 @MapperScan(“mapper所在的包”),否则无法加载 Mppaer bean。
package com.southwind.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southwind.mybatisplus.mapper")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
7、测试
package com.southwind.mybatisplus.mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class UserMapperTest {
@Autowired
private UserMapper mapper;
@Test
void test(){
mapper.selectList(null).forEach(System.out::println);
}
}
映射数据库的表名
package com.southwind.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName(value = "user")
public class Account {
private Integer id;
private String name;
private Integer age;
}
设置主键映射,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 | 数据库自增 |
NONE | MP set 主键,雪花算法实现 |
INPUT | 需要开发者手动赋值 |
ASSIGN_ID | MP 分配 ID,Long、Integer、String |
ASSIGN_UUID | 分配 UUID,Strinig |
INPUT 如果开发者没有手动赋值,则数据库通过自增的方式给主键赋值,如果开发者手动赋值,则存入该值。
AUTO 默认就是数据库自增,开发者无需赋值。
ASSIGN_ID MP 自动赋值,雪花算法。
ASSIGN_UUID 主键的数据类型必须是 String,自动生成 UUID 进行赋值
映射非主键字段,value 映射字段名
exist
表示是否为数据库字段 false,如果实体类中的成员变量在数据库中没有对应的字段,则可以使用 exist
。VO、DTO用的比较多select
表示是否查询该字段fill
表示是否自动填充,将对象存入数据库的时候,由 MyBatis Plus 自动给某些字段赋值,如create_time、update_time1、给表添加 create_time、update_time 字段
2、实体类中添加成员变量
package com.southwind.mybatisplus.entity;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private Integer age;
@TableField(exist = false)
private String gender;
// 第一次插入时候赋值
@TableField(fill = FieldFill.INSERT)
private Date createTime;
// 每次更新都
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
3、创建自动填充处理器
package com.southwind.mybatisplus.handler;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
标记乐观锁,通过 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
package com.southwind.mybatisplus.entity;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private Integer age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
//
@Version
private Integer version;
}
3、注册配置类
package com.southwind.mybatisplus.config;
@Configuration
public class MyBatisPlusConfig {
@Bean // 乐观锁
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
}
1、通用枚举类注解,将数据库字段映射成实体类的枚举类型成员变量
package com.southwind.mybatisplus.enums;
public enum StatusEnum {
WORK(1,"上班"),
REST(0,"休息");
StatusEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
@EnumValue
private Integer code;
private String msg;
}
package com.southwind.mybatisplus.entity;
@Data
@TableName(value = "user")
public class User {
@TableId
private String id;
@TableField(value = "name",select = false)
private String title;
private Integer age;
@TableField(exist = false)
private String gender;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;