MyBatisPlus学习笔记

一、MyBatis-Plus简介

1、简介

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

愿景
我们的愿景是成为 MyBatis 最好的搭档,就像魂斗罗中的 1P、2P,基友搭配,效率翻倍。

2、特性

无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 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 操作智能分析阻断,也可自定义拦截规则,预防
误操作 

二、入门案例

1、创建数据库及表

1)创建表

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

2)添加数据

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

 2、创建Spring Boot工程

1)初始化工程

使用 Spring Initializr 快速初始化一个 Spring Boot 工程

2)引入依赖

<dependencies>
    <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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!--lombok用来简化实体类-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

注意:引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。 

3)配置application.yml 

spring:
  datasource:
    # 配置连接数据库信息
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 12345
    url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false

注意:

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

 3、编写代码

1)启动类

Spring Boot启动类中添加@MapperScan注解,扫描mapper包 

@SpringBootApplication
@MapperScan("com.atguigu.mybatisplus.mapper")
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }

}

2)添加实体

若实体类中的属性使用的是驼峰命名风格,而表中的字段使用的是下划线命名风格

例如实体类属性isDeleted,表中字isdeleted,此时MyBatis-Plus会自动将下划线命名风格转化为驼峰命名风格

在开发的过程中,我们经常遇到以上的问题,即实体类所对应的表都有固定的前缀,例如t_或tbl_,此时,可以使用MyBatis-Plus提供的全局配置,为实体类所对应的表名设置默认的前缀,那么就不需要在每个实体类上通过@TableName标识实体类对应的表。

mybatis-plus:
  global-config:
    db-config:
    # 配置MyBatis-Plus操作表的默认前缀
      table-prefix: t_

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

3)添加mapper

BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型

public interface UserMapper extends BaseMapper<User> {
    
}

4)测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelectList() {
        System.out.println(("----- selectAll method test ------"));
        //UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper
        //所以不填写就是无任何条件
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }
}

注意:

IDEA在 userMapper 处报错,因为找不到注入的对象,因为类是动态创建的,但是程序可以正确的执行。

为了避免报错,可以在 dao 层 的接口上添加 @Repository 注解

结果:

5)添加日志

在application.yml中配置日志输出 

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

三、基本CRUD 

1、BaseMapper

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

public interface BaseMapper<T> extends Mapper<T> {

    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
    int insert(T entity);

    /**
     * 根据 ID 删除
     *
     * @param id 主键ID
     */
    int deleteById(Serializable id);

    /**
     * 根据 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<? 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<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 条件,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    Integer 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)
     */
    <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

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

 2、insert

1)插入操作

@RunWith(SpringRunner.class)
@SpringBootTest
public class CRUDTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testInsert(){

        User user = new User();
        user.setName("Helen");
        user.setAge(18);
        user.setEmail("55317332@qq.com");

        int result = userMapper.insert(user);
        System.out.println(result); //影响的行数
        System.out.println(user); //id自动回填
    }
}

注意:数据库插入id值默认为:全局唯一id

2)主键策略

MyBatis-Plus默认的主键策略是:ID_WORKER  全局唯一ID

参考资料:分布式系统唯一ID生成方案汇总:分布式系统唯一ID生成方案汇总 - nick hao - 博客园

自增策略

要想主键自增需要配置如下主键策略​​​​​​​

  • 需要在创建数据表的时候设置主键自增
  • 实体字段中配置 @TableId(type = IdType.AUTO)
@TableId(type = IdType.AUTO)
private Long id;

要想影响所有实体的配置,可以设置全局主键配置

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

其它主键策略:分析 IdType 源码可知

@Getter
public enum IdType {
    /**
     * 数据库ID自增
     */
    AUTO(0),
    /**
     * 该类型为未设置主键类型
     */
    NONE(1),
    /**
     * 用户输入ID
     * 该类型可以通过自己注册自动填充插件进行填充
     */
    INPUT(2),

