mybatisplus笔记

CREATE DATABASE mybatis_plus /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use mybatis_plus; CREATE TABLE user
( id bigint(20) NOT NULL COMMENT ‘主键ID’,
name varchar(30) DEFAULT NULL COMMENT ‘姓名’,
age int(11) DEFAULT NULL COMMENT ‘年龄’,
email varchar(50) DEFAULT NULL COMMENT ‘邮箱’,
PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mybatisplus默认使用雪花算法生成id

mybtisplus流程:
1.扫描实体类,2通过实体类映射去找数据库中对应的表和字段,3执行sql


mybatisplus简介:
MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 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 的数据库,具体支持情况如下
MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,
ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb
达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据
库,瀚高数据库

引入依赖


org.springframework.boot
spring-boot-starter


spring-boot-starter-test
test

com.baomidou
mybatis-plus-boot-starter
3.5.1


org.projectlombok
lombok
true


mysql
mysql-connector-java
runtime


c>idea中安装lombok插件
4、编写代码
a>配置application.yml
spring:

配置数据源信息

datasource:

配置数据源类型 type: com.zaxxer.hikari.HikariDataSource

配置连接数据库信息 driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf- 8&useSSL=false username: root password: 123456
注意:
1、驱动类driver-class-name
spring boot 2.0(内置jdbc5驱动),驱动类使用:
driver-class-name: com.mysql.jdbc.Driver
spring boot 2.1及以上(内置jdbc8驱动),驱动类使用:
driver-class-name: com.mysql.cj.jdbc.Driver
否则运行测试用例的时候会有 WARN 信息
2、连接地址url
MySQL5.7版本的url:
jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
MySQL8.0版本的url:
jdbc:mysql://localhost:3306/mybatis_plus?
serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or
represents more
在Spring Boot启动类中添加@MapperScan注解,扫描mapper包
c>添加实体
User类编译之后的结果:
@SpringBootApplication
@MapperScan(“com.atguigu.mybatisplus.mapper”)
public class MybatisplusApplication
{
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}

添加日志
在application.yml中配置日志输出
三、基本CRUD
1、BaseMapper
MyBatis-Plus中的基本CRUD在内置的BaseMapper中都已得到了实现,我们可以直接使用,接口如
下:

配置MyBatis日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型
public interface UserMapper extends BaseMapper { }
测试:
@SpringBootTest public class MybatisPlusTest
{
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList(){
//selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
userMapper.selectList(null).forEach(System.out::println); } }

//解析这段lambda表达式:forEach()的参数如下
Comsumer ction=new Comsuer(){
@Override

public void accept(T t){
System.out.Println(t);
}
}
Comsumer action=(t)->Systm.out.println(t);
Comsumer action=Systm.out::println;
lambda表达式对象::实例方法的引用方式,传入的参数必须跟方法体里的参数保持一致,在方法体中有另外一个方法完成了功能就可以使用这种方式。

default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}

@FunctionalInterface

public interface Consumer {
}

