因实际业务需要,服务需要连接两个不同的数据库进行业务处理。
1.添加配置数据库数据源信息
2.增加数据源配置类
主数据源:
package com.ebiz.sale.center.common.db.conf;
import com.github.pagehelper.PageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.client.RestTemplate;
import javax.sql.DataSource;
import java.util.Properties;
@Slf4j
@Configuration
@MapperScan(basePackages = "com.ebiz.sale.center.dao", sqlSessionTemplateRef = "ebizSaleCenterSqlSessionTemplate")
public class DataSourceConfig {
@Bean(name = "ebizSaleCenterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "ebizSaleCenterSqlSessionFactory")
@Primary
public SqlSessionFactory ebizSaleCenterSqlSessionFactory(@Qualifier("ebizSaleCenterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/*/*.xml"));
PageInterceptor pageHelper = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("supportMethodsArguments", "true");
pageHelper.setProperties(properties);
bean.setPlugins(new Interceptor[]{pageHelper});
return bean.getObject();
}
@Bean(name = "ebizSaleCenterTransactionManager")
@Primary
public DataSourceTransactionManager ebizSaleCenterTransactionManager(@Qualifier("ebizSaleCenterDataSource") DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(
dataSource);
dataSourceTransactionManager.setRollbackOnCommitFailure(true);
dataSourceTransactionManager.setGlobalRollbackOnParticipationFailure(true);
return dataSourceTransactionManager;
}
@Bean(name = "ebizSaleCenterSqlSessionTemplate")
@Primary
public SqlSessionTemplate ebizSaleCenterSqlSessionTemplate(@Qualifier("ebizSaleCenterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二数据源
package com.ebiz.sale.center.common.db.conf;
import com.github.pagehelper.PageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.util.Properties;
@Slf4j
@Configuration
@MapperScan(basePackages = "com.ebiz.sale.center.policydao", sqlSessionTemplateRef = "ebizPolicyCenterSqlSessionTemplate")
public class DataSourcePolicyConfig {
@Bean(name = "ebizPolictCenterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "ebizPolictCenterSqlSessionFactory")
public SqlSessionFactory ebizSaleCenterSqlSessionFactory(
@Qualifier("ebizPolictCenterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:policymybatis/*/*.xml"));
PageInterceptor pageHelper = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("supportMethodsArguments", "true");
pageHelper.setProperties(properties);
bean.setPlugins(new Interceptor[] { pageHelper });
return bean.getObject();
}
@Bean(name = "ebizPolictCenterTransactionManager")
public DataSourceTransactionManager ebizSaleCenterTransactionManager(
@Qualifier("ebizPolictCenterDataSource") DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
dataSourceTransactionManager.setRollbackOnCommitFailure(true);
dataSourceTransactionManager.setGlobalRollbackOnParticipationFailure(true);
return dataSourceTransactionManager;
}
@Bean(name = "ebizPolicyCenterSqlSessionTemplate")
public SqlSessionTemplate ebizSaleCenterSqlSessionTemplate(
@Qualifier("ebizPolictCenterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
*不同配置类需要加载对应数据库配置信息,扫描对应的dao接口,并且配置一个主数据源(默认数据源 @Primary)*