    /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
    /**
     * 全局唯一ID (idWorker)
     */
    ID_WORKER(3),
    /**
     * 全局唯一ID (UUID)
     */
    UUID(4),
    /**
     * 字符串全局唯一ID (idWorker 的字符串表示)
     */
    ID_WORKER_STR(5);

    private int key;

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

3、update

1)根据Id更新操作

注意:update时生成的sql自动是动态sql:UPDATE user SET age=? WHERE id=? 

   @Test
    public void testUpdateById(){

        User user = new User();
        user.setId(1L);
        user.setAge(28);

        int result = userMapper.updateById(user);
        System.out.println(result);

    }

2)自动填充

项目中经常会遇到一些数据,每次都使用相同的方式填充,例如记录的创建时间,更新时间等。

我们可以使用MyBatis Plus的自动填充功能,完成这些字段的赋值工作:

1. 数据库表中添加自动填充字段

在User表中添加datetime类型的新的字段 create_time、update_time

2. 实体上添加注解

@Data
public class User {
    ......
        
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
}

3.实现元对象处理器接口

注意:不要忘记添加 @Component 注解

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        LOGGER.info("start insert fill ....");
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LOGGER.info("start update fill ....");
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}

3)乐观锁

主要适用场景:当要更新一条记录的时候,希望这条记录没有被别人更新,也就是说实现线程安全的数据更新

乐观锁实现方式:

  • 取出记录时,获取当前version
  • 更新时,带上这个version
  • 执行更新时, set version = newVersion where version = oldVersion
  • 如果version不对,就更新失败

1. 数据库中添加version字段

ALTER TABLE `user` ADD COLUMN `version` INT

2. 实体类添加version字段

并添加 @Version 注解
@Version
@TableField(fill = FieldFill.INSERT)
private Integer version;

3. 元对象处理器接口添加version的insert默认值

@Override
public void insertFill(MetaObject metaObject) {
    ......
    this.setFieldValByName("version", 1, metaObject);
}

特别说明:

  • 支持的数据类型只有 int,Integer,long,Long,Date,Timestamp,LocalDateTime
  • 整数类型下 newVersion = oldVersion + 1
  • newVersion 会回写到 entity 中
  • 仅支持 updateById(id) 与 update(entity, wrapper) 方法
  • 在 update(entity, wrapper) 方法下, wrapper 不能复用!!!
4. 在 MybatisPlusConfig 中注册 Bean
创建配置类
此时可以删除主类中的 @MapperScan 扫描注解
@EnableTransactionManagement
@Configuration
@MapperScan("com.atguigu.mybatis_plus.mapper")
public class MybatisPlusConfig {

    /**
     * 乐观锁插件
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}

5.测试乐观锁可以修改成功

测试后分析打印的sql语句,将version的数值进行了加1操作

/**
 * 测试 乐观锁插件
 */
@Test
public void testOptimisticLocker() {

    //查询
    User user = userMapper.selectById(1L);
    //修改数据
    user.setName("Helen Yao");
    user.setEmail("helen@qq.com");
    //执行更新
    userMapper.updateById(user);
}

6. 测试乐观锁修改失败

/**
 * 测试乐观锁插件 失败
 */
@Test
public void testOptimisticLockerFail() {

    //查询
    User user = userMapper.selectById(1L);
    //修改数据
    user.setName("Helen Yao1");
    user.setEmail("helen@qq.com1");

    //模拟取出数据后,数据库中version实际数据比取出的值大,即已被其它线程修改并更新了version
    user.setVersion(user.getVersion() - 1);

    //执行更新
    userMapper.updateById(user);
}

4、select

1)根据id查询记录

@Test
public void testSelectById(){

    User user = userMapper.selectById(1L);
    System.out.println(user);
}

2)通过多个id批量查询

完成了动态sql的foreach的功能

@Test
public void testSelectBatchIds(){

    List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
    users.forEach(System.out::println);
}

