Mybatis多数据源配置

young 792 2021-11-12

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

Jndi数据源配置类

public class JndiDataSourceConfig {
    /**
     * 全局mybatis配置
     */
	@Autowired
	private MybatisConfig mybatisConfig;

    /**
     * 创建数据源
     * @param jndiDataSouceProperties 数据源配置配
     * @return
     */
	protected DataSource dataSource(JndiDataSouceProperties jndiDataSouceProperties) {
		JndiDataSourceLookup lookup = new JndiDataSourceLookup();
		lookup.setResourceRef(true);
		return lookup.getDataSource(jndiDataSouceProperties.getJndiName());
	}

    /**
     * 创建数据源事务管理器
     * @param jndiDataSouceProperties
     * @return
     */
	protected DataSourceTransactionManager transactionManager(JndiDataSouceProperties jndiDataSouceProperties) {
		return new DataSourceTransactionManager(dataSource(jndiDataSouceProperties));
	}

    /**
     * 创建SqlSessionFactory
     * @throws Exception
     */
	protected SqlSessionFactory sqlSessionFactory(DataSource primaryDataSource,
			JndiDataSouceProperties jndiDataSouceProperties) throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(primaryDataSource);
		String[] mapperLocationArr = jndiDataSouceProperties.getMapperLocation();
		List<Resource> 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 {
    /**
     * jndi名称
     */
   private String jndiName;
   @NotEmpty
    /**
     * Mybatis扫描路径
     */
   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);
   }
}

配置文件

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