• springboot-配置多个数据源


    1、创建一个datasource包,新建DataSource1,DataSource2两个文件,通过注解来配置数据源

    DataSource1:

    package com.springboot.datasource;
    
    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.DataSourceBuilder;
    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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration//注解到spring容器中
    @MapperScan(basePackages = "com.springboot.data1.mapper",sqlSessionFactoryRef = "data1SqlSessionFactory")
    public class DataSource1 {
    
        /**
         * 返回data1数据库的数据源
         * @return
         */
        @Bean(name="data1Source")
        @Primary//主数据源
        @ConfigurationProperties(prefix = "spring.datasource.data1")
        public DataSource dataSource(){
            return DataSourceBuilder.create().build();
        }
    
        /**
         * 返回data1数据库的会话工厂
         * @param ds
         * @return
         * @throws Exception
         */
        @Bean(name = "data1SqlSessionFactory")
        @Primary
        public SqlSessionFactory sqlSessionFactory(@Qualifier("data1Source") DataSource ds) throws Exception{
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(ds);
            return bean.getObject();
        }
    
        /**
         * 返回data1数据库的会话模板
         * @param sessionFactory
         * @return
         * @throws Exception
         */
        @Bean(name = "data1SqlSessionTemplate")
        @Primary
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data1SqlSessionFactory") SqlSessionFactory sessionFactory) throws  Exception{
            return  new SqlSessionTemplate(sessionFactory);
        }
    
        /**
         * 返回data1数据库的事务
         * @param ds
         * @return
         */
        @Bean(name = "data1TransactionManager")
        @Primary
        public DataSourceTransactionManager transactionManager(@Qualifier("data1Source") DataSource ds){
            return new DataSourceTransactionManager(ds);
        }
    }
    View Code

    DataSource2:

    package com.springboot.datasource;
    
    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.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration//注解到spring容器中
    @MapperScan(basePackages = "com.springboot.data2.mapper",sqlSessionFactoryRef = "data2SqlSessionFactory")
    public class DataSource2 {
    
        /**
         * 返回data2数据库的数据源
         * @return
         */
        @Bean(name="data2Source")
        @ConfigurationProperties(prefix = "spring.datasource.data2")
        public DataSource dataSource(){
            return DataSourceBuilder.create().build();
        }
    
        /**
         * 返回data2数据库的会话工厂
         * @param ds
         * @return
         * @throws Exception
         */
        @Bean(name = "data2SqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("data2Source") DataSource ds) throws Exception{
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(ds);
            return bean.getObject();
        }
    
        /**
         * 返回data2数据库的会话模板
         * @param sessionFactory
         * @return
         * @throws Exception
         */
        @Bean(name = "data2SqlSessionTemplate")
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("data2SqlSessionFactory") SqlSessionFactory sessionFactory) throws  Exception{
            return  new SqlSessionTemplate(sessionFactory);
        }
    
        /**
         * 返回data2数据库的事务
         * @param ds
         * @return
         */
        @Bean(name = "data2TransactionManager")
        public DataSourceTransactionManager transactionManager(@Qualifier("data2Source") DataSource ds){
            return new DataSourceTransactionManager(ds);
        }
    }
    View Code

    2、在application.properties文件配置两个数据源

    spring.datasource.data1.url=jdbc:mysql://127.0.0.1:3306/mysql-boot
    spring.datasource.data1.username=root
    spring.datasource.data1.password=123123
    spring.datasource.data1.driver-class-name=com.mysql.jdbc.Driver
    
    spring.datasource.data2.url=jdbc:mysql://127.0.0.1:3306/mysql-confswh
    spring.datasource.data2.username=root
    spring.datasource.data2.password=123123
    spring.datasource.data2.driver-class-name=com.mysql.jdbc.Driver

    3、两个测试的Service&Mapper

    4、Controller调用

    @RequestMapping("getTopicList")
        public List<Map<String,Object>> getTopicList(){
            List<Map<String,Object>> list1 = userServiceData1.getTopicListData1();
            List<Map<String,Object>> list2 = userServiceData2.getTopicListData2();
            return list1;
        }
  • 相关阅读:
    MySql 获取当前节点及递归所有上级节点
    MySql创建树结构递归查询存储过程
    F2工作流引擎Web层全新扁平化UI上线
    F2工作流引擎参与者类型成员的交、并、互拆计算规则
    F2工作流引擎之组织用户模型(四)
    F2工作流引擎之 工作流运转模型(三)
    F2工作流引擎之 概述(一)
    离线安装docker,并导入docker镜像
    sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set 的解决办法
    yml 文件中使用环境变量
  • 原文地址:https://www.cnblogs.com/lijianda/p/11022892.html
Copyright © 2020-2023  润新知