3)简单的条件查询

通过map封装查询条件

@Test
public void testSelectByMap(){

    HashMap<String, Object> map = new HashMap<>();
    map.put("name", "Helen");
    map.put("age", 18);
    List<User> users = userMapper.selectByMap(map);

    users.forEach(System.out::println);
}

注意:map中的key对应的是数据库中的列名。例如数据库user_id,实体类是userId,这时map的key需要填写user_id

5、分页

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

1. 创建配置类

/**
 * 分页插件
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
}
2. 测试selectPage分页
测试:最终通过page对象获取相关数据

@Test
public void testSelectPage() {

    Page<User> page = new Page<>(1,5);
    userMapper.selectPage(page, null);

    page.getRecords().forEach(System.out::println);
    System.out.println(page.getCurrent());
    System.out.println(page.getPages());
    System.out.println(page.getSize());
    System.out.println(page.getTotal());
    System.out.println(page.hasNext());
    System.out.println(page.hasPrevious());
}

控制台sql语句打印:

SELECT id,name,age,email,create_time,update_time FROM user LIMIT 0,5 

3. 测试selectMapsPage分页:结果集是Map

@Test
public void testSelectMapsPage() {

    Page<User> page = new Page<>(1, 5);

    IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, null);

    //注意:此行必须使用 mapIPage 获取记录列表,否则会有数据类型转换错误
    mapIPage.getRecords().forEach(System.out::println);
    System.out.println(page.getCurrent());
    System.out.println(page.getPages());
    System.out.println(page.getSize());
    System.out.println(page.getTotal());
    System.out.println(page.hasNext());
    System.out.println(page.hasPrevious());
}

6、delete

1)根据id删除记录

@Test
public void testDeleteById(){

    int result = userMapper.deleteById(8L);
    System.out.println(result);
}

2)批量删除

@Test
public void testDeleteBatchIds() {
    int result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
    System.out.println(result);
}

3)简单的条件查询删除

@Test
public void testDeleteByMap() {

    HashMap<String, Object> map = new HashMap<>();
    map.put("name", "Helen");
    map.put("age", 18);

    int result = userMapper.deleteByMap(map);
    System.out.println(result);
}

4)逻辑删除

  • 物理删除:真实删除,将对应数据从数据库中删除,之后查询不到此条被删除数据
  • 逻辑删除:假删除,将对应数据中代表是否被删除字段状态修改为“被删除状态”,之后在数据库中仍旧能看到此条数据记录

1. 数据库中添加 deleted字段

ALTER TABLE `user` ADD COLUMN `deleted` boolean

2. 实体类添加deleted 字段

并加上 @TableLogic 注解 和 @TableField(fill = FieldFill.INSERT) 注解

@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer deleted;

3. 元对象处理器接口添加deleted的insert默认值 

@Override
public void insertFill(MetaObject metaObject) {
    ......
    this.setFieldValByName("deleted", 0, metaObject);
}

4. application.properties 加入配置

此为默认值,如果你的默认值和mp默认的一样,该配置可无

mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

5. 在 MybatisPlusConfig 中注册 Bean 

@Bean
public ISqlInjector sqlInjector() {
    return new LogicSqlInjector();
}

6. 测试逻辑删除

  • 测试后发现,数据并没有被删除,deleted字段的值由0变成了1
  • 测试后分析打印的sql语句,是一条update
  • 注意:被删除数据的deleted 字段的值必须是 0,才能被选取出来执行逻辑删除的操作
/**
 * 测试 逻辑删除
 */
@Test
public void testLogicDelete() {

    int result = userMapper.deleteById(1L);
    System.out.println(result);
}

7. 测试逻辑删除后的查询

MyBatis Plus中查询操作也会自动添加逻辑删除字段的判断

/**
 * 测试 逻辑删除后的查询:
 * 不包括被逻辑删除的记录
 */
