fix(sj_1.2.0-beta1): 优化mybatis xml的加载顺序, 支持多个id同时加载
This commit is contained in:
parent
108eaf7a14
commit
90cea3bd31
@ -3,6 +3,7 @@ package com.aizuda.snailjob.template.datasource.config;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aizuda.snailjob.template.datasource.enums.DbTypeEnum;
|
||||
import com.aizuda.snailjob.template.datasource.handler.InjectionMetaObjectHandler;
|
||||
import com.aizuda.snailjob.template.datasource.handler.SnailJobMybatisConfiguration;
|
||||
import com.aizuda.snailjob.template.datasource.utils.DbUtils;
|
||||
import com.aizuda.snailjob.template.datasource.utils.RequestDataHelper;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
|
||||
@ -44,18 +45,21 @@ public class SnailJobTemplateAutoConfiguration {
|
||||
private static final List<String> TABLES_WITH_PARTITION = Arrays.asList("sj_retry_task", "sj_retry_dead_letter");
|
||||
|
||||
@Bean("sqlSessionFactory")
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, Environment environment, MybatisPlusInterceptor mybatisPlusInterceptor, MybatisPlusProperties mybatisPlusProperties) throws Exception {
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, Environment environment,
|
||||
MybatisPlusInterceptor mybatisPlusInterceptor,
|
||||
MybatisPlusProperties mybatisPlusProperties,
|
||||
SnailJobMybatisConfiguration snailJobMybatisConfiguration) throws Exception {
|
||||
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
|
||||
factoryBean.setDataSource(dataSource);
|
||||
DbTypeEnum dbTypeEnum = DbUtils.getDbType();
|
||||
|
||||
// 动态设置mapper资源: 通用 + 数据库专用
|
||||
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
Resource[] templateMapperResource = resolver.getResources("classpath*:/template/mapper/*.xml");
|
||||
Resource[] specificMapperResource = resolver.getResources(MessageFormat.format("classpath*:/{0}/mapper/*.xml", dbTypeEnum.getDb()));
|
||||
Resource[] templateMapperResource = resolver.getResources("classpath*:/template/mapper/*.xml");
|
||||
List<Resource> resources = new ArrayList<>();
|
||||
resources.addAll(List.of(templateMapperResource));
|
||||
resources.addAll(List.of(specificMapperResource));
|
||||
resources.addAll(List.of(templateMapperResource));
|
||||
factoryBean.setMapperLocations(resources.toArray(new Resource[0]));
|
||||
|
||||
// 分页插件
|
||||
@ -67,6 +71,7 @@ public class SnailJobTemplateAutoConfiguration {
|
||||
globalConfig.setMetaObjectHandler(new InjectionMetaObjectHandler());
|
||||
|
||||
factoryBean.setGlobalConfig(mybatisPlusProperties.getGlobalConfig());
|
||||
factoryBean.setConfiguration(snailJobMybatisConfiguration);
|
||||
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
@ -76,6 +81,10 @@ public class SnailJobTemplateAutoConfiguration {
|
||||
return new SqlSessionTemplate(sqlSessionFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SnailJobMybatisConfiguration snailJobMybatisConfiguration() {
|
||||
return new SnailJobMybatisConfiguration();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(Environment environment) {
|
||||
|
@ -0,0 +1,47 @@
|
||||
package com.aizuda.snailjob.template.datasource.handler;
|
||||
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.JobLogMessageMapper;
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.JobSummaryMapper;
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.RetrySummaryMapper;
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.RetryTaskLogMapper;
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.RetryTaskLogMessageMapper;
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.RetryTaskMapper;
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author: opensnail
|
||||
* @date : 2024-07-19
|
||||
* @since : 1.2.0
|
||||
*/
|
||||
public class SnailJobMybatisConfiguration extends MybatisConfiguration {
|
||||
|
||||
/**
|
||||
* 重复的ID不需要告警 配置列表
|
||||
* 此设计为了多数据源不同数据库包下面加载的xml有重复的mybatis id 会告警报错问题
|
||||
*/
|
||||
static final Set<String> DUPLICATE_IDS = new HashSet<>();
|
||||
|
||||
static {
|
||||
DUPLICATE_IDS.add(JobSummaryMapper.class.getName() + ".insertBatch");
|
||||
DUPLICATE_IDS.add(RetryTaskLogMapper.class.getName() + ".insertBatch");
|
||||
DUPLICATE_IDS.add(RetrySummaryMapper.class.getName() + ".insertBatch");
|
||||
DUPLICATE_IDS.add(RetryTaskLogMessageMapper.class.getName() + ".insertBatch");
|
||||
DUPLICATE_IDS.add(RetryTaskMapper.class.getName() + ".insertBatch");
|
||||
DUPLICATE_IDS.add(JobLogMessageMapper.class.getName() + ".insertBatch");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMappedStatement(final MappedStatement ms) {
|
||||
if (mappedStatements.containsKey(ms.getId())) {
|
||||
if (!DUPLICATE_IDS.contains(ms.getId())) {
|
||||
super.addMappedStatement(ms);
|
||||
}
|
||||
} else {
|
||||
super.addMappedStatement(ms);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user