MybatisPlus框架

MyBatisPlus

配置poml文件

导入plus坐标

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>

导入druid坐标

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>

配置属性文件 后缀改yml

spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: "123456"

dao类

@Mapper
public interface userDao extends BaseMapper<user> {
}

分页查询

IPage page =new Page()

selectPage方法

数据存放在了page对象中

getRecords 数据

需要配置拦截器

image-20240309161730865

image-20240309161752073

条件查询

image-20240309162233396

属性名避免写错

image-20240309162346513

简化第二种方式

image-20240309162447385

或者 条件

image-20240309162652144

条件查询 null值处理

不推荐

image-20240309163306410

推荐

image-20240309163334476

查询投影

image-20240309163732491

查询条件

image-20240309164237748

  • lt
  • le
  • gt
  • ge
  • eq
  • between

模糊匹配

likeLeft

likeRight

映射

image-20240309164526274

编码中出现了数据库中未定义的属性

image-20240309164624633

设置相关字段不参与查询

image-20240309165201618

表名与编码开发设计不同步

image-20240309165255825

id生成策略

image-20240309165808736

雪花算法

image-20240309165927608

image-20240309170032765

或者

image-20240309170108446

多记录

deleteBatchIds

selectBatchIds

逻辑删除

设置逻辑删除字段

库里添加字段

实体类配置

image-20240309171436655

或者pom

image-20240309171531491

乐观锁

数据库添加字段

实体类添加注解

image-20240309172355795

添加拦截器

image-20240309172419488

代码生成器

image-20240309172906089

image-20240309173727804

image-20240309173744144

image-20240309173754659

image-20240309173803505

image-20240309173811929

逻辑删除

只需要修改配置文件 既可以正常调用mp提供的delete get方法

image-20240531223649110

静态工具

仅作为避免循环依赖使用

查询 多表查询

只使用Service层会导致循环注入 所以可以使用静态工具

image-20240531223340742

枚举处理器

image-20240531224647843

mybatis中提供ibatis包

使用注解@EnumValue 然后添加枚举处理器

image-20240531225022239

返回值默认返回枚举 昵称 添加@JsonValue 可选返回value或者dsc

批处理

配置文件开启 rewriteBatchedStatements=true

普通for循环逐条插入速度极差 不推荐

MP默认批处理 基于预编译批处理 仅仅是批量提交

配置jdbc参数 驱动会自动把一条条的语句重写成一条语句

json处理器