IDE下的springboot+mybatis学习笔记(4)--------多数据源配置

文章目录

  • 前言
  • application.yml
  • 两份config配置
    • firstMybatisConfig
    • secondMybatisConfig
  • 目录结构
  • 测试
  • 总结

前言

多数据源这个是很久之前做的了,参考了别人大佬的配置,但现在找不到之前那位大佬的链接了,如果找到原创大佬请提醒我贴上链接,谢谢!

application.yml

之前用的数据源都是系统默认的所以开头用spring,但现在是多数据源所以需要自己配置

first:
  datasource:
    password: root
    username: root
    jdbc-url: jdbc:mysql://localhost:3306/powerbusiness?serverTimezone=UTC&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver

second:
  datasource:
    password: root
    username: root
    jdbc-url: jdbc:mysql://localhost:3306/drugs?serverTimezone=UTC&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver

两份config配置

firstMybatisConfig

package com.example.demo.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.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.example.demo.firstMapper"}, sqlSessionTemplateRef = "userSqlSessionTemplate")
public class firstMybatisConfig {
    @Bean(name = "userDataSource")
    @Primary //必须加此注解,不然报错,下一个类则不需要添加  这个是代表默认使用的数据源
    @ConfigurationProperties(prefix="first.datasource")// prefix值必须是application.properteis中对应属性的前缀
    public DataSource userDataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean
    public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //添加XML目录
        ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mapper/first/*.xml"));
        return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }

}

secondMybatisConfig

package com.example.demo.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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.example.demo.secondMapper"}, sqlSessionTemplateRef = "autoTestSqlSessionTemplate")
public class secondMybatisConfig {
    @Bean(name = "autoTestDataSource")
    @ConfigurationProperties(prefix="second.datasource")// prefix值必须是application.properteis中对应属性的前缀
    public DataSource autoTestDataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean
    public SqlSessionFactory autoTestSqlSessionFactory(@Qualifier("autoTestDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //添加XML目录
        ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mapper/second/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    @Bean
    public SqlSessionTemplate autoTestSqlSessionTemplate(@Qualifier("autoTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
                SqlSessionTemplate template = new  SqlSessionTemplate(sqlSessionFactory);
                return template;
    }


}

目录结构

以上配置已经完成了,需要注意的是关于Mybatis中的Mapper文件和Mapper.xml文件位置要区分好,配置中文件位置也要对应好
IDE下的springboot+mybatis学习笔记(4)--------多数据源配置_第1张图片

测试

关于service层的代码就不贴了,这里和正常的使用都是一样的,就贴个controller层的吧
controller

@Controller
public class TestServiceController {
    @Autowired
    private TestService testService;
    //  查询全部
    @ResponseBody
    @RequestMapping(value = "/first")
    public E3Result selectAll(){
        return testService.firstDao();
    }

    @ResponseBody
    @RequestMapping(value = "/second")
    public E3Result select(){
        return testService.secondDao();
    }
}

测试结果
first数据库:
IDE下的springboot+mybatis学习笔记(4)--------多数据源配置_第2张图片
second数据库:
IDE下的springboot+mybatis学习笔记(4)--------多数据源配置_第3张图片

总结

多数据源的配置就要舍弃采用之前系统默认的数据库配置,配置一个数据源就得配置一份配置文件与之对应,这里再次申明这个是很久之前参考大佬的做的,如果找到原创请告知附上链接

源码已上传: https://github.com/CohLins/TwoSqlResource.git.

你可能感兴趣的:(springboot,mybatis,mysql,spring,boot)