package com.baomidou.mybatisplus.core.mapper;
public interface BaseMapper extends Mapper
{ /*** 插入一条记录 * @param entity 实体对象 /
int insert(T entity);
/
** 根据 ID 删除 * @param id 主键ID /
int deleteById(Serializable id);
/
** 根据实体(ID)删除 * @param entity 实体对象 * @since 3.4.4 */

int deleteById(T entity);
/*** 根据 columnMap 条件,删除记录 * @param columnMap 表字段 map 对象 /
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/
** 根据 entity 条件,删除记录 * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)/ int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** 删除(根据ID 批量删除) * @param idList 主键ID列表(不能为 null 以及 empty) / int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); /** 根据 ID 修改 * @param entity 实体对象 / int updateById(@Param(Constants.ENTITY) T entity); /** 根据 whereEntity 条件,更新记录 * @param entity 实体对象 (set 条件值,可以为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) / int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper); /** 根据 ID 查询 * @param id 主键ID / T selectById(Serializable id); /** 查询(根据ID 批量查询) * @param idList 主键ID列表(不能为 null 以及 empty) / List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); /** 查询(根据 columnMap 条件) * @param columnMap 表字段 map 对象 / List selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); /*

  • 根据 entity 条件,查询一条记录 *

    查询一条记录,例如 qw.last(“limit 1”) 限制取一条记录, 注意:多条数据会报异常

    * @param queryWrapper 实体对象封装操作类(可以为 null) / default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) { List ts = this.selectList(queryWrapper); if (CollectionUtils.isNotEmpty(ts)) { if (ts.size() != 1) { throw ExceptionUtils.mpe(“One record is expected, but the query result is multiple records”); }return ts.get(0); }return null; }/** 根据 Wrapper 条件,查询总记录数 * @param queryWrapper 实体对象封装操作类(可以为 null) / Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** 根据 entity 条件,查询全部记录 * @param queryWrapper 实体对象封装操作类(可以为 null) / List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** 根据 Wrapper 条件,查询全部记录 * @param queryWrapper 实体对象封装操作类(可以为 null) / List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** 根据 Wrapper 条件,查询全部记录 *

    注意: 只返回第一个字段的值

    * @param queryWrapper 实体对象封装操作类(可以为 null) / List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper); /** 根据 entity 条件,查询全部记录(并翻页) * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) / <P extends IPage> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper); /** 根据 Wrapper 条件,查询全部记录(并翻页) * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */ <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

@TableId(value = “id”)
private Integer id;
我们可以设置主键的生成方式

@TableId(value = “id”,type = IdType.INPUT) 这种方式是主键手动输入
主键生成方式类型如下(IdType):

AUTO(0, “数据库ID自增”),
INPUT(1, “用户输入ID”),
ID_WORKER(2, “全局唯一ID”),
UUID(3, “全局唯一ID”),
NONE(4, “该类型为未设置主键类型”),
ID_WORKER_STR(5, “字符串全局唯一ID”);

MyBatis-Plus中的基本CRUD在内置的BaseMapper中都已得到了实现,我们可以直接使用,接口如
下:

配置MyBatis日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl package com.baomidou.mybatisplus.core.mapper;

public interface BaseMapper extends Mapper
{ /*** 插入一条记录 * @param entity 实体对象 / int insert(T entity);
/
** 根据 ID 删除 * @param id 主键ID / int deleteById(Serializable id);
/
** 根据实体(ID)删除 * @param entity 实体对象 * @since 3.4.4 /
int deleteById(T entity); /
** 根据 columnMap 条件,删除记录 * @param columnMap 表字段 map 对象 /
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/
** 根据 entity 条件,删除记录 * @param queryWrapper 实体对象封装操作类
(可以为 null,里面的 entity 用于生成 where 语句)/ int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper);
/
** 删除(根据ID 批量删除) * @param idList 主键ID列表(不能为 null 以及 empty) / int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends
Serializable> idList);
/
** 根据 ID 修改 * @param entity 实体对象 / int updateById(@Param(Constants.ENTITY) T entity);
/
** 根据 whereEntity 条件,更新记录 * @param entity 实体对象 (set 条件值,可以为 null) * @param updateWrapper 实体对象封装操作类
(可以为 null,里面的 entity 用于生成 where 语句) / int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);
/
** 根据 ID 查询 * @param id 主键ID / T selectById(Serializable id); /** 查询(根据ID 批量查询) * @param idList 主键ID列表(不能为 null 以及 empty) /
List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
/
** 查询(根据 columnMap 条件) * @param columnMap 表字段 map 对象 / List selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/
*

  • 根据 entity 条件,查询一条记录 *

    查询一条记录,例如 qw.last(“limit 1”) 限制取一条记录, 注意:多条数据会报异常

* @param queryWrapper 实体对象封装操作类(可以为 null) */ default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) { List ts = this.selectList(queryWrapper); if (CollectionUtils.isNotEmpty(ts)) { if (ts.size() != 1) { throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records"); }return ts.get(0); }return null; } /*** 根据 Wrapper 条件,查询总记录数 * @param queryWrapper 实体对象封装操作类(可以为 null) */ Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper); /*** 根据 entity 条件,查询全部记录 * @param queryWrapper 实体对象封装操作类 (可以为 null) */ List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper); /*** 根据 Wrapper 条件,查询全部记录 * @param queryWrapper 实体对象封装操作类(可以为 null) */ List

说明:
通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用 get 查询单行 remove 删 除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
泛型 T 为任意实体对象
建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService 继承
Mybatis-Plus 提供的基类
官网地址:https://baomidou.com/pages/49cc81/#service-crud-%E6%8E%A5%E5%8F%
A3
a>IService
MyBatis-Plus中有一个接口 IService和其实现类 ServiceImpl,
封装了常见的业务层逻辑
详情查看源码IService和ServiceImpl
b>创建Service接口和实现类
/*** UserService继承IService模板提供的基础功能 / public interface UserService extends IService { }
/
** ServiceImpl实现了IService,提供了IService中基础功能的实现 * 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,
并在实现类中实现 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
@Autowired private UserService userService; @Test public void testGetCount(){ long count = userService.count(); System.out.println(“总记录数:” + count); }@Test public void testSaveBatch(){ // SQL长度有限制,海量数据插入单条SQL无法实行, // 因此MP将批量插入放在了通用Service中实现,而不是通用Mapper ArrayList users = new ArrayList<>(); for (int i = 0; i < 5; i++) { User user = new User(); user.setName(“ybc” + i); user.setAge(20 + i); users.add(user); }//SQL:INSERT INTO t_user ( username, age ) VALUES ( ?, ? ) userService.saveBatch(users); }

常用注解:
@TableName
经过以上的测试,在使用MyBatis-Plus实现基本的CRUD时,我们并没有指定要操作的表,只是在
Mapper接口继承BaseMapper时,设置了泛型User,而操作的表为user表
由此得出结论,MyBatis-Plus在确定操作的表时,由BaseMapper的泛型决定,即实体类型决
定,且默认操作的表名和实体类型的类名一致
若实体类类型的类名和要操作的表的表名不一致,会出现什么问题?
/*** ServiceImpl实现了IService,提供了IService中基础功能的实现 * 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现 */ @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }@Autowired private UserService userService; @Test public void testGetCount(){ long count = userService.count(); System.out.println(“总记录数:” + count); }@Test public void testSaveBatch(){ // SQL长度有限制,海量数据插入单条SQL无法实行, // 因此MP将批量插入放在了通用Service中实现,而不是通用Mapper ArrayList users = new ArrayList<>(); for (int i = 0; i < 5; i++) { User user = new User(); user.setName(“ybc” + i); user.setAge(20 + i); users.add(user); }//SQL:INSERT INTO t_user ( username, age ) VALUES ( ?, ? ) userService.saveBatch(users); }

我们将表user更名为t_user,测试查询功能
程序抛出异常,Table ‘mybatis_plus.user’ doesn’t exist,因为现在的表名为t_user,而默认操作
的表名和实体类型的类名一致,即user表
在实体类类型上添加@TableName(“t_user”),标识实体类对应的表,即可成功执行SQL语句
通过全局配置解决问题
在开发的过程中,我们经常遇到以上的问题,即实体类所对应的表都有固定的前缀,例如t_或tbl_
此时,可以使用MyBatis-Plus提供的全局配置,为实体类所对应的表名设置默认的前缀,那么就
不需要在每个实体类上通过@TableName标识实体类对应的表
mybatis-plus: configuration: # 配置MyBatis日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: # 配置MyBatis-Plus操作表的默认前缀 table-prefix: t_
@TableId
经过以上的测试,MyBatis-Plus在实现CRUD时,会默认将id作为主键列,并在插入数据时,默认
基于雪花算法的策略生成id
@TableId的value属性
若实体类中主键对应的属性为id,而表中表示主键的字段为uid,此时若只在属性id上添加注解
@TableId,则抛出异常Unknown column ‘id’ in ‘field list’,即MyBatis-Plus仍然会将id作为表的
主键操作,而表中表示主键的是字段uid
此时需要通过@TableId注解的value属性,指定表中的主键字段,@TableId(“uid”)或
@TableId(value=“uid”)
@TableId的type属性
type属性用来定义主键策略
@TableField
经过以上的测试,我们可以发现,MyBatis-Plus在执行SQL语句时,要保证实体类中的属性名和
表中的字段名一致
如果实体类中的属性名和字段名不一致的情况,会出现什么问题呢?
a>情况1
若实体类中的属性使用的是驼峰命名风格,而表中的字段使用的是下划线命名风格
例如实体类属性userName,表中字段user_name
此时MyBatis-Plus会自动将下划线命名风格转化为驼峰命名风格
相当于在MyBatis中配置
b>情况2
若实体类中的属性和表中的字段不满足情况1
例如实体类属性name,表中字段username
此时需要在实体类属性上使用@TableField(“username”)设置属性所对应的字段名
@TableLogic
a>逻辑删除
物理删除:真实删除,将对应数据从数据库中删除,之后查询不到此条被删除的数据
逻辑删除:假删除,将对应数据中代表是否被删除字段的状态修改为“被删除状态”,之后在数据库
中仍旧能看到此条数据记录
使用场景:可以进行数据恢复

QueryWrapper
组装查询条件
b>例2:组装排序条件
@Test public void test01(){ //查询用户名包含a,年龄在20到30之间,并且邮箱不为null的用户信息
//SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (username LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.like(“username”, “a”) .between(“age”, 20, 30) .isNotNull(“email”);
List list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
@Test public void test02(){ //按年龄降序查询用户,如果年龄相同则按id升序排列
//SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age DESC,
id ASC QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper .orderByDesc(“age”) .orderByAsc(“id”);
List users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
:组装删除条件
d>例4:条件的优先级
@Test public void test02(){ //按年龄降序查询用户,如果年龄相同则按id升序排列
//SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age DESC,id ASC
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper .orderByDesc(“age”) .orderByAsc(“id”);
List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println); }
@Test
public void test03(){
//删除email为空的用户 //DELETE FROM t_user WHERE (email IS NULL)
QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.isNull(“email”);
//条件构造器也可以构建删除语句的条件 int result = userMapper.delete(queryWrapper);
System.out.println(“受影响的行数:” + result); }
@Test
public void test04() {
QueryWrapper queryWrapper = new QueryWrapper<>();
//将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改
//UPDATE t_user SET age=?, email=? WHERE (username LIKE ? AND age > ? OR email IS NULL)
queryWrapper .like(“username”, “a”) .gt(“age”, 20) .or() .isNull(“email”);
User user = new User(); user.setAge(18);
user.setEmail(“user@atguigu.com”);
int result = userMapper.update(user, queryWrapper);
System.out.println(“受影响的行数:” + result);
}
@Test
public void test04() {
QueryWrapper queryWrapper = new QueryWrapper<>();
//将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 /
/UPDATE t_user SET age=?, email=? WHERE (username LIKE ? AND (age > ? OR email IS NULL))
//lambda表达式内的逻辑优先运算 queryWrapper

e>例5:组装select子句
f>例6:实现子查询
3、UpdateWrapper
.like(“username”, “a”) .and(i -> i.gt(“age”, 20).or().isNull(“email”));
User user = new User(); user.setAge(18); user.setEmail(“user@atguigu.com”);
int result = userMapper.update(user, queryWrapper); System.out.println(“受影响的行数:” + result); }
@Test public void test05() {
//查询用户信息的username和age字段 //SELECT username,age FROM t_user
QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select(“username”, “age”);
//selectMaps()返回Map集合列表,通常配合select()使用,避免User对象中没有被查询到的列值 为nullList<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println); }
@Test
public void test06() {
//查询id小于等于3的用户信息
//SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE (id IN (select id from t_user where id <= 3))
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.inSql(“id”, “select id from t_user where id <= 3”);
List list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
@Test public void test07() {
//将(年龄大于20或邮箱为null)并且用户名中包含有a的用户信息修改
//组装set子句以及修改条件 UpdateWrapper updateWrapper = new UpdateWrapper<>();
//lambda表达式内的逻辑优先运算
updateWrapper .set(“age”, 18) .set(“email”, “user@atguigu.com”) .like(“username”, “a”) .and(i -> i.gt(“age”, 20).or().isNull(“email”));
//这里必须要创建User对象,否则无法应用自动填充。如果没有自动填充,可以设置为null
//UPDATE t_user SET username=?, age=?,email=? WHERE (username LIKE ? AND (age > ? OR email IS NULL))
//User user = new User();
//user.setName(“张三”);

4、condition
在真正开发的过程中,组装条件是常见的功能,而这些条件数据来源于用户输入,是可选的,因
此我们在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,若
没有选择则一定不能组装,以免影响SQL执行的结果
思路一:
思路二:
上面的实现方案没有问题,但是代码比较复杂,我们可以使用带condition参数的重载方法构建查
询条件,简化代码的编写
//int result = userMapper.update(user, updateWrapper); //UPDATE t_user SET age=?,
email=? WHERE (username LIKE ? AND (age > ? OR email IS NULL)) int result = userMapper.update(null, updateWrapper);
System.out.println(result); }@Test public void test08() { //定义查询条件,有可能为null(用户未输入或未选择) String username = null;
Integer ageBegin = 10; Integer ageEnd = 24; QueryWrapper queryWrapper = new QueryWrapper<>();
//StringUtils.isNotBlank()判断某字符串是否不为空且长度不为0且不由空白符(whitespace)
构成 if(StringUtils.isNotBlank(username)){ queryWrapper.like(“username”,“a”); }if(ageBegin != null){ queryWrapper.ge(“age”, ageBegin);
}if(ageEnd != null){ queryWrapper.le(“age”, ageEnd); }//SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE (age >= ? AND age <= ?)
List users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }@Test public void test08UseCondition() { //定义查询条件,有可能为null(用户未输入或未选择) String username = null; Integer ageBegin = 10; Integer ageEnd = 24; QueryWrapper queryWrapper = new QueryWrapper<>(); //StringUtils.isNotBlank()判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成 queryWrapper .like(StringUtils.isNotBlank(username), “username”, “a”)

5、LambdaQueryWrapper
6、LambdaUpdateWrapper

.ge(ageBegin != null, “age”, ageBegin) .le(ageEnd != null, “age”, ageEnd);
//SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE (age >= ? AND age <= ?) List users =
userMapper.selectList(queryWrapper); users.forEach(System.out::println); }@Test public void test09()
{ //定义查询条件,有可能为null(用户未输入) String username = “a”; Integer ageBegin = 10; Integer ageEnd = 24;
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
//避免使用字符串表示字段,防止运行时错误 queryWrapper .like(StringUtils.isNotBlank(username), User::getName, username) .ge(ageBegin != null,
User::getAge, ageBegin) .le(ageEnd != null, User::getAge, ageEnd); List users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println); }@Test public void test10() { //组装set子句 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper .set(User::getAge, 18) .set(User::getEmail, “user@atguigu.com”) .like(User::getName, “a”) .and(i -> i.lt(User::getAge, 24).or()
.isNull(User::getEmail)); //lambda 表达式内的逻辑优先运算 User user = new User(); int result = userMapper.update(user, updateWrapper);
System.out.println(“受影响的行数:” + result); }
六、插件
1、分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
a>添加配置类
@Configuration @MapperScan(“com.atguigu.mybatisplus.mapper”)
//可以将主类中的注解移到此处 public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor()
{ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
@Test public void testPage(){ //设置分页参数 Page page = new Page<>(1, 5); userMapper.selectPage(page, null);
//获取分页数据 List list = page.getRecords(); list.forEach(System.out::println); System.out.println(“当前页:”+page.getCurrent());
System.out.println(“每页显示的条数:”+page.getSize()); System.out.println(“总记录数:”+page.getTotal()); System.out.println(“总页数:”+page.getPages());
System.out.println(“是否有上一页:”+page.hasPrevious()); System.out.println(“是否有下一页:”+page.hasNext()); }

UserMapper.xml中编写SQL
c>测试
结果:
User(id=3, name=Tom, age=28, email=test3@baomidou.com, isDeleted=null) User(id=4,
name=Sandy, age=21, email=test4@baomidou.com, isDeleted=null) User(id=5, name=Billie,
age=24, email=test5@baomidou.com, isDeleted=null) User(id=8, name=ybc1, age=21,
email=null, isDeleted=null) User(id=9, name=ybc2, age=22, email=null, isDeleted=null) 当前
页:1 每页显示的条数:5 总记录数:12 总页数:3 是否有上一页:false 是否有下一页:true

id,username,age,email

–> SELECT
FROM t_user WHERE age > # {age} @Test public void testSelectPageVo()
{ //设置分页参数 Page page = new Page<>(1, 5); userMapper.selectPageVo(page, 20);
//获取分页数据 List list = page.getRecords(); list.forEach(System.out::println);
System.out.println(“当前页:”+page.getCurrent()); System.out.println(“每页显示的条数:”+page.getSize());
System.out.println(“总记录数:”+page.getTotal()); System.out.println(“总页数:”+page.getPages());
System.out.println(“是否有上一页:”+page.hasPrevious()); System.out.println(“是否有下一页:”+page.hasNext()); }

xml自定义分页
a>UserMapper中定义接口方法
@Configuration @MapperScan(“com.atguigu.mybatisplus.mapper”) //可以将主类中的注解移到此处 public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor; } }@Test public void testPage(){ //设置分页参数 Page page = new Page<>(1, 5);
userMapper.selectPage(page, null); //获取分页数据 List list = page.getRecords(); list.forEach(System.out::println);
System.out.println(“当前页:”+page.getCurrent()); System.out.println(“每页显示的条数:”+page.getSize());
System.out.println(“总记录数:”+page.getTotal()); System.out.println(“总页数:”+page.getPages());
System.out.println(“是否有上一页:”+page.hasPrevious()); System.out.println(“是否有下一页:”+page.hasNext()); }
/*** 根据年龄查询用户列表,分页显示 * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位 *
@param age 年龄 * @return */ I Page selectPageVo(@Param(“page”) Page page, @Param(“age”) Integer age);

乐观锁与悲观锁
CREATE TABLE t_product ( id BIGINT(20) NOT NULL COMMENT ‘主键ID’, NAME VARCHAR(30) NULL DEFAULT NULL COMMENT ‘商品名称’, price INT(11) DEFAULT 0 COMMENT ‘价格’, VERSION INT(11) DEFAULT 0 COMMENT ‘乐观锁版本号’, PRIMARY KEY (id) );
INSERT INTO t_product (id, NAME, price) VALUES (1, ‘外星人笔记本’, 100);
package com.atguigu.mybatisplus.entity; import lombok.Data; @Data public class Product { private Long id; private String name; private Integer price; private Integer version; }
public interface ProductMapper extends BaseMapper { }
@Test public void testConcurrentUpdate() { //1、小李 Product p1 = productMapper.selectById(1L); System.out.println(“小李取出的价格:” + p1.getPrice()); //2、小王 Product p2 = productMapper.selectById(1L); System.out.println(“小王取出的价格:” + p2.getPrice());
//3、小李将价格加了50元,存入了数据库 p1.setPrice(p1.getPrice() + 50); int result1 = productMapper.updateById(p1); System.out.println(“小李修改结果:” + result1); //4、小王将商品减了30元,存入了数据库 p2.setPrice(p2.getPrice() - 30); int result2 = productMapper.updateById(p2); System.out.println(“小王修改结果:” + result2); //最后的结果 Product p3 = productMapper.selectById(1L); //价格覆盖,最后的结果:70 System.out.println(“最后的结果:” + p3.getPrice()); }
乐观锁实现流程
数据库中添加version字段
取出记录时,获取当前version
更新时,version + 1,如果where语句中的version版本不对,则更新失败
e>Mybatis-Plus实现乐观锁
修改实体类
添加乐观锁插件配置
//3、小李将价格加了50元,存入了数据库 p1.setPrice(p1.getPrice() + 50); int result1 = productMapper.updateById(p1);
System.out.println(“小李修改结果:” + result1); //4、小王将商品减了30元,存入了数据库 p2.setPrice(p2.getPrice() - 30);
int result2 = productMapper.updateById(p2); System.out.println(“小王修改结果:” + result2); //最后的结果 Product p3 = productMapper.selectById(1L);
//价格覆盖,最后的结果:70 System.out.println(“最后的结果:” + p3.getPrice()); }
SELECT id,name,price,version FROM product WHERE id=1 UPDATE product SET price=price+50,
version=version + 1 WHERE id=1 AND version=1 package com.atguigu.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.Version; import lombok.Data; @Data public class Product
{ private Long id; private String name; private Integer price; @Version private Integer version; }

乐观锁实现流程
数据库中添加version字段
取出记录时,获取当前version
更新时,version + 1,如果where语句中的version版本不对,则更新失败
e>Mybatis-Plus实现乐观锁
修改实体类
package com.atguigu.mybatisplus.entity;
import lombok.Data; @Data public class Product{
private Long id;
private String name;
private Integer price;
private Integer version; }
添加mapper
public interface ProductMapper extends BaseMapper { }
添加乐观锁插件配置
@Test public void testConcurrentUpdate() {
//1、小李 Product p1 = productMapper.selectById(1L); System.out.println(“小李取出的价格:” + p1.getPrice());
//2、小王 Product p2 = productMapper.selectById(1L); System.out.println(“小王取出的价格:” + p2.getPrice());
//3、小李将价格加了50元,存入了数据库 p1.setPrice(p1.getPrice() + 50);
int result1 = productMapper.updateById(p1); System.out.println(“小李修改结果:” + result1);
//4、小王将商品减了30元,存入了数据库 p2.setPrice(p2.getPrice() - 30);
int result2 = productMapper.updateById(p2); System.out.println(“小王修改结果:” + result2);
//最后的结果 Product p3 = productMapper.selectById(1L);
//价格覆盖,最后的结果:70 System.out.println(“最后的结果:” + p3.getPrice()); }
SELECT id,name,price,version FROM product WHERE id=1 UPDATE product SET price=price+50, version=version + 1 WHERE id=1 AND version=1

性能分析
性能分析拦截器,用于输出每条 SQL 语句及其执行时间
SQL 性能执行分析,开发环境使用,超过指定时间,停止运行。有助于发现问题
1、配置插件
(1)参数说明
参数:maxTime: SQL 执行最大时长,超过自动停止运行,有助于发现问题。
参数:format: SQL是否格式化,默认false。
(2)在 MybatisPlusConfig 中配置
/

  • SQL 执行性能分析插件
  • 开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长
    /
    @Bean
    @Profile({“dev”,“test”})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
    PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
    performanceInterceptor.setMaxTime(100);//ms,超过此处设置的ms则sql不执行
    performanceInterceptor.setFormat(true);
    return performanceInterceptor;
    }
    (3)Spring Boot 中设置dev环境
    #环境设置:dev、test、prod
    spring.profiles.active=dev
    可以针对各环境新建不同的配置文件application-dev.properties、application-test.properties、applicationprod.properties
    也可以自定义环境名称:如test1、test2
    2、测试
    (1)常规测试
    /
    *
  • 测试 性能分析插件
    */
    @Test
    public void testPerformance() {
    User user = new User();
    user.setName(“我是Helen”);
    user.setEmail(“helen@sina.com”);
    user.setAge(18);
    userMapper.insert(user);
    }
    将maxTime 改小之后再次进行测试
    performanceInterceptor.setMaxTime(5);//ms,超过此处设置的ms不执行
    如果执行时间过长,则抛出异常:The SQL execution time is too large

代码生成器*
public class getCode {
@Test
public void main1() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty(“user.dir”);
System.out.println(projectPath);
gc.setOutputDir(projectPath + “/src/main/java”);
gc.setAuthor(“atguigu”);
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
/*

  • mp生成service层代码,默认接口名称第一个字母有 I
  • UcenterService
  • */
    gc.setServiceName(“%sService”); //去掉Service接口的首字母I
    gc.setIdType(IdType.ID_WORKER); //主键策略
    gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
    gc.setSwagger2(true);//开启Swagger2模式
    mpg.setGlobalConfig(gc);

// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(“jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8”);
dsc.setDriverName(“com.mysql.cj.jdbc.Driver”);
dsc.setUsername(“root”);
dsc.setPassword(“root”);
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(“serviceedu”); //模块名
pc.setParent(“com.atguigu”);
pc.setController(“controller”);
pc.setEntity(“entity”);
pc.setService(“service”);
pc.setMapper(“mapper”);
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude(“edu_teacher”);
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix(pc.getModuleName() + “_”); //生成实体时去掉表前缀
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain =true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);
// 6、执行
mpg.execute();
}
}

package com.atguigu.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**

  • @author

  • @since 2018/12/13
    */
    public class CodeGenerator {

    @Test
    public void run() {

     // 1、创建代码生成器
     AutoGenerator mpg = new AutoGenerator();
    
     // 2、全局配置
     GlobalConfig gc = new GlobalConfig();
     String projectPath = System.getProperty("user.dir");
     gc.setOutputDir("E:\\work\\guli_parent\\service\\service_edu" + "/src/main/java");
    
     gc.setAuthor("testjava");
     gc.setOpen(false); //生成后是否打开资源管理器
     gc.setFileOverride(false); //重新生成时文件是否覆盖
    
     //UserServie
     gc.setServiceName("%sService");	//去掉Service接口的首字母I
    
     gc.setIdType(IdType.ID_WORKER_STR); //主键策略
     gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
     gc.setSwagger2(true);//开启Swagger2模式
    
     mpg.setGlobalConfig(gc);
    
     // 3、数据源配置
     DataSourceConfig dsc = new DataSourceConfig();
     dsc.setUrl("jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8");
     dsc.setDriverName("com.mysql.cj.jdbc.Driver");
     dsc.setUsername("root");
     dsc.setPassword("root");
     dsc.setDbType(DbType.MYSQL);
     mpg.setDataSource(dsc);
    
     // 4、包配置
     PackageConfig pc = new PackageConfig();
     pc.setModuleName("eduservice"); //模块名
     //包  com.atguigu.eduservice
     pc.setParent("com.atguigu");
     //包  com.atguigu.eduservice.controller
     pc.setController("controller");
     pc.setEntity("entity");
     pc.setService("service");
     pc.setMapper("mapper");
     mpg.setPackageInfo(pc);
    
     // 5、策略配置
     StrategyConfig strategy = new StrategyConfig();
    
     strategy.setInclude("edu_teacher");
    
     strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
     strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
    
     strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
     strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
    
     strategy.setRestControllerStyle(true); //restful api风格控制器
     strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
    
     mpg.setStrategy(strategy);
    
    
     // 6、执行
     mpg.execute();
    

    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值