当前位置: 首页 > news >正文

Mybatis-Plus复习

目录

一.简介

1.简介

2.特性

3.文档参考地址

二.环境配置

1.依赖

2.配置application

3.实体类

4.Mapper接口

三.基本CRUD

1.BaseMapper

1.增

2.删

3.改

4.查

2.IService

1.测试

2.注意

四.常用注解

1.@TableName("user")

2.@TableId

3.@TableField

4. @TableLogic

五.条件构造器和常用接口

1.wrapper(包装)介绍

 2.QueryWrapper和UpdateWrapper

1.特点

2.测试

3.修改 和条件的优先级

4.查询部分字段

5. 子查询

6.动态sql(复杂版)

7.动态sql简化版

8.lambda用法

六.插件

1.分页插件

1.配置

2.使用

3.根据自定义条件进行分页

2.乐观锁插件

1.不用锁

2.配置乐观锁

3.用了乐观锁

4.优化

七.MybatisX插件

1.MyBatisX插件用法:

2.第一个用法生成mapper和service以及实体类模板

 3.第二个用法快捷生成sql

 参考


一.简介

1.简介

MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为 简化开发、提高效率而生。

2.特性

1.无侵入:只做增强不做改变,引入它不会对现有工程产生影响,损耗小:

2.启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作 强大的 CRUD 操作:

3.内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,

4.有强大的条件构造器,满足各类使用需求 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,

5.支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由 配置,完美解决主键问题 支持

6.ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强 大的 CRUD 操作

7. 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,

8.内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等 同于普通 List 查询 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、 Postgre、SQLServer 等多种数据库

9.内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出 慢查询

10.内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防 误操作

3.文档参考地址

官方地址: http://mp.baomidou.com
代码发布地址: Github: https://github.com/baomidou/mybatis-plus Gitee: https://gitee.com/baomidou/mybatis-plus
文档发布地址: https://baomidou.com/pages/24112f 

二.环境配置

1.依赖

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

2.配置application

# 应用名称
spring.application.name=mybatisplus
#默认的数据源
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#显示Sql语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml  默认的配置
#设置实体类所对应表的同一前缀 
#mybatis-plus.global-config.db-config.table-prefix=t_
#mybatis-plus.global-config.db-config.table-underline=
#统一设置主键生成策略
#mybatis-plus.global-config.db-config.id-type=auto
#别名
mybatis-plus.type-aliases-package=top.remained.pojo



3.实体类

注:

mybatisPlus 自动配置了驼峰命名 user_name =userName

mybatisPlus  默认id就是主键

你主键设置的是递增,如果不在配置文件中主键生成策略,默认仍是雪花算法生成的id

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User(String name, Integer age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }
}

4.Mapper接口

@Mapper
public interface UserMapper  extends BaseMapper<User> {

}

三.基本CRUD

1.BaseMapper

 Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能

/*
 * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/*

               :`
                    .:,
                     :::,,.
             ::      `::::::
             ::`    `,:,` .:`
             `:: `::::::::.:`      `:';,`
              ::::,     .:::`   `@++++++++:
               ``        :::`  @+++++++++++#
                         :::, #++++++++++++++`
                 ,:      `::::::;'##++++++++++
                 .@#@;`   ::::::::::::::::::::;
                  #@####@, :::::::::::::::+#;::.
                  @@######+@:::::::::::::.  #@:;
           ,      @@########':::::::::::: .#''':`
           ;##@@@+:##########@::::::::::: @#;.,:.
            #@@@######++++#####'::::::::: .##+,:#`
            @@@@@#####+++++'#####+::::::::` ,`::@#:`
            `@@@@#####++++++'#####+#':::::::::::@.
             @@@@######+++++''#######+##';::::;':,`
              @@@@#####+++++'''#######++++++++++`
               #@@#####++++++''########++++++++'
               `#@######+++++''+########+++++++;
                `@@#####+++++''##########++++++,
                 @@######+++++'##########+++++#`
                @@@@#####+++++############++++;
              ;#@@@@@####++++##############+++,
             @@@@@@@@@@@###@###############++'
           @#@@@@@@@@@@@@###################+:
        `@#@@@@@@@@@@@@@@###################'`
      :@#@@@@@@@@@@@@@@@@@##################,
      ,@@@@@@@@@@@@@@@@@@@@################;
       ,#@@@@@@@@@@@@@@@@@@@##############+`
        .#@@@@@@@@@@@@@@@@@@#############@,
          @@@@@@@@@@@@@@@@@@@###########@,
           :#@@@@@@@@@@@@@@@@##########@,
            `##@@@@@@@@@@@@@@@########+,
              `+@@@@@@@@@@@@@@@#####@:`
                `:@@@@@@@@@@@@@@##@;.
                   `,'@@@@##@@@+;,`
                        ``...``

 _ _     /_ _ _/_. ____  /    _
/ / //_//_//_|/ /_\  /_///_/_\      Talk is cheap. Show me the code.
     _/             /
 */

