栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot Mybatis多数据源配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot Mybatis多数据源配置

生产环境数据源配置为jndi,故代码中的多数据源配置基于jndi

Jndi数据源配置类
public class JndiDataSourceConfig {
    
	@Autowired
	private MybatisConfig mybatisConfig;

    
	protected DataSource dataSource(JndiDataSouceProperties jndiDataSouceProperties) {
		JndiDataSourceLookup lookup = new JndiDataSourceLookup();
		lookup.setResourceRef(true);
		return lookup.getDataSource(jndiDataSouceProperties.getJndiName());
	}

    
	protected DataSourceTransactionManager transactionManager(JndiDataSouceProperties jndiDataSouceProperties) {
		return new DataSourceTransactionManager(dataSource(jndiDataSouceProperties));
	}

    
	protected SqlSessionFactory sqlSessionFactory(DataSource primaryDataSource,
			JndiDataSouceProperties jndiDataSouceProperties) throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(primaryDataSource);
		String[] mapperLocationArr = jndiDataSouceProperties.getMapperLocation();
		List resources = new ArrayList<>();
		for (String mapperLocation : mapperLocationArr) {
			resources.addAll(Arrays.asList(new PathMatchingResourcePatternResolver().getResources(mapperLocation)));
		}
		sessionFactory.setMapperLocations(resources.toArray(new Resource[resources.size()]));
		sessionFactory.setConfiguration(fillConfiguration());
		return sessionFactory.getObject();
	}

	private org.apache.ibatis.session.Configuration fillConfiguration() {
		org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
		configuration.setCacheEnabled(mybatisConfig.isCacheEnabled());
		configuration.setLazyLoadingEnabled(mybatisConfig.isLazyLoadingEnabled());
		configuration.setLocalCacheScope(mybatisConfig.getLocalCacheScope());
		configuration.setJdbcTypeForNull(mybatisConfig.getJdbcTypeForNull());
		configuration.setMapUnderscoreToCamelCase(mybatisConfig.isMapUnderscoreToCamelCase());
		return configuration;
	}
}
Jndi数据源配置类
@Data
public class JndiDataSouceProperties {
    
   private String jndiName;
   @NotEmpty
    
   private String[] mapperLocation;
}
主(默认)数据源配置类
@Configuration
@ConfigurationProperties(prefix = "datasource.primary")
public class PrimaryDataSourceProperties extends JndiDataSouceProperties {
}
主(默认)数据源配置
@EnableConfigurationProperties({PrimaryDataSourceProperties.class, MybatisConfig.class})
@ConditionalOnProperty(prefix = "datasource.primary", name = "jndi-name")
@Configuration
// 配置扫包路径
@MapperScan(basePackages = {"wms.dao.common"},sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig extends JndiDataSourceConfig {
   @Autowired
   private PrimaryDataSourceProperties primaryDataSourceProperties;

   @Primary
   @Bean(name = "primaryDataSource")
   public DataSource primaryDataSource() {
      return super.dataSource(primaryDataSourceProperties);
   }

   @Primary
   @Bean(name = "primaryTransactionManager")
   public DataSourceTransactionManager primaryTransactionManager() {
      return super.transactionManager(primaryDataSourceProperties);
   }

   @Primary
   @Bean(name = "primarySqlSessionFactory")
   public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource)
         throws Exception {
      return super.sqlSessionFactory(primaryDataSource, primaryDataSourceProperties);
   }
}
其他数据源配置
@EnableConfigurationProperties({ClusterDataSourceProperties.class, MybatisConfig.class})
@ConditionalOnProperty(prefix = "datasource.cluster", name = "jndi-name")
@Configuration
@MapperScan(basePackages = {"credit.channel.wind.dao"},sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDataSourceConfig extends JndiDataSourceConfig {
   @Autowired
   private ClusterDataSourceProperties clusterDataSourceProperties;


   @Bean(name = "clusterDataSource")
   public DataSource clusterDataSource() {
      return super.dataSource(clusterDataSourceProperties);
   }

   @Bean(name = "clusterTransactionManager")
   public DataSourceTransactionManager clusterTransactionManager() {
      return super.transactionManager(clusterDataSourceProperties);
   }

   @Bean(name = "clusterSqlSessionFactory")
   public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
         throws Exception {
      return super.sqlSessionFactory(clusterDataSource, clusterDataSourceProperties);
   }
}
配置文件

Application.yaml

datasource:
  primary:
    jndiName: java:comp/env/jndi_credit
    mapperLocation:
      - wms/dao/common/*.xml
  cluster:
    jndi-name: java:comp/env/jndi_bdos
    mapper-location:
      - credit/channel/wind/dao/*.xml
# 自定义mybatis配置      
ibatis:
  config:
    cacheEnabled: false
    lazyLoadingEnabled: false
    localCacheScope: statement
    jdbcTypeForNull: other
    mapUnderscoreToCamelCase: true
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/532254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号