Spring Boot项目中数据源的配置可以通过两种方式实现:
1.application.yml或者application.properties配置
2.注入DataSource及SqlSessionFactory两个Bean
通过第二种方式配置数据源则按照MybatisPlus官方文档使用分页及逻辑删除插件会无效,解决思路是在初始化SqlSessionFactory将插件设置进去
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 逻辑删除插件
*/
@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();
}