/**
 * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
 * <p>这个 Mapper 支持 id 泛型</p>
 *
 * @author hubin
 * @since 2016-01-23
 */
public interface BaseMapper<T> extends Mapper<T> {

    /**
     * 插入一条记录
     *
     * @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<T> queryWrapper);

    /**
     * 删除(根据ID或实体 批量删除)
     *
     * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<?> 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<T> updateWrapper);

    /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
    T selectById(Serializable id);

    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,查询一条记录
     * <p>查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
        List<T> 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 实体对象封装操作类
     * @return
     */
    default boolean exists(Wrapper<T> queryWrapper) {
        Long count = this.selectCount(queryWrapper);
        return null != count && count > 0;
    }

    /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

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

    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     */
    <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

1.增

//可以对ioc容器自动装配
@SpringBootTest
public class MybatisPlusTest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void insert1(){
//        INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
        User user = new User("zz", 20, "123.com");
        int result = userMapper.insert(user);
        System.out.println(result);
//        虽然是自增id,但是使用的是雪花算法的id
//        1564895601187151874id
        System.out.println(user.getId()+"id");

    }

    }

2.删

注:这里写1L是因为定义的为long型

    @Test
    public void del(){
//        通过id删除 DELETE FROM user WHERE id=?
//        int i = userMapper.deleteById(20);
        1
//        System.out.println(i);
//        Map<String,Object> map = new HashMap <String,Object>();
        map里面存放的是条件 DELETE FROM user WHERE age = ?
//        map.put("age",20);
        因为年龄为20的 2个
//        int i1 = userMapper.deleteByMap(map);
//        System.out.println(i1);
//        DELETE FROM user WHERE id IN ( ? , ? )
        List<Long> list = Arrays.asList(1L, 2L);
//      批量删除
        userMapper.deleteBatchIds(list);

    }

3.改

    @Test
    public void update(){
//        UPDATE user SET name=?, age=?, email=? WHERE id=?
//   必须对象里存放Id  要不然白给 因为没Id 所以不更改
        User user =new User("zz",18,"456.com");
        userMapper.updateById(user);
        User user1 = new User();
        user1.setAge(16);
        user1.setId(3L);
//         UPDATE user SET age=? WHERE id=?  只改对象里有的数据,没有的字段原值不变
        userMapper.updateById(user1);
    }

4.查

    @Test
    public void sel(){
//        通过id查询 SELECT id,name,age,email FROM user WHERE id=?
        userMapper.selectById(3L);
//        通过map集合中的条件查询 SELECT id,name,age,email FROM user WHERE age = ?
        Map<String,Object> map = new HashMap <String,Object>();
//        条件为 age=16
        map.put("age",16);
//
        userMapper.selectByMap(map);
//        批量查询 SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
        List<Long> list = Arrays.asList(3L,4L,5L);
        userMapper.selectBatchIds(list);
        //        通过条件查询一个list集合,若没有条件 则可以设置为null 即查询所有
       userMapper.selectList(null);
//       调用自己写的 select * from user where id=?
        userMapper.selectMapById(3L);
    }

2.IService

顶级 Service

/*
 * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baomidou.mybatisplus.extension.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.ChainUpdate;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 顶级 Service
 *
 * @author hubin
 * @since 2018-06-23
 */
public interface IService<T> {

    /**
     * 默认批次提交数量
     */
    int DEFAULT_BATCH_SIZE = 1000;

