记在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里的方法预加载到容器管理,在调用的时候也就找不到。改完后即可解决。