@Test
public void testLogicDeleteSelect() {
    User user = new User();
    List<User> users = userMapper.selectList(null);
    users.forEach(System.out::println);
}

测试后分析打印的sql语句,包含 WHERE deleted=0 

SELECT id,name,age,email,create_time,update_time,deleted FROM user WHERE deleted=0

五、性能分析

性能分析拦截器,用于输出每条 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.propertiesapplication-test.propertiesapplication-prod.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);
}

2)将maxTime 改小之后再次进行测试

performanceInterceptor.setMaxTime(5);//ms,超过此处设置的ms不执行

如果执行时间过长,则抛出异常:The SQL execution time is too large, 

输出:

六、通用Service

说明:

1、通用 Service CRUD 封装IService接口,进一步封装 CRUD 采用  

get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分  Mapper 层避免混淆,
2、泛型  T 为任意实体对象
3、建议如果存在自定义通用 Service 方法的可能,请创建自己的  IBaseService 继承
Mybatis-Plus 提供的基类

1)IService

MyBatis-Plus中有一个接口 IService和其实现类 ServiceImpl,封装了常见的业务层逻辑
详情查看源码IService和ServiceImpl 

2)创建Service接口和实现类

UserService继承IService模板提供的基础功能

public interface UserService extends IService<User> {
 
}

ServiceImpl实现了IService,提供了IService中基础功能的实现,若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
 
}

3)测试查询记录数

@Autowired
private UserService userService;
@Test
public void testGetCount(){
  long count = userService.count();
  System.out.println("总记录数:" + count);
}

4)测试批量插入

SQL长度有限制,海量数据插入单条SQL无法实行,因此MP将批量插入放在了通用Service中实现,而不是通用Mapper

@Test
public void testSaveBatch(){
  ArrayList<User> 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);
}

七、条件构造器和常用接口 

1、wapper介绍

  • Wrapper : 条件构造抽象类,最顶端父类
  • AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
    •  QueryWrapper : 查询条件封装
    • UpdateWrapper : Update 条件封装
    •  AbstractLambdaWrapper : 使用Lambda 语法
      • LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
      • LambdaUpdateWrapper : Lambda 更新封装Wrapper 

2、QueryWrapper

1)组装查询条件

查询用户名包含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)
@Test
public void test01(){
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.like("username", "a")
     .between("age", 20, 30)
     .isNotNull("email");
  List<User> list = userMapper.selectList(queryWrapper);
  list.forEach(System.out::println);
}

2)组装排序条件

按年龄降序查询用户,如果年龄相同则按id升序排列

SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE
is_deleted=0 ORDER BY age DESC,id ASC
@Test
public void test02(){
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper
     .orderByDesc("age")
     .orderByAsc("id");
  List<User> users = userMapper.selectList(queryWrapper);
  users.forEach(System.out::println);
}

3)组装删除条件

删除email为空的用户

DELETE FROM t_user WHERE (email IS NULL)
@Test
public void test03(){
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.isNull("email");
  //条件构造器也可以构建删除语句的条件
  int result = userMapper.delete(queryWrapper);
  System.out.println("受影响的行数:" + result);
}

4)条件的优先级

将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改

UPDATE t_user SET age=?, email=? WHERE (username LIKE ? AND age > ? OR
email IS NULL)
@Test
public void test04() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  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);
}

lambda表达式内的逻辑优先运算 

@Test
public void test04() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper
     .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);
}

5)组装select子句

查询用户信息的username和age字段

@Test
public void test05() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.select("username", "age");
  //selectMaps()返回Map集合列表,通常配合select()使用,避免User对象中没有被查询到的列值
为null
  List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
  maps.forEach(System.out::println);
}

6)实现子查询 

查询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))
@Test
public void test06() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.inSql("id", "select id from t_user where id <= 3");
  List<User> list = userMapper.selectList(queryWrapper);
  list.forEach(System.out::println);
}

3、UpdateWrapper 