    /**
     * 插入一条记录(选择字段,策略插入)
     *
     * @param entity 实体对象
     */
    default boolean save(T entity) {
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean saveBatch(Collection<T> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     */
    boolean saveBatch(Collection<T> entityList, int batchSize);

    /**
     * 批量修改插入
     *
     * @param entityList 实体对象集合
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean saveOrUpdateBatch(Collection<T> entityList) {
        return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    /**
     * 批量修改插入
     *
     * @param entityList 实体对象集合
     * @param batchSize  每次的数量
     */
    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

    /**
     * 根据 ID 删除
     *
     * @param id 主键ID
     */
    default boolean removeById(Serializable id) {
        return SqlHelper.retBool(getBaseMapper().deleteById(id));
    }

    /**
     * 根据 ID 删除
     *
     * @param id      主键(类型必须与实体类型字段保持一致)
     * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
     * @return 删除结果
     * @since 3.5.0
     */
    default boolean removeById(Serializable id, boolean useFill) {
        throw new UnsupportedOperationException("不支持的方法!");
    }

    /**
     * 根据实体(ID)删除
     *
     * @param entity 实体
     * @since 3.4.4
     */
    default boolean removeById(T entity) {
        return SqlHelper.retBool(getBaseMapper().deleteById(entity));
    }

    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    default boolean removeByMap(Map<String, Object> columnMap) {
        Assert.notEmpty(columnMap, "error: columnMap must not be empty");
        return SqlHelper.retBool(getBaseMapper().deleteByMap(columnMap));
    }

    /**
     * 根据 entity 条件,删除记录
     *
     * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default boolean remove(Wrapper<T> queryWrapper) {
        return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
    }

    /**
     * 删除(根据ID 批量删除)
     *
     * @param list 主键ID或实体列表
     */
    default boolean removeByIds(Collection<?> list) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
    }

    /**
     * 批量删除
     *
     * @param list    主键ID或实体列表
     * @param useFill 是否填充(为true的情况,会将入参转换实体进行delete删除)
     * @return 删除结果
     * @since 3.5.0
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean removeByIds(Collection<?> list, boolean useFill) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        if (useFill) {
            return removeBatchByIds(list, true);
        }
        return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
    }

    /**
     * 批量删除(jdbc批量提交)
     *
     * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
     * @return 删除结果
     * @since 3.5.0
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean removeBatchByIds(Collection<?> list) {
        return removeBatchByIds(list, DEFAULT_BATCH_SIZE);
    }

    /**
     * 批量删除(jdbc批量提交)
     *
     * @param list    主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
     * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
     * @return 删除结果
     * @since 3.5.0
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
        return removeBatchByIds(list, DEFAULT_BATCH_SIZE, useFill);
    }

    /**
     * 批量删除(jdbc批量提交)
     *
     * @param list      主键ID或实体列表
     * @param batchSize 批次大小
     * @return 删除结果
     * @since 3.5.0
     */
    default boolean removeBatchByIds(Collection<?> list, int batchSize) {
        throw new UnsupportedOperationException("不支持的方法!");
    }

    /**
     * 批量删除(jdbc批量提交)
     *
     * @param list      主键ID或实体列表
     * @param batchSize 批次大小
     * @param useFill   是否启用填充(为true的情况,会将入参转换实体进行delete删除)
     * @return 删除结果
     * @since 3.5.0
     */
    default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
        throw new UnsupportedOperationException("不支持的方法!");
    }

