• Springboot项目的多数据源配置


    spring boot项目配置多个数据源很常见!

    话不多说,上代码。

    首先先在system账号下创建了一个用户test1,并授予权限

    1. create user test1 identified by 123456;
    2. grant connect,resource to test1;

    接下来登录test1用户,创建一个表student

    1. create table student
    2. (
    3. id number primary key,
    4. name varchar2(30),
    5. address varchar2(100)
    6. );

    项目目录如下:

    修改之前的配置文件

    1. spring:
    2. datasource:
    3. driver-class-name: oracle.jdbc.driver.OracleDriver
    4. url: jdbc:oracle:thin:@localhost:1521:ORCL
    5. username: test
    6. password: 123456

    调整后多个数据源后的配置

    1. spring:
    2. datasource:
    3. main:
    4. driver-class-name: oracle.jdbc.driver.OracleDriver
    5. jdbc-url: jdbc:oracle:thin:@localhost:1521:ORCL
    6. username: test
    7. password: 123456
    8. ext:
    9. driver-class-name: oracle.jdbc.driver.OracleDriver
    10. jdbc-url: jdbc:oracle:thin:@localhost:1521:ORCL
    11. username: test1
    12. password: 123456

    数据源配置:

    1. import org.springframework.boot.context.properties.ConfigurationProperties;
    2. import org.springframework.boot.jdbc.DataSourceBuilder;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.context.annotation.Primary;
    6. import javax.sql.DataSource;
    7. /**
    8. * 主数据源配置
    9. */
    10. @Configuration
    11. public class MainDataSource {
    12. @Bean(name = "mainDataSources")
    13. @Primary
    14. @ConfigurationProperties(prefix = "spring.datasource.main")
    15. public DataSource mainDataSource() {
    16. return DataSourceBuilder.create().build();
    17. }
    18. }

    主数据源的mybatis配置

    1. import org.apache.ibatis.session.SqlSessionFactory;
    2. import org.mybatis.spring.SqlSessionFactoryBean;
    3. import org.mybatis.spring.SqlSessionTemplate;
    4. import org.mybatis.spring.annotation.MapperScan;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.beans.factory.annotation.Qualifier;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;
    9. import org.springframework.context.annotation.Primary;
    10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    11. import org.springframework.transaction.PlatformTransactionManager;
    12. import javax.sql.DataSource;
    13. /**
    14. * 主数据源的mybatis配置
    15. */
    16. @Configuration
    17. @MapperScan(basePackages = "com.example.demo.mapper", sqlSessionFactoryRef = "mainSqlSessionFactory", sqlSessionTemplateRef = "mainSqlSessionTemplate")
    18. public class MainMybatisConfig {
    19. @Autowired
    20. private DataSource mainDataSources;
    21. @Bean(name = "mainSqlSessionFactory")
    22. @Primary
    23. public SqlSessionFactory mainSqlSessionFactory() throws Exception {
    24. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    25. bean.setDataSource(mainDataSources);
    26. // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    27. return bean.getObject();
    28. }
    29. //配置声明式事务管理器
    30. @Bean(name = "mainTransactionManager")
    31. @Primary
    32. public PlatformTransactionManager mainTransactionManager() {
    33. return new DataSourceTransactionManager(mainDataSources);
    34. }
    35. @Bean(name = "mainSqlSessionTemplate")
    36. @Primary
    37. public SqlSessionTemplate mainSqlSessionTemplate(
    38. @Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    39. return new SqlSessionTemplate(sqlSessionFactory);
    40. }
    41. }

    副数据源配置:

    1. import org.springframework.beans.factory.annotation.Qualifier;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.boot.jdbc.DataSourceBuilder;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import javax.sql.DataSource;
    7. /**
    8. * 副数据源配置
    9. */
    10. @Configuration
    11. public class ExtDataSource {
    12. @Bean(name = "extDataSources")
    13. @Qualifier(value = "extDataSources")
    14. @ConfigurationProperties(prefix = "spring.datasource.ext")
    15. public DataSource extDataSource() {
    16. return DataSourceBuilder.create().build();
    17. }
    18. }

    副数据源的mybatis配置:

    1. import org.apache.ibatis.session.SqlSessionFactory;
    2. import org.mybatis.spring.SqlSessionFactoryBean;
    3. import org.mybatis.spring.SqlSessionTemplate;
    4. import org.mybatis.spring.annotation.MapperScan;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.beans.factory.annotation.Qualifier;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;
    9. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    11. import org.springframework.transaction.PlatformTransactionManager;
    12. import javax.sql.DataSource;
    13. /**
    14. * 副数据源的mybatis配置
    15. */
    16. @Configuration
    17. @MapperScan(basePackages = "com.example.demo.extMapper", sqlSessionFactoryRef = "extSqlSessionFactory", sqlSessionTemplateRef = "extSqlSessionTemplate")
    18. public class ExtMybatisConfig {
    19. @Autowired
    20. @Qualifier(value = "extDataSources")
    21. private DataSource extDataSource;
    22. @Bean(name = "extSqlSessionFactory")
    23. public SqlSessionFactory extSqlSessionFactory() throws Exception {
    24. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    25. bean.setDataSource(extDataSource);
    26. // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:extMapper/*.xml"));
    27. return bean.getObject();
    28. }
    29. //配置声明式事务管理器
    30. @Bean(name = "extTransactionManager")
    31. public PlatformTransactionManager extTransactionManager() {
    32. return new DataSourceTransactionManager(extDataSource);
    33. }
    34. @Bean(name = "extSqlSessionTemplate")
    35. public SqlSessionTemplate extSqlSessionTemplate(
    36. @Qualifier("extSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    37. return new SqlSessionTemplate(sqlSessionFactory);
    38. }
    39. }

    StudentMapper类

    1. import com.example.demo.entity.Student;
    2. import org.apache.ibatis.annotations.Mapper;
    3. import java.util.List;
    4. @Mapper
    5. public interface StudentMapper {
    6. List queryList();
    7. }

    StudentMapper.xml文件

    1. "1.0" encoding="utf-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.example.demo.extMapper.StudentMapper">
    6. <resultMap type="com.example.demo.entity.Student" id="Result">
    7. <result property="id" column="id"/>
    8. <result property="name" column="name"/>
    9. <result property="address" column="address"/>
    10. resultMap>
    11. <select id="queryList" resultMap="Result">
    12. select id,name,address from student
    13. select>
    14. mapper>

    UserMapper文件

    1. import com.example.demo.entity.User;
    2. import java.util.List;
    3. import org.apache.ibatis.annotations.Mapper;
    4. @Mapper
    5. public interface UserMapper {
    6. List queryList();
    7. }

    UserMapper.xml

    1. "1.0" encoding="utf-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.example.demo.mapper.UserMapper">
    6. <resultMap type="com.example.demo.entity.User" id="userResult">
    7. <result property="id" column="id"/>
    8. <result property="name" column="name"/>
    9. <result property="age" column="age"/>
    10. resultMap>
    11. <select id="queryList" resultMap="userResult">
    12. select id,name,age from "USER"
    13. select>
    14. mapper>

    controller类:

    1. import com.example.demo.entity.Student;
    2. import com.example.demo.entity.User;
    3. import com.example.demo.extMapper.StudentMapper;
    4. import com.example.demo.mapper.UserMapper;
    5. import org.slf4j.Logger;
    6. import org.slf4j.LoggerFactory;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.web.bind.annotation.PostMapping;
    9. import org.springframework.web.bind.annotation.RequestBody;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. import java.util.List;
    13. @RestController
    14. @RequestMapping("/user")
    15. public class UserController {
    16. private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    17. @Autowired
    18. private UserMapper userMapper;
    19. @Autowired
    20. private StudentMapper studentMapper;
    21. @PostMapping("/queryUserList")
    22. public List queryUserList() {
    23. return userMapper.queryList();
    24. }
    25. @PostMapping("/queryStudentList")
    26. public List queryStudentList() {
    27. return studentMapper.queryList();
    28. }
    29. }

    此时查询结果:

    副数据源:

        主数据源:

     若将副数据源的xml文件放到resources目录下

     此时 需要将副数据源的mybatis配置修改下:

  • 相关阅读:
    架构学习——Redis内存数据库学习要点
    LeetCode-解数独(C++)
    2023.10.17
    深入解析Linux Bridge:原理、架构、操作与持久化配置
    logback1.3.x配置详解与实践
    第十八课、Qt 下载、安装与配置
    Jmeter性能测试指南
    Docker常用命令
    [C/C++]数据结构 深入挖掘环形链表问题
    数据库SQL语句:给表添加外键的四种方式
  • 原文地址:https://blog.csdn.net/qq_37342720/article/details/134266679