当前位置: 首页 > news >正文

SpringBoot项目:mybatis升级mybatis-plus

  1. 替换依赖
  2. 修改sqlSessionFactory bean
  3. 分页插件不生效问题记录

1.替换依赖:

将原来的mybatis整合springboot的依赖去掉,替换成mybatis-plus

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.7</version>
</dependency>

2.修改sqlSessionFactory bean

@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {try {//mybatis升级mybatis-plus,需要替换MybatisSqlSessionFactoryBeanMybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();//SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//原来mybatis的方式sqlSessionFactoryBean.setDataSource(dataSource);PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:/mapper/*.xml");org.springframework.core.io.Resource config = resolver.getResource("classpath:mybatis-config.xml");sqlSessionFactoryBean.setMapperLocations(resources);sqlSessionFactoryBean.setConfigLocation(config);return sqlSessionFactoryBean.getObject();} catch (Exception e) {logger.error("create sqlSessionFactoryBean error", e);throw new RuntimeException("create sqlSessionFactoryBean error");}
}

3.分页插件不生效问题

3.1配置分页插件:

@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbTypereturn interceptor;}
}

3.2 使用分页方法

/*** 分页查询用户列表*/
public List<TUserInfo> selectUserListByPage(TUserInfo userInfo, Page<TUserInfo> page) {return baseMapper.selectPage(page, new QueryWrapper<>(userInfo)).getRecords();
}

但是他的sql打印并没有带limit,解决方案:手动设置插件

@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) {try {//mybatis升级mybatis-plus,需要替换MybatisSqlSessionFactoryBeanMybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();//SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//原来mybatis的方式sqlSessionFactoryBean.setDataSource(dataSource);PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:/mapper/*.xml");org.springframework.core.io.Resource config = resolver.getResource("classpath:mybatis-config.xml");sqlSessionFactoryBean.setMapperLocations(resources);sqlSessionFactoryBean.setConfigLocation(config);//分页未生效效,这里手动设置插件Interceptor[] plugins = {mybatisPlusInterceptor};sqlSessionFactoryBean.setPlugins(plugins);return sqlSessionFactoryBean.getObject();} catch (Exception e) {logger.error("create sqlSessionFactoryBean error", e);throw new RuntimeException("create sqlSessionFactoryBean error");}
}


http://www.mrgr.cn/news/51542.html

相关文章:

  • Gin框架操作指南12:完结篇
  • Java mybatis day1015
  • 免费分享1885页Python电子书,耗时200小时整理!!!
  • 如何搭建使用采购管理系统?
  • Flask 将表单数据发送到模板
  • T2彩色图片分类
  • 数据结构——广义表
  • MATLAB和Python电车电池制造性能度量分析
  • 51单片机的厨房安全监控系统【proteus仿真+程序+报告+原理图+演示视频】
  • 提示msvcr100.dll丢失的解决方法,推荐这6种解决方法
  • c++前置和后置的运算符重载,红黑树的概念以及static关键字
  • 移动端面试问题笔记(一)
  • 【v5-Lite】模型导入使用-attempt_load
  • 【进阶OpenCV】 (16)-- 人脸识别 -- FisherFaces算法
  • 基于Arduino的简易收音机
  • DW-大模型生图安全疫苗注入作业记录
  • ES6新增特性
  • 60个java常用的代码(能够帮助您更好地理解Java编程)
  • Java 二分搜索
  • 二叉树——二叉树的前中后序遍历