    /**
     * 根据 ID 选择修改
     *
     * @param entity 实体对象
     */
    default boolean updateById(T entity) {
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    /**
     * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
     *
     * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
     */
    default boolean update(Wrapper<T> updateWrapper) {
        return update(null, updateWrapper);
    }

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象
     * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
     */
    default boolean update(T entity, Wrapper<T> updateWrapper) {
        return SqlHelper.retBool(getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 根据ID 批量更新
     *
     * @param entityList 实体对象集合
     */
    @Transactional(rollbackFor = Exception.class)
    default boolean updateBatchById(Collection<T> entityList) {
        return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
    }

    /**
     * 根据ID 批量更新
     *
     * @param entityList 实体对象集合
     * @param batchSize  更新批次数量
     */
    boolean updateBatchById(Collection<T> entityList, int batchSize);

    /**
     * TableId 注解存在更新记录,否插入一条记录
     *
     * @param entity 实体对象
     */
    boolean saveOrUpdate(T entity);

    /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
    default T getById(Serializable id) {
        return getBaseMapper().selectById(id);
    }

    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表
     */
    default List<T> listByIds(Collection<? extends Serializable> idList) {
        return getBaseMapper().selectBatchIds(idList);
    }

    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    default List<T> listByMap(Map<String, Object> columnMap) {
        return getBaseMapper().selectByMap(columnMap);
    }

    /**
     * 根据 Wrapper,查询一条记录 <br/>
     * <p>结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")</p>
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default T getOne(Wrapper<T> queryWrapper) {
        return getOne(queryWrapper, true);
    }

    /**
     * 根据 Wrapper,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     * @param throwEx      有多个 result 是否抛出异常
     */
    T getOne(Wrapper<T> queryWrapper, boolean throwEx);

    /**
     * 根据 Wrapper,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    Map<String, Object> getMap(Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     * @param mapper       转换函数
     */
    <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

    /**
     * 查询总记录数
     *
     * @see Wrappers#emptyWrapper()
     */
    default long count() {
        return count(Wrappers.emptyWrapper());
    }

    /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default long count(Wrapper<T> queryWrapper) {
        return SqlHelper.retCount(getBaseMapper().selectCount(queryWrapper));
    }

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default List<T> list(Wrapper<T> queryWrapper) {
        return getBaseMapper().selectList(queryWrapper);
    }

    /**
     * 查询所有
     *
     * @see Wrappers#emptyWrapper()
     */
    default List<T> list() {
        return list(Wrappers.emptyWrapper());
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
        return getBaseMapper().selectPage(page, queryWrapper);
    }

    /**
     * 无条件翻页查询
     *
     * @param page 翻页对象
     * @see Wrappers#emptyWrapper()
     */
    default <E extends IPage<T>> E page(E page) {
        return page(page, Wrappers.emptyWrapper());
    }

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
        return getBaseMapper().selectMaps(queryWrapper);
    }

    /**
     * 查询所有列表
     *
     * @see Wrappers#emptyWrapper()
     */
    default List<Map<String, Object>> listMaps() {
        return listMaps(Wrappers.emptyWrapper());
    }

    /**
     * 查询全部记录
     */
    default List<Object> listObjs() {
        return listObjs(Function.identity());
    }

    /**
     * 查询全部记录
     *
     * @param mapper 转换函数
     */
    default <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return listObjs(Wrappers.emptyWrapper(), mapper);
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default List<Object> listObjs(Wrapper<T> queryWrapper) {
        return listObjs(queryWrapper, Function.identity());
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     * @param mapper       转换函数
     */
    default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
        return getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
        return getBaseMapper().selectMapsPage(page, queryWrapper);
    }

    /**
     * 无条件翻页查询
     *
     * @param page 翻页对象
     * @see Wrappers#emptyWrapper()
     */
    default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
        return pageMaps(page, Wrappers.emptyWrapper());
    }

    /**
     * 获取对应 entity 的 BaseMapper
     *
     * @return BaseMapper
     */
    BaseMapper<T> getBaseMapper();

    /**
     * 获取 entity 的 class
     *
     * @return {@link Class<T>}
     */
    Class<T> getEntityClass();

    /**
     * 以下的方法使用介绍:
     *
     * 一. 名称介绍
     * 1. 方法名带有 query 的为对数据的查询操作, 方法名带有 update 的为对数据的修改操作
     * 2. 方法名带有 lambda 的为内部方法入参 column 支持函数式的
     * 二. 支持介绍
     *
     * 1. 方法名带有 query 的支持以 {@link ChainQuery} 内部的方法名结尾进行数据查询操作
     * 2. 方法名带有 update 的支持以 {@link ChainUpdate} 内部的方法名为结尾进行数据修改操作
     *
     * 三. 使用示例,只用不带 lambda 的方法各展示一个例子,其他类推
     * 1. 根据条件获取一条数据: `query().eq("column", value).one()`
     * 2. 根据条件删除一条数据: `update().eq("column", value).remove()`
     *
     */

    /**
     * 链式查询 普通
     *
     * @return QueryWrapper 的包装类
     */
    default QueryChainWrapper<T> query() {
        return ChainWrappers.queryChain(getBaseMapper());
    }

    /**
     * 链式查询 lambda 式
     * <p>注意:不支持 Kotlin </p>
     *
     * @return LambdaQueryWrapper 的包装类
     */
    default LambdaQueryChainWrapper<T> lambdaQuery() {
        return ChainWrappers.lambdaQueryChain(getBaseMapper());
    }

    /**
     * 链式查询 lambda 式
     * kotlin 使用
     *
     * @return KtQueryWrapper 的包装类
     */
    default KtQueryChainWrapper<T> ktQuery() {
        return ChainWrappers.ktQueryChain(getBaseMapper(), getEntityClass());
    }

    /**
     * 链式查询 lambda 式
     * kotlin 使用
     *
     * @return KtQueryWrapper 的包装类
     */
    default KtUpdateChainWrapper<T> ktUpdate() {
        return ChainWrappers.ktUpdateChain(getBaseMapper(), getEntityClass());
    }

    /**
     * 链式更改 普通
     *
     * @return UpdateWrapper 的包装类
     */
    default UpdateChainWrapper<T> update() {
        return ChainWrappers.updateChain(getBaseMapper());
    }

    /**
     * 链式更改 lambda 式
     * <p>注意:不支持 Kotlin </p>
     *
     * @return LambdaUpdateWrapper 的包装类
     */
    default LambdaUpdateChainWrapper<T> lambdaUpdate() {
        return ChainWrappers.lambdaUpdateChain(getBaseMapper());
    }

    /**
     * <p>
     * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
     * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
     * </p>
     *
     * @param entity 实体对象
     */
    default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
        return update(entity, updateWrapper) || saveOrUpdate(entity);
    }
}

自己的接口应该定义为

public interface UserService extends IService<User> {
}

其实现类写成

@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService  {
    //<UserMapper, User> 第一个要操作的Mapper接口 第二个要操作的类型
}

1.测试

@SpringBootTest
public class MybatisPlusServiceTest {
    @Autowired
    UserService userService;
    @Test
    public void count(){
//        总记录数版本3.3 SELECT COUNT( 1 ) FROM user
//        版本3.5SELECT COUNT( * ) FROM user
        long count = userService.count();
        System.out.println(count);
    }
    @Test
    public void insert(){
//        mapper里面并没有批量添加 原因:sql语句长
//        save是保存  saveOrUpdate既有修改又有添加
//        区分:看实体类中有无id有id就是修改 没id就是添加
        List<User> list  = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            User user = new User();
            user.setAge(16+i);
            user.setName("gg"+i);
            list.add(user);
        }
//        INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
//        为什么不是批量添加sql 它是基于BaseMapper实现的  它是基于单个循环添加
        boolean b = userService.saveBatch(list);

    }
}

2.注意

insert变为save

saveOrUpdate 既有修改又有添加

区分:看实体类中有无id有id就是修改 没id就是添加

四.常用注解

1.@TableName("user")

