SpringBoot集成多数据源

多数据源就是连接多个数据库

1、在application.properties中配置两个数据库

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8
spring.datasource.test2.username=root
spring.datasource.test2.password=root

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

2、创建两个数据库配置类(DataSourceConfig、DataSourceConfig2)

package com.cppdy.config;

import javax.sql.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;

//配置mapper所在的包
@Configuration
@MapperScan(basePackages="com.cppdy.mapper.test1",sqlSessionTemplateRef="testSqlSessionTemplate")
public class DataSourceConfig {
    
    @Bean(name="testDataSource")
    @ConfigurationProperties(prefix="spring.datasource")
    //primary代表主库
    @Primary
    public DataSource testDateSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean(name="testSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
    
    @Bean(name="testTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
    
    @Bean(name="testSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
package com.cppdy.config;

import javax.sql.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;

//配置mapper所在的包
@Configuration
@MapperScan(basePackages = "com.cppdy.mapper.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate")
public class DataSourceConfig2 {

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource testDateSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "test2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

3、在mapper文件夹下创建test1、tests2文件夹,并在两个文件夹下分别创建(UserMapper、UserMapper2)

package com.cppdy.mapper.test1;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cppdy.entity.User;

@Mapper
public interface UserMapper {

    @Select("select * from user where id=#{id}")
    User getUserById(@Param("id") int id);

    @Insert("insert into user values(NULL,#{name})")
    User insertUser(@Param("name") String name);
    
    @Update("update user set username=#{name} where id=#{id}")
    User update(@Param("id") int id,@Param("name") String name);
    
    @Delete("delete from user where id=#{id}")
    User delete(@Param("id") int id);
}
package com.cppdy.mapper.test2;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.cppdy.entity.User;

@Mapper
public interface UserMapper2 {
    
    @Select("select * from user where id=#{id}")
    User getUserById(@Param("id") int id);

}

4、在HelloWordController中创建测试方法

@RequestMapping("getUserById2")
    public User getUserById2() {

        return userMapper2.getUserById(1);
    }

5、分别调用getUserById和getUserById2方法,测试从两个数据库分别获取用户信息

posted @ 2018-12-01 12:29  知识追求者  阅读(264)  评论(0编辑  收藏  举报