当前位置: 首页> 健康> 养生 > 科技馆_重庆网站建设有佳网络_重庆网站建设技术外包_seo外包如何

科技馆_重庆网站建设有佳网络_重庆网站建设技术外包_seo外包如何

时间:2025/8/23 5:34:11来源:https://blog.csdn.net/Leo_Hth/article/details/142991617 浏览次数:0次
科技馆_重庆网站建设有佳网络_重庆网站建设技术外包_seo外包如何

  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");}
}

关键字:科技馆_重庆网站建设有佳网络_重庆网站建设技术外包_seo外包如何

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: