spring boot项目自定义数据源,mybatisplus分页、逻辑删除无效解决方法

 2023-09-09 阅读 20 评论 0

摘要:Spring Boot项目中数据源的配置可以通过两种方式实现: 1.application.yml或者application.properties配置 2.注入DataSource及SqlSessionFactory两个Bean 通过第二种方式配置数据源则按照MybatisPlus官方文档使用分页及逻辑删除插件会无效,解决思路是在初始化S

Spring Boot项目中数据源的配置可以通过两种方式实现:

1.application.yml或者application.properties配置

2.注入DataSource及SqlSessionFactory两个Bean

通过第二种方式配置数据源则按照MybatisPlus官方文档使用分页及逻辑删除插件会无效,解决思路是在初始化SqlSessionFactory将插件设置进去

    /*** 逻辑删除插件*/@Beanpublic GlobalConfig globalConfig() {GlobalConfig globalConfig = new GlobalConfig();GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();dbConfig.setLogicDeleteValue("Y");dbConfig.setLogicNotDeleteValue("N");globalConfig.setDbConfig(dbConfig);globalConfig.setSqlInjector(new LogicSqlInjector());return globalConfig;}/*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();paginationInterceptor.setDialectType(DbType.MYSQL.getDb());return paginationInterceptor;}@Bean(name = "sqlSessionFactory")public SqlSessionFactory sqlSessionFactory() throws Exception {logger.info("初始化SqlSessionFactory");String mapperLocations = "classpath:mybatis/mapper/**/*.xml";String configLocation = "classpath:mybatis/mybatis-config.xml";MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource()); //数据源ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();sqlSessionFactory.setMapperLocations(resolver.getResources(mapperLocations));sqlSessionFactory.setConfigLocation(resolver.getResource(configLocation));sqlSessionFactory.setTypeAliasesPackage("com.innjoy.pms.order.infrastructure.domain.model");sqlSessionFactory.setGlobalConfig(globalConfig());sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});return sqlSessionFactory.getObject();}

 

java三种分页实现?转载于:https://www.cnblogs.com/loptis/p/10879417.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/31786.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息