zoukankan      html  css  js  c++  java
  • 037.[转] springboot 配置多个数据源

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

    #默认使用 tomcat-jdbc
    spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
    
    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

     2、创建一个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);
        }
    }

    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);
        }
    }

    3、测试 Service&Mapper

        @RequestMapping("getTopicList")
        public List<Map<String,Object>> getTopicList(){
            List<Map<String,Object>> list1 = userServiceData1.getTopicListData1();
            List<Map<String,Object>> list2 = userServiceData2.getTopicListData2();
            return list1;
        }

    https://www.cnblogs.com/lijianda/p/11022892.html

    附:

    http://www.demodashi.com/demo/14622.html

  • 相关阅读:
    线性最小二乘两种方法
    Coursera machine learning 第二周 编程作业 Linear Regression
    Coursera machine learning 第二周 quiz 答案 Octave/Matlab Tutorial
    Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables
    Codeforces Round #392 (Div. 2) F. Geometrical Progression
    四边形不等式优化DP——石子合并问题 学习笔记
    Codeforces Round #373 (Div. 2) E. Sasha and Array
    hihoCoder #1388 : Periodic Signal
    hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
    Java动态代理分析
  • 原文地址:https://www.cnblogs.com/badboyh2o/p/11258402.html
Copyright © 2011-2022 走看看