实体类所对应的数据库表名 "user"
/*
 * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baomidou.mybatisplus.annotation;

import java.lang.annotation.*;

/**
 * 数据库表相关
 *
 * @author hubin, hanchunlin
 * @since 2016-01-23
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface TableName {

    /**
     * 实体对应的表名
     */
    String value() default "";

    /**
     * schema
     * <p>
     * 配置此值将覆盖全局配置的 schema
     *
     * @since 3.1.1
     */
    String schema() default "";

    /**
     * 是否保持使用全局的 tablePrefix 的值
     * <p> 只生效于 既设置了全局的 tablePrefix 也设置了上面 {@link #value()} 的值 </p>
     * <li> 如果是 false , 全局的 tablePrefix 不生效 </li>
     *
     * @since 3.1.1
     */
    boolean keepGlobalPrefix() default false;

    /**
     * 实体映射结果集,
     * 只生效于 mp 自动注入的 method
     */
    String resultMap() default "";

    /**
     * 是否自动构建 resultMap 并使用,
     * 只生效于 mp 自动注入的 method,
     * 如果设置 resultMap 则不会进行 resultMap 的自动构建并注入,
     * 只适合个别字段 设置了 typeHandler 或 jdbcType 的情况
     *
     * @since 3.1.2
     */
    boolean autoResultMap() default false;

    /**
     * 需要排除的属性名
     *
     * @since 3.3.1
     */
    String[] excludeProperty() default {};
}

注:如果每个表有前缀可以在配置文件中 统一配置

#设置实体类所对应表的同一前缀
#mybatis-plus.global-config.db-config.table-prefix=t_

2.@TableId

将属性所对应的字段指定为主键 value=数据库中的字段

不指定type默认id生成是雪花算法 并不是递增

如果自己主动设置id 主动设置的优先

public @interface TableId {

    /**
     * 字段名(该值可无)
     */
    String value() default "";

    /**
     * 主键类型
     * {@link IdType}
     */
    IdType type() default IdType.NONE;
}

类型可以为

import lombok.Getter;

/**
 * 生成ID类型枚举类
 *
 * @author hubin
 * @since 2015-11-10
 */
@Getter
public enum IdType {
    /**
     * 数据库ID自增
     * <p>该类型请确保数据库设置了 ID自增 否则无效</p>
     */
    AUTO(0),
    /**
     * 该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
     */
    NONE(1),
    /**
     * 用户输入ID
     * <p>该类型可以通过自己注册自动填充插件进行填充</p>
     */
    INPUT(2),

    /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
    /**
     * 分配ID (主键类型为number或string),
     * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(雪花算法)
     *
     * @since 3.3.0
     */
    ASSIGN_ID(3),
    /**
     * 分配UUID (主键类型为 string)
     * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(UUID.replace("-",""))
     */
    ASSIGN_UUID(4);

    private final int key;

    IdType(int key) {
        this.key = key;
    }
}

雪花算法(粗略)

不可能只有一张表 储存的数量太多 因此进行表的拆分   
核心优点 保证拆分时不同表的主键的不重复性 主键有序性(后添加的id>之前添加的ID)

也可以在全局配置中统一配置

#统一设置主键生成策略
mybatis-plus.global-config.db-config.id-type=auto

3.@TableField

注:mybatisplus自动配置了驼峰        

普通字段对应的数据中的列名
public @interface TableField {

    /**
     * 数据库字段值
     * <p>
     * 不需要配置该值的情况:
     * <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 true 时,
     * (mp下默认是true,mybatis默认是false), 数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase() </li>
     * <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 false 时,
     * 数据库字段值.toUpperCase() == 实体属性名.toUpperCase() </li>
     */
    String value() default "";

    /**
     * 是否为数据库表字段
     * <p>
     * 默认 true 存在,false 不存在
     */
    boolean exist() default true;

    /**
     * 字段 where 实体查询比较条件
     * <p>
     * 默认 {@link SqlCondition#EQUAL}
     */
    String condition() default "";

    /**
     * 字段 update set 部分注入, 该注解优于 el 注解使用
     * <p>
     * 例1:@TableField(.. , update="%s+1") 其中 %s 会填充为字段
     * 输出 SQL 为:update 表 set 字段=字段+1 where ...
     * <p>
     * 例2:@TableField(.. , update="now()") 使用数据库时间
     * 输出 SQL 为:update 表 set 字段=now() where ...
     */
    String update() default "";

    /**
     * 字段验证策略之 insert: 当insert操作时,该字段拼接insert语句时的策略
     * <p>
     * IGNORED: 直接拼接 insert into table_a(column) values (#{columnProperty});
     * NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
     * NOT_EMPTY: insert into table_a(<if test="columnProperty != null and columnProperty!=''">column</if>) values (<if test="columnProperty != null and columnProperty!=''">#{columnProperty}</if>)
     * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
     *
     * @since 3.1.2
     */
    FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;

    /**
     * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
     * <p>
     * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
     * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if>
     * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
     * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
     *
     * @since 3.1.2
     */
    FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;

