mybatis+springboot+druid多数据源配置
现在数据库中间件基本取代了多数据源配置,大家可以去学习一下MyCat以及SharingSphere,有空的时候和大家分享一波。这次主要介绍的是一个项目中多数据源的配置,可以达到读写分离的效果,加上aop,简直就是一个注解无缝插入,有空也会上传aop的代码。大家一起学习一起进步。
整体梳理过程
- 加上注解
- application.properties/yml中配置
- 注入datasource和SqlsessionTemplate,并配置bean名称和@MapperScan
- 写自己的mapper
- 单测
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); } }
更多阅读
- gRPC(Go)入门教程(五)---gPRC拦截器Interceptor
- gRPC(Go)入门教程(六)---提升服务安全性之自定义身份校验
- gRPC(Go)入门教程(七)---利用Gateway同时提供HTTP和RPC服务
- gRPC(Go)入门教程(八)---使用context进行超时控制
- gRPC(Go)入门教程(九)---配置retry自动重试
- gRPC(Go)入门教程(十)------gRPC压测工具ghz
- gRPC(Go)入门教程(十一)---NameResolver 实战及原理分析
- gRPC(Go)入门教程(十二)---客户端负载均衡
- gRPC(Go)入门教程(十三)---Kubernetes 环境下的 gRPC 负载均衡
- 海关179对接
标签云