Spring Boot項目中數據源的配置可以通過兩種方式實現: 1.application.yml或者application.properties配置 2.註入DataSource及SqlSessionFactory兩個Bean 通過第二種方式配置數據源則按照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(); }