介绍MybatisPlus
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性
无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
支持的数据库
任何能使用 mybatis 进行 crud, 并且支持标准 sql 的数据库
整合SpringBoot
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>数据库
MySQL dump 10.13 Distrib 5.5.27, for Win32 (x86) CREATE TABLE `payment` ( `id` int(11) NOT NULL AUTO_INCREMENT, `serial` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; >INSERT INTO `payment` VALUES (1,'aaa'),(2,'bbb'),(3,'aabb03'),(4,'aabb03'),(5,'aabb03'),(6,'aabb04'); UNLOCK TABLES;
更改配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT%2B8
username: 自己的数据库用户名
password: 自己的数据库密码
# mybatis日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl编写实体类
package com.cly.mybatisplus.pojo;
import lombok.Data;
import java.io.Serializable;
/**
* @author cly
* @version 1.0
* @ClassName Payment
* @date 2021-06-06 11:30
*/
@Data
public class Payment implements Serializable {
private int id;
private String serial;
}编写mapper
编写的mapper接口需要继承BaseMapper<t> T指代的是对应的实体类</t>
package com.cly.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cly.mybatisplus.pojo.Payment;
import org.springframework.stereotype.Repository;
@Repository
public interface PaymentMapper extends BaseMapper<Payment> {
}编写测试
插入的测试
@Test
public void insert(){
Payment payment = new Payment();
payment.setSerial("cly");
int insert = paymentMapper.insert(payment);
System.out.println("insert:"+insert);
}
// 测试结果
Creating a new SqlSessionSqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d5508e0] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@285781448 wrapping com.mysql.cj.jdbc.ConnectionImpl@8a98f38] will not be managed by Spring
==> Preparing: INSERT INTO payment ( id, serial ) VALUES ( ?, ? )
==> Parameters: 0(Integer), cly(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d5508e0]
insert:1其中主键不写,MybatisPlus提供了默认的生成主键的规则
有:默认使用雪花算法生成ID、使用自动增长、使用UUID生成、手动输入
设置主键生成方式可以在实体类的主键属性上添加TableId注解
@TableId(type = IdType.AUTO) // 使用自动增长 @TableId(type = IdType.ID_WORKER) // 使用雪花算法对应的主键类型为数字类型 @TableId(type = IdType.ID_WORKER_STR) //使用雪花算法对应的主键类型为字符串类型 @TableId(type = IdType.UUID) // 使用UUID生成 @TableId(type = IdType.NONE) // 手动输入id @TableId(type = IdType.INPUT) // 手动输入id
修改的测试
@Test
public void update(){
Payment payment = new Payment();
payment.setId(2);
payment.setSerial("cly");
int update = paymentMapper.updateById(payment);
System.out.println("update:"+update);
}
// 测试结果
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5b84f14] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@2048869869 wrapping com.mysql.cj.jdbc.ConnectionImpl@1744a475] will not be managed by Spring
==> Preparing: UPDATE payment SET serial=? WHERE id=?
==> Parameters: cly(String), 2(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5b84f14]
update:1
自动填充
修改表
ALTER TABLE db2020.payment DROP COLUMN create_time; ALTER TABLE db2020.payment DROP COLUMN update_time; ALTER TABLE db2020.payment ADD create_time DATETIME; ALTER TABLE db2020.payment ADD update_time DATETIME;
修改实体类
package com.cly.mybatisplus.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
/**
* @author cly
* @version 1.0
* @ClassName Payment
* @date 2021-06-06 11:30
*/
@Data
public class Payment implements Serializable {
@TableId(type = IdType.INPUT)
private int id;
private String serial;
private DATE createTime;
private DATE updateTime;
}自动填充实现规则
在实体类上需要自动填充的属性加上注解
@TableField(fill = FieldFill.INSERT) // 添加时自动填充
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE) // 添加或修改时自动填充
private Date updateTime;创建一个类,实现MetaObjectHandler接口
package com.cly.mybatisplus.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @author cly
* @version 1.0
* @ClassName MyMetaObjectHandle
* @date 2021-06-06 15:33
*/
@Slf4j
@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
// 使用MP的添加时调用该方法
@Override
public void insertFill(MetaObject metaObject) {
// setFieldValByName第一个参数:需要自动填充的实体类属性名
// setFieldValByName第二个参数:需要自动填充的值
// setFieldValByName第三个参数:元数据
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
// 使用MP的修改时调用该方法
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}测试自动填充
// 测试添加
@Test
public void insert(){
Payment payment = new Payment();
payment.setSerial("cly");
int insert = paymentMapper.insert(payment);
System.out.println("insert:"+insert);
}
// 测试结果
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27a97e08] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@390374517 wrapping com.mysql.cj.jdbc.ConnectionImpl@444cc791] will not be managed by Spring
==> Preparing: INSERT INTO payment ( id, serial, create_time, update_time ) VALUES ( ?, ?, ?, ? )
==> Parameters: 0(Integer), cly(String), 2021-06-06 15:44:15.08(Timestamp), 2021-06-06 15:44:15.08(Timestamp)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@27a97e08]
insert:1
// 测试修改
@Test
public void update(){
Payment payment = new Payment();
payment.setId(2);
payment.setSerial("cly");
int update = paymentMapper.updateById(payment);
System.out.println("update:"+update);
}
// 测试结果
Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@68fe48d7] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@407877261 wrapping com.mysql.cj.jdbc.ConnectionImpl@71d8cfe7] will not be managed by Spring
==> Preparing: UPDATE payment SET serial=?, update_time=? WHERE id=?
==> Parameters: cly(String), 2021-06-06 15:45:58.449(Timestamp), 2(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@68fe48d7]
update:1从结果中发现添加和修改时都会自动填充上时间,自动填充设置是成功
MP中乐观锁的实现
数据库中添加一个字段
ALTER TABLE db2020.payment ADD version int;
修改实体类
@Version // 版本号注解
@TableField(fill = FieldFill.INSERT)
private int version;编写一个配置类
package com.cly.mybatisplus.config;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author cly
* @version 1.0
* @ClassName MyConfig
* @date 2021-06-06 16:06
*/
@Configuration
@MapperScan("com.cly.mybatisplus.mapper")
public class MyConfig {
// 乐观锁插件
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
}测试乐观锁
@Test
public void testOptimisticLocker(){
Payment payment = paymentMapper.selectById(9);
payment.setSerial("ccllyy");
paymentMapper.updateById(payment);
}
// 测试结果
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a34e9f] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1167916234 wrapping com.mysql.cj.jdbc.ConnectionImpl@2acbc859] will not be managed by Spring
==> Preparing: SELECT id,serial,create_time,update_time,version FROM payment WHERE id=?
==> Parameters: 9(Integer)
<== Columns: id, serial, create_time, update_time, version
<== Row: 9, cly, 2021-06-06 16:15:18, 2021-06-06 16:15:18, 1
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a34e9f]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26d96e5] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1730129134 wrapping com.mysql.cj.jdbc.ConnectionImpl@2acbc859] will not be managed by Spring
==> Preparing: UPDATE payment SET serial=?, create_time=?, update_time=?, version=? WHERE id=? AND version=?
==> Parameters: ccllyy(String), 2021-06-06 16:15:18.0(Timestamp), 2021-06-06 16:17:15.109(Timestamp), 2(Integer), 9(Integer), 1(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26d96e5]发现数据库中的version变为了2 乐观锁测试成功
查询的测试
查询所有
package com.cly.mybatisplus;
import com.cly.mybatisplus.mapper.PaymentMapper;
import com.cly.mybatisplus.pojo.Payment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class MybatisPlusApplicationTests {
@Autowired
private PaymentMapper paymentMapper;
@Test
public void findAll(){
List<Payment> payments = paymentMapper.selectList(null);
System.out.println(payments);
}
}
// 测试结果
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c551ad4] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1145882513 wrapping com.mysql.cj.jdbc.ConnectionImpl@1c5c616f] will not be managed by Spring
==> Preparing: SELECT id,serial FROM payment
==> Parameters:
<== Columns: id, serial
<== Row: 1, aaa
<== Row: 2, bbb
<== Row: 3, aabb03
<== Row: 4, aabb03
<== Row: 5, aabb03
<== Row: 6, aabb04
<== Total: 6
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c551ad4]
[Payment(id=1, serial=aaa), Payment(id=2, serial=bbb), Payment(id=3, serial=aabb03), Payment(id=4, serial=aabb03), Payment(id=5, serial=aabb03), Payment(id=6, serial=aabb04)]通过ID查询
@Test
public void findById() {
Payment payment = paymentMapper.selectById(2);
System.out.println(payment);
}
// 查询结果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6f6621e3] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@717998169 wrapping com.mysql.cj.jdbc.ConnectionImpl@6ab7ce48] will not be managed by Spring
==> Preparing: SELECT id,serial,create_time,update_time,version FROM payment WHERE id=?
==> Parameters: 2(Integer)
<== Columns: id, serial, create_time, update_time, version
<== Row: 2, cly, null, 2021-06-06 15:45:58, null
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6f6621e3]
Payment(id=2, serial=cly, createTime=null, updateTime=Sun Jun 06 15:45:58 CST 2021, version=0)通过ids实现查询
@Test
public void findByIds() {
List<Payment> payments = paymentMapper.selectBatchIds(Arrays.asList(1, 2, 3));
System.out.println(payments);
}
// 查询结果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@39ad12b6] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@603184112 wrapping com.mysql.cj.jdbc.ConnectionImpl@31d6f3fe] will not be managed by Spring
==> Preparing: SELECT id,serial,create_time,update_time,version FROM payment WHERE id IN ( ? , ? , ? )
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<== Columns: id, serial, create_time, update_time, version
<== Row: 1, aaa, null, null, null
<== Row: 2, cly, null, 2021-06-06 15:45:58, null
<== Row: 3, aabb03, null, null, null
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@39ad12b6]
[Payment(id=1, serial=aaa, createTime=null, updateTime=null, version=0), Payment(id=2, serial=cly, createTime=null, updateTime=Sun Jun 06 15:45:58 CST 2021, version=0), Payment(id=3, serial=aabb03, createTime=null, updateTime=null, version=0)]简单的条件查询
@Test
public void findByCondition() {
HashMap<String, Object> condtionMap = new HashMap<>();
condtionMap.put("serial","cly"); // key:查询的数据库字段 value: 查询字段满足的要求
List<Payment> payments = paymentMapper.selectByMap(condtionMap);
System.out.println(payments);
}
// 查询结果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4eb45fec] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@869610006 wrapping com.mysql.cj.jdbc.ConnectionImpl@69a2b3b6] will not be managed by Spring
==> Preparing: SELECT id,serial,create_time,update_time,version FROM payment WHERE serial = ?
==> Parameters: cly(String)
<== Columns: id, serial, create_time, update_time, version
<== Row: 2, cly, null, 2021-06-06 15:45:58, null
<== Row: 7, cly, null, null, null
<== Row: 8, cly, 2021-06-06 15:44:15, 2021-06-06 15:44:15, null
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4eb45fec]
[Payment(id=2, serial=cly, createTime=null, updateTime=Sun Jun 06 15:45:58 CST 2021, version=0), Payment(id=7, serial=cly, createTime=null, updateTime=null, version=0), Payment(id=8, serial=cly, createTime=Sun Jun 06 15:44:15 CST 2021, updateTime=Sun Jun 06 15:44:15 CST 2021, version=0)]MP实现分页查询
在配置类中添加配置
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}添加好后编写分页代码
@Test
public void testPage() {
Page<Payment> page = new Page<>(1,5); // 第一个参数:从第几条数据开始 第二个参数:一页多少条数据
// 将查询到的数据封装到page对象中
paymentMapper.selectPage(page, null);// 第一个参数:page对象 第二个参数:查询条件
System.out.println(page.getCurrent()); // 获得当前页
System.out.println(page.getPages()); // 获得总页数
System.out.println(page.getRecords()); // 获得当前页的所有记录
System.out.println(page.getSize()); // 获得当前页的记录数
System.out.println(page.getTotal()); // 获得查询后的总记录数
System.out.println(page.hasNext()); // 获得是否还有下一页
System.out.println(page.hasPrevious()); // 获得是否还有上一页
}
// 查询结果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f5eae0f] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@75480150 wrapping com.mysql.cj.jdbc.ConnectionImpl@151ef57f] will not be managed by Spring
JsqlParserCountOptimize sql=SELECT id,serial,create_time,update_time,version FROM payment
==> Preparing: SELECT COUNT(1) FROM payment
==> Parameters:
<== Columns: COUNT(1)
<== Row: 9
==> Preparing: SELECT id,serial,create_time,update_time,version FROM payment LIMIT 0,5
==> Parameters:
<== Columns: id, serial, create_time, update_time, version
<== Row: 1, aaa, null, null, null
<== Row: 2, cly, null, 2021-06-06 15:45:58, null
<== Row: 3, aabb03, null, null, null
<== Row: 4, aabb03, null, null, null
<== Row: 5, aabb03, null, null, null
<== Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f5eae0f]
1
2
[Payment(id=1, serial=aaa, createTime=null, updateTime=null, version=0), Payment(id=2, serial=cly, createTime=null, updateTime=Sun Jun 06 15:45:58 CST 2021, version=0), Payment(id=3, serial=aabb03, createTime=null, updateTime=null, version=0), Payment(id=4, serial=aabb03, createTime=null, updateTime=null, version=0), Payment(id=5, serial=aabb03, createTime=null, updateTime=null, version=0)]
5
9
true
falseMP实现删除
物理删除
将数据从数据库删除
通过id删除
@Test
public void deleteById() {
int result = paymentMapper.deleteById(2);
System.out.println(result);
}通过ids删除
@Test
public void deleteByIds() {
int result = paymentMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
System.out.println(result);
}带条件的删除
@Test
public void deleteByCondition() {
HashMap<String, Object> condtionMap = new HashMap<>();
condtionMap.put("serial","cly");
int result = paymentMapper.deleteByMap(condtionMap);
System.out.println(result);
}逻辑删除
添加一个字段删除就更改字段值,数据库中不会清除
添加数据库字段
ALTER TABLE db2020.payment ADD delete_id int;
修改实体类
package com.cly.mybatisplus.pojo;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author cly
* @version 1.0
* @ClassName Payment
* @date 2021-06-06 11:30
*/
@Data
public class Payment implements Serializable {
private int id;
private String serial;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
@TableField(fill = FieldFill.INSERT)
private int version;
@TableLogic
@TableField(fill = FieldFill.INSERT)
private int deleteId;
}配置类中添加插件
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}修改配置文件(配置默认0是不删除,1是删除。可以自己修改)
# mybatis日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config: logic-delete-value=1
db-config: logic-not-delete-value=0调用的方法和物理删除的方法一样
MP的性能分析插件
性能分析拦截器,用于输出每条sql语句及执行时间,
sql性能执行分析,开发环境使用,超过指定时间,停止运行。方便发现问题
配置插件
在配置类中添加如下配置
@Bean
@Profile({"dev","test"}) // 指定使用该插件的环境
public PerformanceInterceptor performanceInterceptor(){
final PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(100); // 设置超时时间单位为ms
performanceInterceptor.setFormat(true); // sql是否格式化 默认为false
return performanceInterceptor;
}修改配置
spring:
profiles:
active: dev测试不超时结果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4088741b] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1415937490 wrapping com.mysql.cj.jdbc.ConnectionImpl@706eab5d] will not be managed by Spring
==> Preparing: INSERT INTO payment ( id, serial, create_time, update_time, version, delete_id ) VALUES ( ?, ?, ?, ?, ?, ? )
==> Parameters: 0(Integer), cly(String), 2021-06-06 19:24:43.319(Timestamp), 2021-06-06 19:24:43.319(Timestamp), 1(Integer), 0(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4088741b]
insert:1
Time:10 ms - ID:com.cly.mybatisplus.mapper.PaymentMapper.insert
Execute SQL:
INSERT
INTO
payment
( id, serial, create_time, update_time, version, delete_id )
VALUES
( 0, 'cly', '2021-06-06 19:24:43', '2021-06-06 19:24:43', 1, 0 )
测试超时结果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@184fb68d] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1112062307 wrapping com.mysql.cj.jdbc.ConnectionImpl@7f08caf] will not be managed by Spring
==> Preparing: INSERT INTO payment ( id, serial, create_time, update_time, version, delete_id ) VALUES ( ?, ?, ?, ?, ?, ? )
==> Parameters: 0(Integer), cly(String), 2021-06-06 19:28:00.67(Timestamp), 2021-06-06 19:28:00.67(Timestamp), 1(Integer), 0(Integer)
<== Updates: 1
Time:10 ms - ID:com.cly.mybatisplus.mapper.PaymentMapper.insert
Execute SQL:
INSERT
INTO
payment
( id, serial, create_time, update_time, version, delete_id )
VALUES
( 0, 'cly', '2021-06-06 19:28:00', '2021-06-06 19:28:00', 1, 0 )
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@184fb68d]
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
### The error may involve com.cly.mybatisplus.mapper.PaymentMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO payment ( id, serial, create_time, update_time, version, delete_id ) VALUES ( ?, ?, ?, ?, ?, ? )
### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize ! 超时会抛出Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize ! 异常
MP实现复杂的条件查询
创建一个QQueryWrapper象
调用QueryWrapper对象的方法实现条件查询
@Test
public void testQueryWrapper() {
QueryWrapper<Payment> queryWrapper = new QueryWrapper<>();
// ge:大于等于、gt:大于、le:小于等于、lt:小于
queryWrapper.ge("id",3); // 第一个参数:数据库的字段 第二个参数:要查询查询的范围
// eq:等于、ne:不等于
// between:查询对象在两值之间、notBetween:查询对象不在两值之间
// like 模糊查询
// orderByDesc 降序、orderByAsc:升序
// last 将sql拼接到最后
// 指定要查询的列
List<Payment> payments = paymentMapper.selectList(queryWrapper); // 得到复杂条件查询的对象集
}
只是举了一例 其他都是相似的就不一一列举了
SpringBoot整合MybatisPlus体上就是这么多了

京公网安备 11010502036488号