一、配置文件
spring:
# datasource:
# username: root
# password: 123456
# url: jdbc:mysql://127.0.0.1:3306/jun01?characterEncoding=utf-8&serverTimezone=UTC
# driver-class-name: com.mysql.cj.jdbc.Driverdatasource:# 数据源1onedata:jdbc-url: jdbc:mysql://127.0.0.1:3306/jun01?useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver# 数据源2twodata:jdbc-url: jdbc:mysql://127.0.0.1:3306/jun02?useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
二、数据源配置类
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 javax.sql.DataSource;@Configuration
public class DataSourceConfiguration {// Primary 注解是在没有指明使用哪个数据源的时候指定默认使用的主数据源@Primary@Bean("oneDataSource")@ConfigurationProperties(prefix = "spring.datasource.onedata")public DataSource primaryDataSource() {return DataSourceBuilder.create().build();}@Bean("twoDataSource")@ConfigurationProperties(prefix = "spring.datasource.twodata")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}
}
三、数据源与 Mybatis 配置
1、数据源一
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.annotation.Resource;
import javax.sql.DataSource;@Configuration
// 指定该数据源扫描指定包下面的Mapper接口 与 *.xml文件
@MapperScan(basePackages = "com.south.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryOne",sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class DataSourceOneConfig {// 注入数据源1@Resourceprivate DataSource oneDataSource;@Bean@Primarypublic SqlSessionFactory sqlSessionFactoryOne() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(oneDataSource);return sqlSessionFactoryBean.getObject();}@Bean@Primarypublic SqlSessionTemplate sqlSessionTemplateOne() throws Exception {return new SqlSessionTemplate(sqlSessionFactoryOne());}
}
2、数据源二(两种不同的配置方式,都能实现多数据源的效果)
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.example.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryTwo")
public class DataSourceTwoConfig {// mapper 扫描 xml 文件的路径private static final String MAPPER_LOCATION = "classpath:mapper2/*.xml";private DataSource twoDataSource;// 通过构造方法进行注入public DataSourceTwoConfig(DataSource dataSource) {this.twoDataSource = dataSource;}@Beanpublic SqlSessionFactory sqlSessionFactoryTwo() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();// 指定数据源sqlSessionFactoryBean.setDataSource(twoDataSource);/** 获取xml文件资源对象* 当Mapper接口所对应的.xml文件与Mapper接口文件分离,存储在 resources* 文件夹下的时候,需要手动指定.xml文件所在的路径*/Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);sqlSessionFactoryBean.setMapperLocations(resources);return sqlSessionFactoryBean.getObject();}@Beanpublic DataSourceTransactionManager SecondaryDataSourceManager() {return new DataSourceTransactionManager(twoDataSource);}
}
四、使用案例
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import java.util.List;@Controller
@RequestMapping("/testController")
public class TestController {// 数据源1Mapper注入@Resourceprivate OneMapper oneMapper;// 数据源2Mapper注入@Resourceprivate TwoMapper twoMapper;@GetMapping("/selectManyDataSouroneMapperceData")@ResponseBodypublic void selectManyDataSourceDatwoMapperta() {// 查询数据源1中的数据List<String> message = oneMapper.getMessage();System.out.println(message);// 查询数据源2中的数据List<String> tag = twoMapper.getTag();System.out.println(tag);}
}