mybatis+springboot+druid多数据源配置

作者: adm 分类: java,mybatis 发布时间: 2021-08-05 22:26

现在数据库中间件基本取代了多数据源配置,大家可以去学习一下MyCat以及SharingSphere,有空的时候和大家分享一波。这次主要介绍的是一个项目中多数据源的配置,可以达到读写分离的效果,加上aop,简直就是一个注解无缝插入,有空也会上传aop的代码。大家一起学习一起进步。

整体梳理过程

  1. 加上注解
  2. application.properties/yml中配置
  3. 注入datasource和SqlsessionTemplate,并配置bean名称和@MapperScan
  4. 写自己的mapper
  5. 单测

0.注意点

加上这段annotation@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})

1.在application.properties中配置如下


spring.datasource.gfu00.driver-class-name= ****
spring.datasource.gfu00.url= ****
spring.datasource.gfu00.username= ****
spring.datasource.gfu00.password= ****

spring.datasource.gfu01.driver-class-name= ****
spring.datasource.gfu01.url = ****
spring.datasource.gfu01.username = ****
spring.datasource.gfu01.password = ****

2.配置多个datasource,并用druid创建datasource。此处也可用DataSourceBuilder.create().build()

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
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 javax.sql.DataSource;
/**
* multi-datasource config
*
* @author QY
* @date 2019/10/18
*/
@Configuration
public class DataSourceConfig {
@Bean("gfu00")
@ConfigurationProperties(prefix = "spring.datasource.gfu00")
public DataSource gfu00() {
return DruidDataSourceBuilder.create().build();
}

@Bean("gfu01")
@ConfigurationProperties(prefix = "spring.datasource.gfu01")
public DataSource gfu01() {
return DruidDataSourceBuilder.create().build();
}

3.配置SqlSessionTemplate

3.1配置SqlSessionTemplate01

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

import javax.sql.DataSource;
/**
* @author QY
* @date 2019/10/18
*/

@Configuration
@MapperScan(basePackages = {"org.sjframework.learn.mybatis.dao"}, sqlSessionFactoryRef = "sqlSession00")
public class Gfu00Config {
@Autowired
@Qualifier("gfu00")
private DataSource gfu00;

@Bean
public SqlSessionFactory sqlSession00() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(gfu00);
return factoryBean.getObject();
}

@Bean("sessionTemplate00")
public SqlSessionTemplate sessionTemplate00() throws Exception {
return new SqlSessionTemplate(sqlSession00());
}

}

3.2配置SqlSessionTemplate01

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

import javax.sql.DataSource;
/**

* @author QY
* @date 2019/10/18
*/

@Configuration
@MapperScan(basePackages = {"org.sjframework.learn.mybatis.read"}, sqlSessionFactoryRef = "sqlSession01")
public class Gfu01Config {

@Autowired
@Qualifier("gfu01")
private DataSource gfu01;

@Bean
public SqlSessionFactory sqlSession01() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(gfu01);
return factoryBean.getObject();
}

@Bean("sessionTemplate01")
public SqlSessionTemplate sessionTemplate01() throws Exception {
return new SqlSessionTemplate(sqlSession01());
}

}

4.测试类

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.SqlSessionTemplate;
import org.sjframework.learn.mybatis.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* mybatis test
*
* @author QY
* @date 2019/10/17
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class})
public class MultiDataSourceTest {

@Autowired
UserDao userDao;

@Autowired
@Qualifier("sessionTemplate00")
SqlSessionTemplate sessionTemplate00;

@Autowired
@Qualifier("sessionTemplate01")
SqlSessionTemplate sessionTemplate01;

@Test
public void loadContext() {
Page<Map<String, String>> page = new Page<>(2, 100);

Object userId00 = sessionTemplate00.selectOne("org.sjframework.learn.mybatis.dao.UserDao.multiDataSource", "userId");
Object userId01 = sessionTemplate01.selectOne("org.sjframework.learn.mybatis.read.UserDaoRead.multiDataSource", "userId");

System.out.println(userId00);
}

}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!