前面我们将了Spring Boot集成Mybatis相关的操作,而在Mybatis-plus则是基于Mybatis进行了更加丰富的基础功能提供和封装,比如预置了大量的默认方法以及分页组件。

比如其中提供的BaseMapper,用于其他业务Mapper的集成接口变定义了如下常见的功能的接口:

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 wrapper 实体对象封装操作类(可以为 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    /**
     * 删除(根据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);
}

通过上述预置的接口及实现,我们可以节省大量的时间,减少大量重复代码的编写,本篇文章就带大家了解一下如何在spring boot中集成mybatis-plus。

依赖引入

mybatis-plus本身提供了spring boot的starter,注意这里依旧不是springboot官方提供的。

相关核心依赖引入如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 引入mybatisPlus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1</version>
    </dependency>

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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </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>
</dependencies>

这里重点是mybatis-plus-boot-starter,其他的引入依赖与mybatis相似,但没有引入mybatis对应的starter。

数据库配置

在application配置文件中进行数据库的配置:

spring.datasource.url=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=genesis_123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

依旧采用spring的datasource配置。

开启Mapper接口扫描

在启动类上添加@MapperScan来指定Mapper接口对应的包路径。

@SpringBootApplication
@MapperScan("com.secbro.mapper")
public class SpringBootMainApplication {

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

}

MybatisPlus分页插件配置

这里主要演示分页功能使用,因此需要通过单独的配置类来配置分页组件:

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

实体类与Mapper接口

实体类如下,注意如果类名与数据库表名不一致使用@TableName指定数据库表名。

@Data
@TableName("tb_order")
public class Order {

    private int id;

    private String orderNo;

    private int amount;
}

Mapper接口定义如下,集成BaseMapper,这样便拥有了上面展示的BaseMapper的对应方法功能。

public interface OrderMapper extends BaseMapper<Order> {

}

此时,不用再写任何代码便可以注入OrderMapper,调用一系列默认的增产改查及分页功能了。

单元测试

这里以分页功能的使用为例来进行单元测试的演示:

@Slf4j
@SpringBootTest
class OrderMapperTest {

    @Resource
    private OrderMapper orderMapper;

    @Test
    void queryByPage() {
        //参数一是当前页,参数二是每页个数
        IPage<Order> orderPage = new Page<>(1, 2);
        orderPage = orderMapper.selectPage(orderPage, null);
        List<Order> list = orderPage.getRecords();
        for (Order order : list) {
            System.out.println(order);
        }
    }

}

当然,在实践中OrderMapper是要注入到对应service中进行使用的。

执行单元测试,发现成功查询数据库中记录并打印,分页功能正常使用。这里Page有多个构造参数,也会返回各类数据,比如分页总数,总条数等信息。大家可按照该思路调用对应方法尝试使用。

精品SpringBoot 2.x视频教程

《Spring Boot 2.x 视频教程全家桶》,精品Spring Boot 2.x视频教程,打造一套最全的Spring Boot 2.x视频教程。



SpringBoot集成mybatis-plus及分页插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://choupangxia.com/2020/04/08/springboot-mybatis-plus/