    /**
     * 字段验证策略之 where: 表示该字段在拼接where条件时的策略
     * <p>
     * IGNORED: 直接拼接 column=#{columnProperty}
     * NOT_NULL: <if test="columnProperty != null">column=#{columnProperty}</if>
     * NOT_EMPTY: <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
     * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
     *
     * @since 3.1.2
     */
    FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;

    /**
     * 字段自动填充策略
     * <p>
     * 在对应模式下将会忽略 insertStrategy 或 updateStrategy 的配置,等于断言该字段必有值
     */
    FieldFill fill() default FieldFill.DEFAULT;

    /**
     * 是否进行 select 查询
     * <p>
     * 大字段可设置为 false 不加入 select 查询范围
     */
    boolean select() default true;

    /**
     * 是否保持使用全局的 columnFormat 的值
     * <p>
     * 只生效于 既设置了全局的 columnFormat 也设置了上面 {@link #value()} 的值
     * 如果是 false , 全局的 columnFormat 不生效
     *
     * @since 3.1.1
     */
    boolean keepGlobalFormat() default false;

    /**
     * {@link ResultMapping#property} and {@link ParameterMapping#property}
     *
     * @since 3.4.4
     */
    String property() default "";

    /**
     * JDBC类型 (该默认值不代表会按照该值生效),
     * 只生效于 mp 自动注入的 method,
     * 建议配合 {@link TableName#autoResultMap()} 一起使用
     * <p>
     * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
     *
     * @since 3.1.2
     */
    JdbcType jdbcType() default JdbcType.UNDEFINED;

    /**
     * 类型处理器 (该默认值不代表会按照该值生效),
     * 只生效于 mp 自动注入的 method,
     * 建议配合 {@link TableName#autoResultMap()} 一起使用
     * <p>
     * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
     *
     * @since 3.1.2
     */
    Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;

    /**
     * 只在使用了 {@link #typeHandler()} 时判断是否辅助追加 javaType
     * <p>
     * 一般情况下不推荐使用
     * {@link ParameterMapping#javaType}
     *
     * @since 3.4.0 @2020-07-23
     */
    boolean javaType() default false;

    /**
     * 指定小数点后保留的位数,
     * 只生效于 mp 自动注入的 method,
     * 建议配合 {@link TableName#autoResultMap()} 一起使用
     * <p>
     * {@link ParameterMapping#numericScale}
     *
     * @since 3.1.2
     */
    String numericScale() default "";
}

4. @TableLogic

public @interface TableLogic {

    /**
     * 默认逻辑未删除值(该值可无、会自动获取全局配置)
     */
    String value() default "";

    /**
     * 默认逻辑删除值(该值可无、会自动获取全局配置)
     */
    String delval() default "";
}
逻辑删除的字段 数据库中也要添加    删除变为修改,只不过你查不出来 因为默认查询时会有where id_del=0

五.条件构造器和常用接口

1.wrapper(包装)介绍

 

 Wrapper : 条件构造抽象类,最顶端父类

AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件

QueryWrapper : 查询条件封装

UpdateWrapper : Update 条件封装

AbstractLambdaWrapper : 使用Lambda 语法

LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper

LambdaUpdateWrapper : Lambda 更新封装Wrappe

 2.QueryWrapper和UpdateWrapper

1.特点

1.对象封装操作类

2.用于组装查询条件

3.调每一个方法返回值都是用的QueryWrapper<User>(链式结构)

4.删除和查询都是用QueryWrapper

5. 默认条件之间拼接and

6.lambda条件表达式优先执行

2.测试

    @Autowired
    UserMapper userMapper;

    @Test
    public void t1(){

        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        ge>= le<=
//        第一个是字段名    调每一个方法返回值都是用的QueryWrapper<User>
//        所以是链式结构
        //        用户名包含a,年龄在20-30之间,邮箱不为空
        QueryWrapper<User> w = wrapper.like("name", "a")
                .between("age", 20, 30)
                .isNotNull("email");
//SELECT id,name,age,email FROM user 
//WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
//        %a%(String), 20(Integer), 30(Integer)
         userMapper.selectList(w);

    }
    @Test
    public void t2(){

        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        查询用户信息 按照年龄降序排, 年龄相同id升序
//        SELECT id,name,age,email FROM user ORDER BY age DESC,id ASC
        wrapper.orderByDesc("age")
                .orderByAsc("id");
        userMapper.selectList(wrapper);
    }
//    删除和查询都是用QueryWrapper
    @Test
    public void del(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        DELETE FROM user WHERE (age IS NULL AND email IS NULL)
        QueryWrapper<User> w = wrapper.isNull("age").isNull("email");
        userMapper.delete(w);
    }

