MyBatisPlus相关组件 的应用注意事项

本文解决MybatisPlus在SpringBoot项目中分页插件PaginationInterceptor无法正确返回total和pages的问题,及BaseMapper插入操作报错的详细解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记在MybatisPlus+Springboot使用过程中踩过的坑

问题1. mybatisplus分页插件PaginationInterceptor的使用,可以正常分页,但是返回不了total和pages参数,但是我按照官网说明引入了PaginationInterceptor配置。
在使用mybatisplus分页查询时,只需要在对应.xml文件的方法中多传一个page即可。如下:

public Page<StaffInfo> queryStaffInfo(StaffInfo staff, Page page) {
        List<StaffInfo> staffInfos = mapper.queryStaffInfo(staff, page);
        page.setRecords(staffInfos);
        return page;
    }

对应的.xml映射方法如下:

<select id="queryStaffInfo" resultMap="staffInfoResult">
        select <include refid="baseSql"/> from STAFF_INFO
        where 1 = 1
        <if test="staff.id != '' and staff.id != null">
            and id = #{staff.id}
        </if>
        <if test="staff.staffName != '' and staff.staffName != null">
            and STAFF_NAME like '%' || #{staff.staffName} || '%'
        </if>
        <if test="staff.staffSex != '' and staff.staffSex != null">
            and STAFF_SEX = #{staff.staffSex}
        </if>
        <if test="staff.departId != '' and staff.departId != null">
            and DEPART_ID = #{staff.departId}
        </if>
        <if test="staff.departName != '' and staff.departName != null">
            and DEPART_NAME = #{staff.departName}
        </if>
        <if test="staff.higherId != '' and staff.higherId != null">
            and HIGHER_ID = #{staff.higherId}
        </if>
        <if test="staff.staffCode != '' and staff.staffCode != null">
            and STAFF_CODE = #{staff.staffCode}
        </if>
        <if test="staff.address != '' and staff.address != null">
            and ADDRESS = #{staff.address}
        </if>
        <if test="staff.telphone != '' and staff.telphone != null">
            and TELPHONE = #{staff.telphone}
        </if>
        <if test="staff.indetityNumber != '' and staff.indetityNumber != null">
            and INDETITY_NUMBER = #{staff.indetityNumber}
        </if>
        <if test="staff.createTime != '' and staff.createTime != null">
            and to_char(CREATE_TIME,'yyyy-mm-dd') = to_char(#{staff.createTime},'yyyy-mm-dd')
        </if>
        <if test="staff.createOpid != '' and staff.createOpid != null">
            and CREATE_OPID = #{staff.createOpid}
        </if>
        order by CREATE_TIME desc
    </select>

我的PaginationInterceptor配置如下:

 @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType(DBType.ORACLE.getDb());
        return paginationInterceptor;
    }

官网截图如下:
在这里插入图片描述

后来查看方法调用信息,发现Page的父类 Pagination的父类RowBounds中的total压根没有赋值,没有调用到jar包中Plug中的方法:
org.apache.ibatis.plugin.Plugin#invoke
com.baomidou.mybatisplus.plugins.PaginationInterceptor#intercept
正常来讲是会调用的,所以判断还是插件引用不成功。

最后发现在自己的sqlSessionFactory配置时未将注入的插件配置进去,代码如下:

@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceConfig());

        //设置扫描 mybatis-config.xml
        sqlSessionFactoryBean.setConfigLocation(null);

        //设置扫描mapper.xml
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources(MAPPER_XML_PACKAGE);
        sqlSessionFactoryBean.setMapperLocations(resources);
        //!!!!设置插件:将分页插件配置好,否则分页插件不可用!!!!
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor()});
        //设置扫描实体类
        sqlSessionFactoryBean.setTypeAliasesPackage(MYBATIS_BEAN_PACKAGE);

        return sqlSessionFactoryBean.getObject();
    }

其中的sqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor()});一开始并没有加,加上就可以了。

问题2. 调用MybatisPlus的BaseMapper中的方法insert()报错:
mybatis-plus Invalid bound statement (not found)
同样查询官网得知:
在这里插入图片描述
官网的意思是mappper没扫描到,但是我的配置时没问题的,仅仅调用baseMapper里的方法才会报错。所以并不是这样原因,后来专门针对baseMapper方法报错的原因进行度娘一番(引用自:https://www.cnblogs.com/zhouyb/p/10469973.html ),找到一个类似的例子,说是将SqlSessionFactory的注入方法改为MyBatisSqlSessionBean,我看了下自己的是SqlSessionFactoryBean。故导致不能将BaseMpper里的方法预加载到容器管理,在调用的时候也就找不到。改完后即可解决。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值