MyBatis-Plus【踩坑记录01】
不要使用Mybatis原生的SqlSessionFactory,而应使用MybatisSqlSessionFactory。
原因
依赖关系如下

因此会在使用Mybaits-Plus时默认的时Mybatis原生的SqlSessionFactory因为
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】的更多相关文章
- CentOS7.4安装MySQL踩坑记录
CentOS7.4安装MySQL踩坑记录 time: 2018.3.19 CentOS7.4安装MySQL时网上的文档虽然多但是不靠谱的也多, 可能因为版本与时间的问题, 所以记录下自己踩坑的过程, ...
- SUCTF 2019 Upload labs 2 踩坑记录
SUCTF 2019 Upload labs 2 踩坑记录 题目地址 : https://github.com/team-su/SUCTF-2019/tree/master/Web/Upload La ...
- unionId突然不能获取的踩坑记录
昨天(2016-2-2日),突然发现系统的一个微信接口使用不了了.后来经查发现,是在网页授权获取用户基本信息的时候,unionid获取失败导致的. 在网页授权获取用户基本信息的介绍中(http://m ...
- ubuntu 下安装docker 踩坑记录
ubuntu 下安装docker 踩坑记录 # Setp : 移除旧版本Docker sudo apt-get remove docker docker-engine docker.io # Step ...
- SpringBoot + Shiro + shiro.ini 的踩坑记录
0.写在前面的话 好久没写博客了,诶,好多时候偷懒直接就抓网上的资料丢笔记里了,也就没有自己提炼,偷懒偷懒.然后最近参加了一个网络课程,要交作业的那种,为了能方便看下其他同学的作业,就写了个爬虫把作业 ...
- 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密
你真的了解字典(Dictionary)吗? 从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...
- google nmt 实验踩坑记录
最近因为要做一个title压缩的任务,所以调研了一些text summary的方法. text summary 一般分为抽取式和生成式两种.前者一般是从原始的文本中抽取出重要的word o ...
- ABP框架踩坑记录
ABP框架踩坑记录 ASP.NET Boilerplate是一个专用于现代Web应用程序的通用应用程序框架. 它使用了你已经熟悉的工具,并根据它们实现最佳实践. 文章目录 使用MySQL 配置User ...
- SpringBoot+SpringSecurity+Thymeleaf认证失败返回错误信息踩坑记录
Spring boot +Spring Security + Thymeleaf认证失败返回错误信息踩坑记录 步入8102年,现在企业开发追求快速,Springboot以多种优秀特性引领潮流,在众多使 ...
随机推荐
- Ubuntu 18.04 + pip3 install virtualenvwrapper 找不到virtualenvwrapper.sh
Reference Ubuntu 18.04 只自带python3.6.5, 因此不想装python2了, 但通过apt install 装virtualenvwrapper时发现必须得装python ...
- JavaScript 的 7 种设计模式
原文地址:Understanding Design Patterns in JavaScript 原文作者:Sukhjinder Arora 译者:HelloGitHub-Robert 当启动一个新的 ...
- 2016 最新的 树莓派3 Raspberry Pi 3 上手评测 图解教程 新手必看!(VNC 安装,启动,关闭)
1.png . 官方教程: INSTALLING OPERATING SYSTEM IMAGES: https://www.raspberrypi.org/documentation/installa ...
- Docker In Action
Docker In Action Docker 实战 https://docs.docker.com/get-started/overview/ Docker Engine Docker Archit ...
- Flutter CodePen challenges
Flutter CodePen challenges 挑战赛 https://mp.weixin.qq.com/s/qIYokWN9SVgr-F7YxbJuOQ CodePen Flutter 编辑器 ...
- 解决ROS及Fast-RTPS安装和使用中raw.githubusercontent.com无法连接的问题
资料参考: https://blog.csdn.net/weixin_44692299/article/details/105869229
- [Python学习笔记]组织文件
shutil 模块 shutil 模块可以让我们很方便的在Python程序中复制.移动.改名和删除文件. 复制文件和文件夹 使用shutil.copy()来复制文件,该函数含两个参数,均为字符串格式的 ...
- listen EADDRINUSE: address already in use ::: 端口被占用
netstat -ano|findstr "端口号" // 查询被占用端口号的pid(3000,端口号则是被占用的端口号) taskkill /pid 8888 /F // 结束端 ...
- 使用PowerDesigner进行数据库设计并直接把设计好的表导出相应的建表语句
Power Designer:数据库表设计工具 PowerDesigner是Sybase公司的一款软件,使用它可以方便地对系统进行分析设计,他几乎包括了数据库模型设计的全过程.利用PowerDesig ...
- Vue使用 空白占位符
当有时候需要在页面显示时显示空格时,可以使用 ,但是使用这个占位符时,无论写多少个,就只能显示一个空格.要想显示多个空格进行占位,这种方式显然是可行的,解决方法是使用转义字符. 先看代码: <t ...