3.修改 和条件的优先级

  @Test
    public void update(){
//        2个参数 第一个为修改为的实体类 第二个是修改条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//         UPDATE user SET name=?, age=?, email=? WHERE (name LIKE ? OR age BETWEEN ? AND ?)
        User user = new User("mm",21,"789.com");
      wrapper.like("name", "a").or().between("age", 20, 30);
        userMapper.update(user,wrapper);
        //        1个参数  即第一个设置为null 第二个即是条件也是要修改的内容
//        UPDATE user SET name=? WHERE (name LIKE ?)
        UpdateWrapper<User> wrapper1 = new UpdateWrapper<>();
        wrapper1.set("name","hh").like("name","a");
        userMapper.update(null,wrapper1);


    }
    @Test
    public void grade(){
        //        默认条件之间拼接and
//        用户名包含a 且(年龄大于20或邮箱为null)
//        lambda条件表达式优先执行
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
//        UPDATE user SET name=?, age=?, email=? 
//        WHERE (name LIKE ? AND (age > ? OR email IS NULL))
//
        wrapper.like("name","a")
                .and(i->i.gt("age",20).or().isNull("email"));
        userMapper.update(new User("mm",23,"147.com"),wrapper);

    }

4.查询部分字段

    @Test
    public void partColumn(){
//        查询部分字段
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        SELECT name,age FROM user
        wrapper.select("name","age");
        userMapper.selectList(wrapper);
    }

5. 子查询

    @Test
    public void sonSelect(){
//        子查询    id <=100
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        第一个参数 字段名 第二个 参数 条件
//        SELECT id,name,age,email FROM user 
//        WHERE (age IN (select id from user where id<=100))
        wrapper.inSql("age","select id from user where id<=100");
        userMapper.selectList(wrapper);

    }

6.动态sql(复杂版)

    @Test
    public void judge(){
//        动态sql

        String name="";
        Integer ageBegin = 20;
        Integer ageEnd = 40;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        SELECT id,name,age,email FROM user WHERE (age >= ? AND age <= ?)
        if (StringUtils.isNotBlank(name)){
//            isNotBlank 判断是否不为空 不为null 不为空白符
            wrapper.like("name",name);
        }
        if (ageBegin !=null){
            wrapper.ge("age",20);
        }
        if (ageEnd !=null){
            wrapper.le("age",ageEnd);
        }
        userMapper.selectList(wrapper);
    }

7.动态sql简化版

通过条件判断的condition
    @Test
    public void simple(){
//        通过条件判断的condition condition 为true执行 反之不执行该条件
        String name="";
        Integer ageBegin = null;
        Integer ageEnd = 40;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//         SELECT id,name,age,email FROM user WHERE (age <= ?)
//        StringUtils 封装了很多关于String的判断方法
        wrapper.like(StringUtils.isNotBlank(name),"name",name)
                .ge(ageBegin != null,"age",ageBegin)
                .le(ageEnd != null,"age",ageEnd);
        userMapper.selectList(wrapper);
    }

8.lambda用法

    @Test
    public void lambda(){
        String name="";
        Integer ageBegin = 20;
        Integer ageEnd = 40;
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        好处 本来是数据库中的字段 现在通过lambda表达式变为实体类中的属性 防止字段写错
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),User::getName,name)
                .ge(ageBegin != null,User::getAge,ageBegin)
                .le(ageEnd != null,User::getAge,ageEnd);
    }

六.插件

1.分页插件

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

注:实现原理就是先查询所有数据然后拦截第一页的数据进行显示

1.配置

@Configuration
public class MybatisPlusConfig {
//    配置分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        数据库之间的分页不相同 DbType.MYSQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        return interceptor;
    }
}

2.使用

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;
    @Test
    public void t1(){
        Page<User> page = new Page<>(1,3);
//        SELECT id,name,age,email FROM user LIMIT ?
//        只有一个参数,因为是第一页
        userMapper.selectPage(page,null);
        System.out.println(page);
    }

3.根据自定义条件进行分页

虽然Sql语句不需要Page,但是如果想用MybatisPlus提供的分页
​​​​​​​1.参数中就必须有Page<User> page, 且放在第一个参数
2.返回值必须是page

1.在Mapper接口里

@Mapper
public interface UserMapper  extends BaseMapper<User> {

//    自定义分页插件 通过年龄查出用户信息并分页
//    虽然Sql语句不需要Page,但是如果想用MybatisPlus提供的分页
//    1.参数中就必须有Page<User> page, 且放在第一个参数
//    2.返回值必须是page
    Page<User> mySelectPage(@Param("page") Page<User> page, 
                    @Param("age") Integer age);
}

2.在xml文件中直接sql的筛选条件

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.remained.mapper.UserMapper">

        <select id="mySelectPage" resultType="User">
            select  id,name,age,email from user where age > #{age}
        </select>

</mapper>

3.使用

    @Test
    public void t2(){
//      SELECT COUNT(*) AS total FROM user WHERE age > ?
//      select id,name,age,email from user where age > ? LIMIT ?,?
        Page<User> page = new Page<User>(2,3);
        userMapper.mySelectPage(page,18);

    }

2.乐观锁插件

乐观锁:

每次修改之后会核对一下之前的版本号,如果相同修改之后保存,反之进行回滚。

