spring boot项目自定义数据源,mybatisplus分页逻辑删除无效解决方法
Posted loptis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot项目自定义数据源,mybatisplus分页逻辑删除无效解决方法相关的知识,希望对你有一定的参考价值。
Spring Boot项目中数据源的配置可以通过两种方式实现:
1.application.yml或者application.properties配置
2.注入DataSource及SqlSessionFactory两个Bean
通过第二种方式配置数据源则按照MybatisPlus官方文档使用分页及逻辑删除插件会无效,解决思路是在初始化SqlSessionFactory将插件设置进去
/** * 逻辑删除插件 */ @Bean public 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; } /** * 分页插件 */ @Bean public 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(); }
以上是关于spring boot项目自定义数据源,mybatisplus分页逻辑删除无效解决方法的主要内容,如果未能解决你的问题,请参考以下文章
通过注解实现自定义Spring Boot Starter自动装配