在日常开发中我们可能会用到多数据源开发, 什么是多数据源?
简单来讲的话, 就是一个项目连接多个数据库. 当然只是可能会用到, 我暂时没见过应用场景, 但是还是了解学习一下
此项目可以基于上一个简单集成项目进行简单的修改, 就能实现多数据源了.
application.YAML 配置
我们在上一个项目的基础上进行修改, 实现多数据源配置
- spring:
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- url: jdbc:MySQL://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- username: root
- password: 123456
- driver-class-name: com.MySQL.cj.jdbc.Driver
- druid:
- one:
- username: root
- password: 123456
- driver-class-name: com.MySQL.cj.jdbc.Driver
- url: jdbc:MySQL://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- initialSize: 5
- minIdle: 5
- maxActive: 20
- initial-size: 3
- min-idle: 3
- max-active: 10
- max-wait: 60000
- two:
- username: root
- password: 123456
- driver-class-name: com.MySQL.cj.jdbc.Driver
- url: jdbc:MySQL://localhost:3306/layui?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- initialSize: 5
- minIdle: 5
- maxActive: 20
- initial-size: 6
- min-idle: 6
- max-active: 20
- max-wait: 12000
- stat-view-servlet:
- login-username: admin
- login-password: admin
- filter:
- stat:
- log-slow-sql: true
- slow-sql-millis: 2000
- mybatis:
- mapper-locations: classpath:mappers///Mapper.xml
- type-aliases-package: com.ccsert.spdruid..model
- configuration:
- map-Underscore-to-camel-case: true
- logging:
- file: logs/mass.log
- level:
- org.springframework: info
- com.ccsert: DEBUG
着是完整的配置
主要在 druid 数据源和 mybatis 的 mapper.xml 进行了细微修改
这里我建立一个 layui 数据库, 里面有个和 demo 里一样的表, 数据和结构都一样, 方便等会测试
包结构调整, 代码修改
包结构调整
我们先把 mapper 接口修改一下
在原来的 mapper 包下建立两个包, 一个 one, 一个 two
然后把之前的 mapper 接口分别复制到 one 和 two 下
然后改一下名字
改完以后大概就是这个样子
代码修改
把之前的 mapper 注解都去掉
后面会用别的方法去映射
这是 oneMapper
- package com.ccsert.spdruid.test.mapper.one;
- import com.ccsert.spdruid.test.model.TestUser;
- import java.util.List;
- public interface TestUserOneMapper {
- List<TestUser> getall();
- TestUser getById(Integer id);
- int save(TestUser testUser);
- }
这是 twoMapper
- package com.ccsert.spdruid.test.mapper.two;
- import com.ccsert.spdruid.test.model.TestUser;
- public interface TestUserTwoMapper {
- TestUser getById(Integer id);
- }
这里为了方便我就只写一个接口
然后我们在修改一下 service 实现类
- package com.ccsert.spdruid.test.service.impl;
- import com.ccsert.spdruid.test.mapper.one.TestUserOneMapper;
- import com.ccsert.spdruid.test.mapper.two.TestUserTwoMapper;
- import com.ccsert.spdruid.test.model.TestUser;
- import com.ccsert.spdruid.test.service.TestUserService;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.List;
- @Service
- public class TestUserServiceImpl implements TestUserService {
- @Resource
- private TestUserOneMapper testUserOneMapper;
- @Resource
- private TestUserTwoMapper testUserTwoMapper;
- @Override
- public List<TestUser> getall() {
- return testUserOneMapper.getall();
- }
- @Override
- public TestUser getById(Integer id) {
- return testUserTwoMapper.getById(id);
- }
- @Override
- public int save(TestUser testUser) {
- return testUserOneMapper.save(testUser);
- }
- }
getById 方法让他去调用 twoMapper
其余的还是让它去调用原来的接口
配置文件修改
然后我们把 xml 的位置移动一下
在 resources 下的 mappers 下在建立两个文件夹
一个 one
一个 two
然后在把之前的 TestUser 目录复制两份到 one 和 two 下
把原来的 TestUser 删除掉
在把之前的 xml 名字修改一下
改完以后大概就是这个样子了
onemapper.xml 的内容保持不变
主要写一下 twomapper.xml 的 save 保存方法
<?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.ccsert.spdruid.test.mapper.two.TestUserTwoMapper">
- <resultMap id="BaseResultMap" type="com.ccsert.spdruid.test.model.TestUser">
- <id column="id" property="Id" />
- <result column="user_name" property="userName"/>
- <result column="password" property="password" />
- </resultMap>
- <select id="getById" parameterType="Integer" resultMap="BaseResultMap">
- SELECT
- id,user_name,password
- FROM
- test_user
- WHERE id=#{id}
- </select>
- </mapper>
因为这里的 mapper 接口路径是修改过的, onemapper.xml 要注意一下
多数据源配置
准备工作做好了接着就是配置多数据源了
在 test 包下建立一个 config 包, 用于存放配置
然后在 config 包下建立一个 MultiDataSourceConfig 类
- package com.ccsert.spdruid.test.config;
- import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- 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 MultiDataSourceConfig {
- @Primary
- @Bean(name = "oneDataSource")
- @ConfigurationProperties("spring.datasource.druid.one")
- public DataSource dataSourceOne(){
- return DruidDataSourceBuilder.create().build();
- }
- @Bean(name = "twoDataSource")
- @ConfigurationProperties("spring.datasource.druid.two")
- public DataSource dataSourceTwo(){
- return DruidDataSourceBuilder.create().build();
- }
- }
这里的 ConfigurationProperties 是获取的 YAML 或者 properties 里的值
spring.datasource.druid.one 和 spring.datasource.druid.two 就是我们配置的数据源
Primary 只能指定一个为默认数据源, 这里指定了 one 数据库
在 config 下建立 DataSource1Config 类, 用于配置数据源 one
- package com.ccsert.spdruid.test.config;
- 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.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;
- @Configuration
- @MapperScan(basePackages = "com.ccsert.spdruid.test.mapper.one", sqlSessionTemplateRef = "test1SqlSessionTemplate")
- public class DataSource1Config {
- @Bean(name = "test1SqlSessionFactory")
- @Primary
- public SqlSessionFactory testSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/one/**/*Mapper.xml"));
- return bean.getObject();
- }
- @Bean(name = "test1TransactionManager")
- @Primary
- public DataSourceTransactionManager testTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- @Bean(name = "test1SqlSessionTemplate")
- @Primary
- public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
- return new SqlSessionTemplate(sqlSessionFactory);
- }
- }
@MapperScan(basePackages = "com.ccsert.spdruid.test.mapper.one", sqlSessionTemplateRef = "test1SqlSessionTemplate") 指定了实体类的路径, 这里就完成了映射, 所以不需要在 mapper 接口上写 @Mapper 注解
然后在建立一个 DataSource2Config 类
内容和上面的差不多
- package com.ccsert.spdruid.test.config;
- 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.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import javax.sql.DataSource;
- @Configuration
- @MapperScan(basePackages = "com.ccsert.spdruid.test.mapper.two", sqlSessionTemplateRef = "test2SqlSessionTemplate")
- public class DataSource2Config {
- @Bean(name = "test2SqlSessionFactory")
- public SqlSessionFactory testSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception {
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/two/**/*Mapper.xml"));
- return bean.getObject();
- }
- @Bean(name = "test2TransactionManager")
- public DataSourceTransactionManager testTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- @Bean(name = "test2SqlSessionTemplate")
- public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
- return new SqlSessionTemplate(sqlSessionFactory);
- }
- }
注意这两个路径写成自己的不要搞错了
到这里就配置完成了
测试使用
我们先访问一下接口能否调通
启动项目然后使用谷歌插件访问接口
可以看到我们两个接口都调用成功
我们去 druid 的监控界面查看一下执行的 sql 和数据源信息
可以看到 druid 监控了我们执行的两条 sql, 以及两个数据源信息
来源: http://www.bubuko.com/infodetail-3402772.html