将(年龄大于20或邮箱为null)并且用户名中包含有a的用户信息修改

UPDATE t_user SET username=?, age=?,email=? WHERE (username LIKE ? AND
(age > ? OR email IS NULL))
@Test
public void test07() {
  //组装set子句以及修改条件
  UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
  //lambda表达式内的逻辑优先运算
  updateWrapper
   .set("age", 18)
   .set("email", "user@atguigu.com")
   .like("name", "a")
   .and(i -> i.gt("age", 20).or().isNull("email"));

  //这里必须要创建User对象,否则无法应用自动填充。如果没有自动填充,可以设置为null
  //User user = new User();
  //user.setName("张三");
  //int result = userMapper.update(user, updateWrapper);
  
  int result = userMapper.update(null, updateWrapper);
  System.out.println(result);
}

4、condition

在真正开发的过程中,组装条件是常见的功能,而这些条件数据来源于用户输入,是可选的,因
此我们在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,若
没有选择则一定不能组装,以免影响SQL执行的结果。

思路一:

@Test
public void test08() {
  //定义查询条件,有可能为null(用户未输入或未选择)
  String username = null;
  Integer ageBegin = 10;
  Integer ageEnd = 24;
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  
  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<User> users = userMapper.selectList(queryWrapper);
  users.forEach(System.out::println);
}

思路二:

上面的实现方案没有问题,但是代码比较复杂,我们可以使用带condition参数的重载方法构建查
询条件,简化代码的编写

@Test
public void test08UseCondition() {
  //定义查询条件,有可能为null(用户未输入或未选择)
  String username = null;
  Integer ageBegin = 10;
  Integer ageEnd = 24;
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();

  queryWrapper
     .like(StringUtils.isNotBlank(username), "username", "a")
     .ge(ageBegin != null, "age", ageBegin)
     .le(ageEnd != null, "age", ageEnd);

  List<User> users = userMapper.selectList(queryWrapper);
  users.forEach(System.out::println);
}

5、LambdaQueryWrapper

@Test
public void test09() {
  //定义查询条件,有可能为null(用户未输入)
  String username = "a";
  Integer ageBegin = 10;
  Integer ageEnd = 24;
  LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  //避免使用字符串表示字段,防止运行时错误
  queryWrapper
     .like(StringUtils.isNotBlank(username), User::getName, username)
     .ge(ageBegin != null, User::getAge, ageBegin)
     .le(ageEnd != null, User::getAge, ageEnd);
  List<User> users = userMapper.selectList(queryWrapper);
  users.forEach(System.out::println);
}

6、LambdaUpdateWrapper 

@Test
public void test10() {
  //组装set子句
  LambdaUpdateWrapper<User> 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)); 

  User user = new User();
  int result = userMapper.update(user, updateWrapper);
  System.out.println("受影响的行数:" + result);
}

七、通用枚举

表中的有些字段值是固定的,例如性别(男或女),此时我们可以使用MyBatis-Plus的通用枚举
来实现

1)数据库表添加字段sex

2)创建通用枚举类型

@Getter
public enum SexEnum {
  MALE(1, "男"),
  FEMALE(2, "女");
  @EnumValue
  private Integer sex;
  private String sexName;
  SexEnum(Integer sex, String sexName) {
    this.sex = sex;
    this.sexName = sexName;
  }
}

3)配置扫描通用枚举

mybatis-plus:
 # 配置扫描通用枚举
  type-enums-package: com.atguigu.mybatisplus.enums

4)测试

设置性别信息为枚举项,会将@EnumValue注解所标识的属性值存储到数据库

@Test
public void testSexEnum(){
  User user = new User();
  user.setName("john");
  user.setAge(20);
  user.setSex(SexEnum.MALE);
  userMapper.insert(user);
}

八、代码生成器

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、
Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

1、引入依赖

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>3.4.1</version>
</dependency>
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.31</version>
</dependency>

2、快速生成

