Springboot -简单使用mybatis 构建多数据源应用

本文转自:https://blog.csdn.net/qq_15071263/article/details/96591059

文章目录

Springboot - 使用mybatis 构建多数据源应用
1、初始化2个数据库
3、初始化数据
4、创建一个springboot 应用
5、编写配置文件
6、编写数据库配置
7、编写数据源配置
8、编写实体类
9、编写Mapper 接口
10、编写测试代码进行测试
Springboot - 使用mybatis 构建多数据源应用

1、初始化2个数据库

使用docker 构建2个数据库

docker run --name mysql-1 -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

docker run --name mysql-2 -p 3307:3306 -d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

 

创建完毕


3、初始化数据

我们创建2个数据库,每个库建立一张表,插入一下测试数据

4、创建一个springboot 应用

创建过程省略

5、编写配置文件

spring:
  datasource:
    rtx:
      jdbc-url: jdbc:mysql://localhost:3306/rtx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password:
    titan:
      jdbc-url: jdbc:mysql://localhost:3307/titan?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: root

6、编写数据库配置


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

/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:02.
 * © All Rights Reserved.
 */

@Configuration
public class DataSourceConfig {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.rtx")
    public DataSource rtx(){
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.titan")
    public DataSource titan(){
        return DataSourceBuilder.create().build();
    }
}


7、编写数据源配置

数据源一


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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:06.
 * © All Rights Reserved.
 */

@Configuration
@MapperScan(basePackages = "com.wretchant.manydb.mapper.rtx",sqlSessionFactoryRef = "sessionFactoryRTX" )
public class RTXMyBatisDataSourceConfig {


    @Bean
    @Primary
    public SqlSessionFactory sessionFactoryRTX(@Qualifier("rtx")DataSource dataSource)throws Exception{
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        return sessionFactoryBean.getObject();
    }

    @Bean
    @Primary
    public SqlSessionTemplate sessionTemplateRTX(@Qualifier("sessionFactoryRTX") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}


数据源二

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:12.
 * © All Rights Reserved.
 */
@Configuration
@MapperScan(basePackages = "com.wretchant.manydb.mapper.titan",sqlSessionFactoryRef = "sessionFactoryTitan" )
public class TITANMyBatisDataSourceConfig {


    @Bean
    public SqlSessionFactory sessionFactoryTitan(@Qualifier("titan") DataSource dataSource)throws Exception{
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        return sessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sessionTemplateTitan(@Qualifier("sessionFactoryTitan")SqlSessionFactory sqlSessionFactory)throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}


8、编写实体类

/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:36.
 * © All Rights Reserved.
 */
@Data
public class Amd {

    private Integer id;
    private String name;
}


/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:36.
 * © All Rights Reserved.
 */

@Data
public class Inter {

    private Integer id;
    private String name;

}


9、编写Mapper 接口

import com.wretchant.manydb.domain.Inter;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:37.
 * © All Rights Reserved.
 */
@Repository
public interface InterMapper {

    @Select("select * from inter")
    List<Inter> findAll();
}


import com.wretchant.manydb.domain.Amd;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Created by 谭健 on 2019/7/19. 星期五. 10:38.
 * © All Rights Reserved.
 */
@Repository
public interface AmdMapper {

    @Select("select * from amd")
    List<Amd> findAll();
}



10、编写测试代码进行测试


import com.wretchant.manydb.mapper.rtx.InterMapper;
import com.wretchant.manydb.mapper.titan.AmdMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ManyDbApplicationTests {

    @Autowired
    private InterMapper interMapper;
    @Autowired
    private AmdMapper amdMapper;

    @Test
    public void contextLoads() {
        Assert.assertEquals(5, interMapper.findAll().size());
        Assert.assertEquals(6, amdMapper.findAll().size());

        interMapper.findAll().forEach(System.out::println);
        amdMapper.findAll().forEach(System.out::println);
    }

}

 

还可参考:

https://blog.csdn.net/mxw2552261/article/details/78640062

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值