不要使用Mybatis原生的SqlSessionFactory,而应使用MybatisSqlSessionFactory。

原因

  1. 依赖关系如下



    因此会在使用Mybaits-Plus时默认的时Mybatis原生的SqlSessionFactory

  2. 因为MybatisPlus中自定义了MybatisSqlSessionFactoryBean这个类,而这个类是实现了FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent>这些接口,而在Mybatis中有一个SqlSessionFactoryBean类也实现了这些接口,所以在MybatisPlus的配置文件中配置SqlSessionFactoryBean时需要换成MybatisPlus中自定义的这个类MyBatisSqlSessionFactoryBean,否则使用原生的SqlSessionFactoryBean会产生很多问题,这也是官方文档出现-invalid-bound-statement-not-found-异常中的最后第4点特别说明并推荐的

解决方案

相应的配置类如下

package zeyfra.com.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import zeyfra.com.aspect.mybatis_plus.MyMetaObjectHandler; import javax.sql.DataSource; /**
* MyBatis-Plus配置类
* @author ZeyFra
* @date 2021/2/16 13:27
*/
@Configuration
@EnableTransactionManagement
@MapperScan("zeyfra.com.modules.**.mapper")
public class MybatisPlusConfig { @Bean("sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Autowired @Qualifier("dataSource") DataSource dataSource) throws Exception { // 未使用MyBatis-Plus的分页插件的小伙伴可忽略此部分
// MybatisPlus内部拦截器
// 为自动分页插件设置DB类型
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 可添加自定义拦截器,没有自定义拦截器的小伙伴可忽略此部分
// MybatisConfiguration
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.addInterceptor(mybatisPlusInterceptor); // 开启缓存,可自行选择
mybatisConfiguration.setCacheEnabled(true); // 使用MybatisSqlSessionFactoryBean
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); // 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource); // 添加MybatisConfiguration
sqlSessionFactoryBean.setConfiguration(mybatisConfiguration); // 设置mapper相应的xml路径
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:zeyfra/com/modules/**/*.xml")); // 返回MybatisSqlSessionFactoryBean从而替代原生的sqlSessionFactory
return sqlSessionFactoryBean.getObject();
} }

注意:

1、使用MybatisSqlSessionFactory之后配置应重新在此注入相应的拦截器

2、在3.4.0过后已弃用PaginationInterceptor,应使用MybatisPlusInterceptor添加自动分页插件PaginationInnerInterceptor

3、应在自动分页插件设置DB类型,否则容易出现分页查询后total为0的问题

4、pageHelper依赖会与自动分页插件产生冲突,也会出现分页查询后total为0的问题

MyBatis-Plus【踩坑记录01】的更多相关文章

  1. CentOS7.4安装MySQL踩坑记录

    CentOS7.4安装MySQL踩坑记录 time: 2018.3.19 CentOS7.4安装MySQL时网上的文档虽然多但是不靠谱的也多, 可能因为版本与时间的问题, 所以记录下自己踩坑的过程, ...

  2. SUCTF 2019 Upload labs 2 踩坑记录

    SUCTF 2019 Upload labs 2 踩坑记录 题目地址 : https://github.com/team-su/SUCTF-2019/tree/master/Web/Upload La ...

  3. unionId突然不能获取的踩坑记录

    昨天(2016-2-2日),突然发现系统的一个微信接口使用不了了.后来经查发现,是在网页授权获取用户基本信息的时候,unionid获取失败导致的. 在网页授权获取用户基本信息的介绍中(http://m ...

  4. ubuntu 下安装docker 踩坑记录

    ubuntu 下安装docker 踩坑记录 # Setp : 移除旧版本Docker sudo apt-get remove docker docker-engine docker.io # Step ...

  5. SpringBoot + Shiro + shiro.ini 的踩坑记录

    0.写在前面的话 好久没写博客了,诶,好多时候偷懒直接就抓网上的资料丢笔记里了,也就没有自己提炼,偷懒偷懒.然后最近参加了一个网络课程,要交作业的那种,为了能方便看下其他同学的作业,就写了个爬虫把作业 ...

  6. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  7. google nmt 实验踩坑记录

       最近因为要做一个title压缩的任务,所以调研了一些text summary的方法.    text summary 一般分为抽取式和生成式两种.前者一般是从原始的文本中抽取出重要的word o ...

  8. ABP框架踩坑记录

    ABP框架踩坑记录 ASP.NET Boilerplate是一个专用于现代Web应用程序的通用应用程序框架. 它使用了你已经熟悉的工具,并根据它们实现最佳实践. 文章目录 使用MySQL 配置User ...

  9. SpringBoot+SpringSecurity+Thymeleaf认证失败返回错误信息踩坑记录

    Spring boot +Spring Security + Thymeleaf认证失败返回错误信息踩坑记录 步入8102年,现在企业开发追求快速,Springboot以多种优秀特性引领潮流,在众多使 ...

随机推荐

  1. 关于free和delete的使用

    上一篇篇幅太长,这里再区分free和delete的用法. 两个同时存在是有它的原因的,我们前面说过,free是函数,它只释放内存,但不会调用析构函数,如果用free去释放new申请的空间,会因为无法调 ...

  2. HDU 6155 Subsequence Count(矩阵 + DP + 线段树)题解

    题意:01串,操作1:把l r区间的0变1,1变0:操作2:求出l r区间的子序列种数 思路:设DP[i][j]为到i为止以j结尾的种数,假设j为0,那么dp[i][0] = dp[i - 1][1] ...

  3. u-boot 移植 --->3、S5PV210启动序列

    通过三星官方的资料S5PV210_iROM_ApplicationNote_Preliminary_20091126.pdf,了解到S5PVS10这款芯片的复位过程启动序列.芯片在出厂时就在内部固化了 ...

  4. 24 WAYS to impress your friends

    24 WAYS to impress your friends 24 ways is the advent calendar for web geeks. For twenty-four days e ...

  5. React Toolchains

    React Toolchains Create React App / component library https://create-react-app.dev/docs/getting-star ...

  6. holy shit CSDN

    holy shit CSDN 垃圾 CSDN 到处都是垃圾文章, 无人子弟 到处都是垃圾广告,看的恶心 毫无底线,窃取别人的知识成果,毫无版权意识 垃圾爬虫,垃圾小号 ...等等 Google Sea ...

  7. true && number !== boolean

    true && number !== boolean bug let result = ``; // section, name ? create text, compute cent ...

  8. Flutter: random color

    import 'dart:math' as math; import 'package:flutter/material.dart'; void main() => runApp(App()); ...

  9. React Portal - 弹出层的优秀解决方案

    对于需要使用弹出层的需求 ,Portal可以说是提供了一种完美的解决方案.相比于React Native中的实现更多的使用Modal或者绝对定位,Portal实在是简易友好得多. 场景 对话框,确认提 ...

  10. 京东数科二面:常见的 IO 模型有哪些?Java 中的 BIO、NIO、AIO 有啥区别?

    IO 模型这块确实挺难理解的,需要太多计算机底层知识.写这篇文章用了挺久,就非常希望能把我所知道的讲出来吧!希望朋友们能有收货!为了写这篇文章,还翻看了一下<UNIX 网络编程>这本书,太 ...