public class AutoCode {
    public static void main(String[] args) {
    // 需要构建一个 代码自动生成器 对象
    AutoGenerator mpg = new AutoGenerator();
    // 配置策略
    // 1、全局配置
    GlobalConfig gc = new GlobalConfig();
        //当前项目的路径
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath+"/src/main/java");
    gc.setAuthor("xiaoludan");
    gc.setOpen(false);
    gc.setFileOverride(false); // 是否覆盖
    gc.setServiceName("%sService"); // 去Service的I前缀
    gc.setIdType(IdType.ID_WORKER);
    gc.setDateType(DateType.ONLY_DATE);
    gc.setSwagger2(true);
    mpg.setGlobalConfig(gc);
    //2、设置数据源
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    dsc.setUsername("root");
    dsc.setPassword("12345");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    //3、包的配置
    PackageConfig pc = new PackageConfig();
    pc.setModuleName("blog");
    pc.setParent("com.atguigu");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    mpg.setPackageInfo(pc);
    //4、策略配置
    StrategyConfig strategy = new StrategyConfig();
    // 设置要映射的表名
    strategy.setInclude("blog_tags","course","links","sys_settings","user_record"); 
                trategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel(true); // 自动lombok;
    strategy.setLogicDeleteFieldName("deleted");
    // 自动填充配置
    TableFill gmtCreate = new TableFill("cearte_time", FieldFill.INSERT);
    TableFill gmtModified = new TableFill("update_time",
                FieldFill.INSERT_UPDATE);
    ArrayList<TableFill> tableFills = new ArrayList<>();
    tableFills.add(create_time);
    tableFills.add(update_time);
    strategy.setTableFillList(tableFills);
    //乐观锁 
    strategy.setVersionFieldName("version");
    strategy.setRestControllerStyle(true);
    strategy.setControllerMappingHyphenStyle(true);
    mpg.setStrategy(strategy);
    mpg.execute(); //执行 
    }
}

九、多数据源

适用于多种场景:纯粹多库、 读写分离、 一主多从、 混合模式等

目前我们就来模拟一个纯粹多库的一个场景
场景说明:
我们创建两个库,分别为:mybatis_plus(以前的库不动)与mybatis_plus_1(新建),将
mybatis_plus库的product表移动到mybatis_plus_1库,这样每个库一张表,通过一个测试用例
分别获取用户数据与商品数据,如果获取到说明多库模拟成功

1、创建数据库及表

创建数据库mybatis_plus_1和表product

CREATE DATABASE `mybatis_plus_1`;
use `mybatis_plus_1`;
CREATE TABLE 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 product (id, NAME, price) VALUES (1, '外星人笔记本', 100);

删除mybatis_plus库product表

use mybatis_plus;
DROP TABLE IF EXISTS product;

2、引入依赖

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>3.5.0</version>
</dependency>

3、配置多数据源

spring:
 # 配置数据源信息
datasource:
 dynamic:
   # 设置默认的数据源或者数据源组,默认值即为master
  primary: master
   # 严格匹配数据源,默认false.true未匹配到指定数据源时抛异常,false使用默认数据源
  strict: false
  datasource:
   master:
    url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
   slave_1:
    url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456

4、创建用户service

public interface UserService extends IService<User> {
}
@DS("master") //指定所操作的数据源
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements
UserService {
}

5、创建商品service

public interface ProductService extends IService<Product> {
}
@DS("slave_1")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
implements ProductService {
}

6、测试

@Autowired
private UserService userService;

@Autowired
private ProductService productService;

@Test
public void testDynamicDataSource(){
  System.out.println(userService.getById(1L));
  System.out.println(productService.getById(1L));
}

结果:
1、都能顺利获取对象,则测试成功
2、如果我们实现读写分离,将写操作方法加上主库数据源,读操作方法加上从库数据源,自动切
换,是不是就能实现读写分离?

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鲁蛋儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值