MyBatis-Plus查询:批量、条件、分页查询的实现

MyBatis-Plus在MyBatis的基础上只做增强,不做改变,目的是为了简化开发,提高效率。本专栏六篇文章围绕MyBatis-Plus的常用技术点,结合springboot,实现了对数据库记录的CRUD操作,其他文章请参考:


作者:Hudie
个人公众号/CSDN博客:编程一只蝶
项目已开源至gitee:https://gitee.com/guo-qianliang/mybatis-plus-test
项目已开源至github:https://github.com/Guoqianliang/mybatis-plus-test


一、MyBatis-Plus实现批量查询

MyBatis-Plus的selectBatchIds方法完成了动态sql的foreach的功能,需要传入一个集合作为批量id的容器,可通过Arrays的asList()方法直接填入。

    // 多个id批量查询
    @Test
    public void testSelect01() {
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        System.out.println(users);
    }

查询结果:
批量查询的结果

二、MyBatis-Plus实现条件查询

通过map封装查询条件时,key对应数据库中的列名。如:数据库user_id,实体类是userId,这时map的key需要填写user_id。

    // 简单条件查询:查询出name=Tom & age=28 的用户信息
    @Test
    public void testSelect02() {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("name", "Tom");
        columnMap.put("age", "28");
        List<User> users = userMapper.selectByMap(columnMap);
        System.out.println(users);
    }

查询结果:
条件查询的结果

实际上通过map封装查询条件的方式很少使用,后面文章中讲到的条件构造器才是经常使用的方法,这里作为了解即可

三、MyBatis-Plus实现分页查询

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能,具体步骤如下:

步骤1:配置分页插件

在配置类中配置分页插件。

@Configuration
@MapperScan("com.gql.mybatisplustest.mapper")
public class MpConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

}

步骤2:编写分页代码

分页的所有数据都在userPage对象中封装着,所以可以调用userPage对象的一系列方法对分页数据进行操作。

 /**
     * 分页查询
     * new Page(current,size):
     *      current:当前页
     *      size:   每页记录数
     */
    @Test
    public void testSelectPage() {
        Page<User> page = new Page(1, 3);
        Page<User> userPage = userMapper.selectPage(page, null);
        // 分页的所有数据都在userPage对象中封装着
        // 获取总页数
        long pages = userPage.getPages();
        // 获取当前页
        long current = userPage.getCurrent();
        // 获取当前页数据集合
        List<User> records = userPage.getRecords();
        // 获取总记录数
        long total = userPage.getTotal();
        // 当前页是否有下一页
        boolean hasNext = userPage.hasNext();
        // 当前页是否有上一页
        boolean hasPrevious = userPage.hasPrevious();

        System.out.println("总页数pages=" + pages);
        System.out.println("当前页current=" + current);
        System.out.println("当前页数据集合records=" + records);
        System.out.println("总记录数total=" + total);
        System.out.println("是否有下一页hasNext=" + hasNext);
        System.out.println("是否有上一页hasPrevious=" + hasPrevious);
    }

测试

当前数据库的user表中有8条记录,设置当前页数为1,每页记录数为3。
当前数据库中有8条记录
执行后的代码如下:
分页查询的结果
至此,批量查询、条件查询、分页查询都实现完成了。

Mybatis Plus提供了三种批量插入的方案:原生方法(saveBatch())、基于Mybatis Plus的自定义SQL语句、基于Mybatis Plus的批量插入。其中,原生方法(saveBatch())性能一般,无需配置;基于Mybatis Plus的自定义SQL语句可以支持一千条以上的插入,但需要编写XML文件;基于Mybatis Plus的批量插入是性能最优的方案,但需要进行配置。 为了正确利用MyBatis Plus进行批量插入,你可以遵循以下步骤: 1. 创建一个类,将其注入到Spring容器中: ```java @Component public class EasySqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); // 过滤Insert语句中的字段 methodList.add(new InsertBatchSomeColumn(t -> !"weekend".equals(t.getColumn()) && !"position".equals(t.getColumn()) && !"date".equals(t.getColumn()))); // 添加InsertBatchSomeColumn方法 return methodList; } } ``` 2. 创建一个Mapper接口: ```java public interface YourMapper extends BaseMapper<YourEntity> { Integer insertBatchSomeColumn(Collection<YourEntity> entityList); } ``` 3. 在业务层中注入这个Mapper,并传入List集合进行批量插入操作。 需要注意的是,SQL Server在批量插入时,最多支持2100个字符。如果超出字符限制,会报错。此外,不同的数据库对于批量插入语法的支持也不同,因此MyBatis Plus没有直接提供批量插入方法,需要自己进行复写。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hudie.

不要打赏!不要打赏!不要打赏!

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

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

打赏作者

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

抵扣说明:

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

余额充值