基于注解实现SpringBoot多数据源配置
1.功能介绍
在实际的开发中,同一个项目中使用多个数据源是很常见的场景。最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能。具体实现方式如下。
2.在application.properties中添加多数据源配置
添加多个数据源和mapper文件路径配置,此配置用于基于java的配置数据源中使用。
#数据库配置
spring.datasource.demo.user.url=jdbc:mysql://xxx.xx.xx.xx:3306/demo-user
spring.datasource.demo.user.username=xxxx
spring.datasource.demo.user.password=xxxx
spring.datasource.demo.user.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.demo.server.url=jdbc:mysql://xxx.xx.xx.xx:3306/springbootdemo
spring.datasource.demo.server.username=xxxx
spring.datasource.demo.server.password=xxxx
spring.datasource.demo.server.driver-class-name=com.mysql.jdbc.Driver
#mapper文件的路径
mybatis.demo.server.mapper-location=classpath*:mapper/demo-server/*.xml
mybatis.demo.user.mapper-location=classpath*:mapper/demo-user/*.xml
3.基于java的方式实现数据库配置
配置类图如下:

其中DemoUserDbConfig类源代码如下:
其中Configuration注解表识此类为Spring的配置类。
MapperScan注解中的basePackages、annotationClass、sqlSessionTemplateRef用于配置此数据库链接扫描com.example包中所有注解为DemoUserMapper的接口。
@Configuration
@MapperScan(basePackages = {"com.example"},annotationClass = DemoUserMapper.class,
sqlSessionTemplateRef = "demoUserTemplate")
public class DemoUserDbConfig extends AbstractDbConfig {
@Value("${spring.datasource.demo.user.url}")
private String url;
@Value("${spring.datasource.demo.user.username}")
private String userName;
@Value("${spring.datasource.demo.user.password}")
private String password;
@Value("${spring.datasource.demo.user.driver-class-name}")
private String driveClassName;
@Value(value = "${mybatis.demo.user.mapper-location}")
private String mapperLocation;
@Bean(name = "demoUser")
public DataSource secondaryDataSource() {
return dataSourceFactory(driveClassName, url, userName, password);
}
@Bean(name = "demoUserTemplate")
public SqlSessionTemplate demoUserSqlTemplate() throws Exception {
return new SqlSessionTemplate((sqlSessionFactory(secondaryDataSource(), mapperLocation)));
}
@Bean
@Qualifier("demoUserTransaction")
public PlatformTransactionManager demoUserTransaction() {
return new DataSourceTransactionManager(secondaryDataSource());
}
}
其中AbstractDatasource设置了通用的方法,源代码如下:
public abstract class AbstractDbConfig {
protected SqlSessionFactory sqlSessionFactory(DataSource dataSource, String mapperLocation) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Resource[] resource= resourceResolver.getResources(mapperLocation);
factoryBean.setMapperLocations(resource);
return factoryBean.getObject();
}
protected DataSource dataSourceFactory(String driveClassName, String url, String userName, String password){
DruidDataSource datasource = new DruidDataSource();
datasource.setDriverClassName(driveClassName);
datasource.setUrl(url);
datasource.setUsername(userName);
datasource.setPassword(password);
datasource.setMaxActive(20);
datasource.setInitialSize(20);
return datasource;
}
}
使用相同的方法定义其他数据源。
4.定义接口和mapper文件
分别定义连接demo-user数据库和springbootdemo数据库的mapper文件。连接demo-user数据库的接口如下,使用DemoUserMapper注解表识。
@DemoUserMapper
public interface UserDao {
/**
* 返回所有的dictionary列表
*
* @return 所有的dictionary列表
*/
List<String> list();
}
mapper文件如下:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.multidatasource.business.user.UserDao">
<select id="list" resultType="string">
SELECT `name` FROM `user`
</select>
</mapper>
定义读取springbootdemo数据库的接口,代码如下。使用DemoServerMapper注解表识
@DemoServerMapper
public interface DictionaryDao {
/**
* 返回所有的dictionary列表
*
* @return 所有的dictionary列表
*/
List<Dictionary> list();
/**
* 查询此key下的所有子节点
*
* @param key 数据字典key
* @return 返回key所有的子节点列表
*/
List<Dictionary> listChildrenByKey(String key);
/**
* 插入数据到数据库
*
* @param dictionary
*/
void insert(Dictionary dictionary);
}
mapper文件代码如下:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.multidatasource.business.dictionary.dao.DictionaryDao">
<resultMap id="DictionaryResultMap" type="com.example.multidatasource.business.dictionary.Dictionary">
<result property="id" column="id"></result>
<result property="dictKey" column="dict_key"></result>
<result property="dictValue" column="dict_value"></result>
<result property="parentId" column="parent_id"></result>
<result property="description" column="description"></result>
</resultMap>
<select id="list" resultMap="DictionaryResultMap">
SELECT * FROM `dictionary`
</select>
<select id="listChildrenByKey" resultMap="DictionaryResultMap">
SELECT * FROM dictionary where parent_id= (select id from dictionary where dict_key= #{key})
</select>
<delete id="delete" parameterType="int">
delete from dictionary where id = #{id}
</delete>
<insert id="insert" parameterType="com.example.multidatasource.business.dictionary.Dictionary">
INSERT INTO `dictionary`(`dict_key`,`dict_value`,`parent_id`,`description`)
VALUES(#{dictKey}, #{dictValue}, #{parentId}, #{description})
</insert>
</mapper>
5.定义注解
定义DemoUserMapper和DemoServerMapper注解,分别作为使用demo-user和springbootdemo数据库的表识。
定义代码如下:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
@Mapper
public @interface DemoServerMapper {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
String value() default "";
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
@Mapper
public @interface DemoUserMapper {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
String value() default "";
}
6.使用单元测试验证配置
编写单元测试代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MultiDatasourceApplicationTests {
@Autowired
private DictionaryDao dictionaryDao;
@Autowired
private UserDao userDao;
@Test
public void contextLoads() {
System.out.println(dictionaryDao.list());
System.out.println("===================");
System.out.println(userDao.list());
}
}
基于注解实现SpringBoot多数据源配置的更多相关文章
- 基于注解的Spring多数据源配置和使用(非事务)
原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...
- 基于注解的Spring多数据源配置和使用
前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...
- 基于xml的Spring多数据源配置和使用
上一篇讲了<基于注解的Spring多数据源配置和使用>,通过在类或者方法上添加@DataSource注解就可以指定某个数据源.这种方式的优点是控制粒度细,也更灵活. 但是当有些时候项目分模 ...
- 基于注解的springboot+mybatis的多数据源组件的实现
通常业务开发中,我们会使用到多个数据源,比如,部分数据存在mysql实例中,部分数据是在oracle数据库中,那这时候,项目基于springboot和mybatis,其实只需要配置两个数据源即可,只需 ...
- Springboot 多数据源配置,结合tk-mybatis
一.前言 作为一个资深的CRUD工程师,我们在实际使用springboot开发项目的时候,难免会遇到同时使用多个数据库的情况,比如前脚刚查询mysql,后脚就要查询sqlserver. 这时,我们很直 ...
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- spring基于通用Dao的多数据源配置详解【ds1】
spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...
- mybatis基于注解形式的多数据源
最近在做一个系统管理项目,需要使用到多数据源,尝试了注解形式和xml形式的多数据源配置,以下是基于注解形式的Mybatis多数据源配置. 1.application.yml 配置文件 database ...
- Spring-Boot 多数据源配置+动态数据源切换+多数据源事物配置实现主从数据库存储分离
一.基础介绍 多数据源字面意思,比如说二个数据库,甚至不同类型的数据库.在用SpringBoot开发项目时,随着业务量的扩大,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源. ...
随机推荐
- Mongo学习
几种可能存在的实体类型 public class AAA{ public List<Common> CommonList{get;set;} } public class BBB{ pub ...
- netty-socketio即时通讯
jar包和依赖包在360云盘中:所有文件 > 学习 > jar包 > netty-socketio-1.7.10以及依赖 原文链接:http://www.cnblogs.com/al ...
- springclould feign客户端添加全局参数
用springclould feign作为调用服务的客户端,一般来说参数可以写在feignclient的方法参数里 有时需要所有feign请求都统一添加一些参数,例如token用于鉴权等,可以这样做: ...
- 用python格式化小说txt
下载了<无人生还>的txt版.传到手机,发现阅读器识别得不够好. 原文格式如下: 第一章 一 沃格雷夫法官先生新近离任退休,现在正在头等车厢的吸烟室里,倚角而坐,一 边喷着雪茄烟,一边兴致 ...
- 洛谷 P1334 瑞瑞的木板==P2664 【题目待添加】
题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了一根足够长的木板,长度为 ...
- eclipse中增加matplotlib、web应用’和pip框架包
由于python主要应用在Linux下和相关的vc下,对于熟悉eclipse的我来说,这是一个难题,通过在命令行中转pip可以安装python任何信息,具体的插件直接在一下网页中搜索https://p ...
- Luogu P4593 [TJOI2018]教科书般的亵渎
亵渎终于离开标准了,然而铺场快攻也变少了 给一个大力枚举(无任何性质)+艹出自然数幂和的方法,但是复杂度极限是\(O(k^4)\)的,不过跑的好快233 首先简单数学分析可以得出\(k=m+1\),因 ...
- VC-基础:VC++动态链接库(DLL)编程深入浅出
1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静 ...
- 企业自颁布服务器证书的有效性验证(C#为例)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/notjusttech/article/details/72779904 目前根据项目的需要,整理了一 ...
- activiti整合开发实例总结
参考手册:http://www.mossle.com/docs/activiti/ 一.applicationContext.xml中引入activiti相关配置的xml文件 <!-- begi ...