1.不用锁

   @Test
    public void te1(){
//       不用锁 出现问题
//        查询价格
        Product product = productMapper.selectById(1);
        System.out.println(product.getPrice() + ":小李查询");
//        100:小李查询
        Product product1 = productMapper.selectById(1);
//        100:小王查询
        System.out.println(product1.getPrice() + ":小王查询");
//        小李增加50
        product.setPrice(product.getPrice()+50);
//        150
        productMapper.updateById(product);
//        小王-30
        product1.setPrice(product1.getPrice() -30);
//        70
        productMapper.updateById(product1);
//        查询改变后的
        System.out.println(productMapper.selectById(1).getPrice()+":最后的");
//                70

    }

2.配置乐观锁

1.标识乐观锁字段号

2. 配置乐观锁插件

@Configuration
public class MybatisPlusConfig {
//    配置分页插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        数据库之间的分页不相同
        interceptor.addInnerInterceptor
(new PaginationInnerInterceptor(DbType.MYSQL));
//        添加乐观锁机制
        interceptor.addInnerInterceptor
(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}

3.用了乐观锁

@Test
    public void te2(){
        Product product = productMapper.selectById(1);
        System.out.println(product.getPrice() + ":小李查询");
//        100:小李查询
        Product product1 = productMapper.selectById(1);
//        100:小王查询
        System.out.println(product1.getPrice() + ":小王查询");
//        小李增加50
        product.setPrice(product.getPrice()+50);
//        150
        productMapper.updateById(product);
//        UPDATE t_product SET name=?, price=?, 
//        version=? WHERE id=? AND version=?
//        小王-30
        product1.setPrice(product1.getPrice() -30);
//        70
        productMapper.updateById(product1);
//        查询改变后的
        System.out.println(productMapper.selectById(1)
            .getPrice()+":最后的");
//                150
//  最后version为1
    }

4.优化

小王之所以不成功 版本号不一致

解决:如果获取更行后的返回值=0 重新查询 重新更改

    @Test
    public void te3(){
        Product product = productMapper.selectById(1);
        Product product1 = productMapper.selectById(1);
        product.setPrice(product.getPrice()+50);
        productMapper.updateById(product);
        product1.setPrice(product1.getPrice() -30);
        int i = productMapper.updateById(product1);
        if (i == 0) {
            product1 = productMapper.selectById(1);
            product1.setPrice(product1.getPrice() -30);
            productMapper.updateById(product1);

        }
        System.out.println(productMapper.selectById(1).
        getPrice()+":最后的");
//        120 版本号改变2次
    }

七.MybatisX插件

1.MyBatisX插件用法:

https://baomidou.com/pages/ba5b24/

2.第一个用法生成mapper和service以及实体类模板

环境配置

idea连接上数据库

1.选中生成的表 

2.生成实体类的配置

3.生成service和mapper的配置

 

4.

 3.第二个用法快捷生成sql

 

 参考

https://baomidou.com/pages/ba5b24/

 

相关文章:

  • 8、JAVA入门——switch选择结构
  • Inno Setup 创建Revit安装包
  • Windows和Linux使用FRP实现内网穿透
  • c++代码如何实现在win/linux下创建编译及部署后台服务,并管理其他服务
  • UI 自动化测试应不应该投入?有没有前途?怎样做最明智?
  • 股票量化交易有什么优势?
  • 元宇宙电商-NFG系统,是如何用数字藏品平台,促进新营销的?
  • thunderbird102编译-环境搭建(1)
  • curl用法:查看响应时间
  • 房地产基础知识!!!
  • 写一个简单食用的拦截器
  • 算法竞赛进阶指南 0x68 二分图的匹配
  • 【无标题】数字ic设计|ic芯片设计全流程
  • Stable Diffusion搭建全过程记录,生成自己的专属艺术照
  • 【iOS自动化测试】第二章:环境安装
  • 08.Android之View事件问题
  • Angular 响应式表单 基础例子
  • C学习-枚举(九)
  • Java 多线程编程之:notify 和 wait 用法
  • JavaScript设计模式系列一:工厂模式
  • js学习笔记
  • PaddlePaddle-GitHub的正确打开姿势
  • 观察者模式实现非直接耦合
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 说说动画卡顿的解决方案
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 小李飞刀:SQL题目刷起来!
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 做一名精致的JavaScripter 01:JavaScript简介
  • raise 与 raise ... from 的区别
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #{}和${}的区别是什么 -- java面试
  • #laravel 通过手动安装依赖PHPExcel#
  • (Java)【深基9.例1】选举学生会
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (篇九)MySQL常用内置函数
  • (学习日记)2024.02.29:UCOSIII第二节
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (转)shell调试方法
  • (转)菜鸟学数据库(三)——存储过程
  • (转)母版页和相对路径
  • (转)使用VMware vSphere标准交换机设置网络连接
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .net 无限分类
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .NET连接数据库方式
  • /bin/bash^M: bad interpreter: No such file or directory
  • @Autowired自动装配