baomidou中怎么利用dynamic-datasource实现读写分离
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家介绍baomidou中怎么利用dynamic-datasource实现读写分离,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。maven
千家信息网最后更新 2026年11月03日baomidou中怎么利用dynamic-datasource实现读写分离
这篇文章给大家介绍baomidou中怎么利用dynamic-datasource实现读写分离,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
maven
com.baomidou dynamic-datasource-spring-boot-starter 2.5.7
纯读写分离(mybatis环境)
场景:
在纯的读写分离环境,写操作全部是master,读操作全部是slave。
不想通过注解配置完成以上功能。
答:在mybatis环境下可以基于mybatis插件结合本数据源完成以上功能。 手动注入插件。
@Beanpublic MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin(){ return new MasterSlaveAutoRoutingPlugin();}默认主库名称master,从库名称slave。
问题
我在配置好了之后,调试发现对数据库读的操作不得进入MasterSlaveAutoRoutingPlugin,而且进入了默认的库。只有写进入了MasterSlaveAutoRoutingPlugin中。当然也可以默认为从库,但是感觉就不是很好。
于是我自定义了一个aop切面来,来完成库的选择,代码如下:
import java.lang.reflect.Method;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.context.annotation.Lazy;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import com.baomidou.dynamic.datasource.annotation.DS;import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;import lombok.extern.java.Log;/** * Copyright: Copyright (c) 2019 *说明:动态数据源配置
* * @version: V1.0 * @author: BianPeng * */@Aspect@Component@Order(0)@Lazy(false)@Logpublic class DataSourceAop{ private static final String MASTER = "master"; private static final String SLAVE = "slave"; @Pointcut("execution(* com.buybit.power.service..*.*(..)) || execution(* com.baomidou.mybatisplus.extension.service..*.*(..))") public void checkArgs() { } // 这里切到你的方法目录 @Before("checkArgs()") public void process(JoinPoint joinPoint) throws NoSuchMethodException, SecurityException { String methodName = joinPoint.getSignature().getName(); if (methodName.startsWith("get") || methodName.startsWith("count") || methodName.startsWith("find") || methodName.startsWith("list") || methodName.startsWith("select") || methodName.startsWith("check") || methodName.startsWith("page")) { log.info("当前执行的库:"+SLAVE); DynamicDataSourceContextHolder.push(SLAVE); } else { log.info("当前执行的库:"+MASTER); DynamicDataSourceContextHolder.push(MASTER); } } @After("checkArgs()") public void afterAdvice(){ DynamicDataSourceContextHolder.clear(); }}
但是发现,baomidou/dynamic-datasource自带的@DS没失去了着用,于是我把有@DS的类和方法排除掉,代码入下:
import java.lang.reflect.Method;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.context.annotation.Lazy;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import com.baomidou.dynamic.datasource.annotation.DS;import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;import lombok.extern.java.Log;/** * Copyright: Copyright (c) 2019 *说明:动态数据源配置
* * @version: V1.0 * @author: BianPeng * */@Aspect@Component@Order(0)@Lazy(false)@Logpublic class DataSourceAop{ private static final String MASTER = "master"; private static final String SLAVE = "slave"; @Pointcut("execution(* com.buybit.power.service..*.*(..)) || execution(* com.baomidou.mybatisplus.extension.service..*.*(..))") public void checkArgs() { } // 这里切到你的方法目录 @Before("checkArgs()") public void process(JoinPoint joinPoint) throws NoSuchMethodException, SecurityException { String methodName = joinPoint.getSignature().getName(); Class clazz = joinPoint.getTarget().getClass(); if(clazz.isAnnotationPresent(DS.class)){ //获取类上注解 return; } String targetName = clazz.getSimpleName(); Class[] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes(); Method methdo = clazz.getMethod(methodName,parameterTypes); if (methdo.isAnnotationPresent(DS.class)) { return; } if (methodName.startsWith("get") || methodName.startsWith("count") || methodName.startsWith("find") || methodName.startsWith("list") || methodName.startsWith("select") || methodName.startsWith("check") || methodName.startsWith("page")) { log.info("当前执行的库:"+SLAVE); DynamicDataSourceContextHolder.push(SLAVE); } else { log.info("当前执行的库:"+MASTER); DynamicDataSourceContextHolder.push(MASTER); } } @After("checkArgs()") public void afterAdvice(){ DynamicDataSourceContextHolder.clear(); }}
关于baomidou中怎么利用dynamic-datasource实现读写分离就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
数据
配置
数据源
方法
环境
代码
内容
功能
动态
名称
插件
更多
注解
目录
帮助
不错
兴趣
切面
只有
场景
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
云之行互联网科技
互联网和科技技术
A股互联网金融科技公司
今日互联网科技
互联网金融与金融科技
互联网的科技创新
奇酷互联网络科技
广西互联网科技公司
掌上梦互联网科技
美国互联网前沿科技
安控科技rtu产品工业互联网
宁波青枫互联网科技有限公司
随着互联网科技发展
互联网科技有哪些公司
智腾互联网科技有限公司
互联网科技大奖都有哪些公司
上海创道互联网络科技有限公司
互联网科技年终奖
广州梦谷互联网科技有限公司领导
恒生科技指数 中概互联网
科技平台互联网
互联网最新的科技
互联网科技未来
西安互联网科技公司
链环科技传统企业怎么转型互联网
乌鲁木齐新华智原互联网科技学校
卓翼科技产业互联网合作
中国互联网有什么科技
西安左小互联网科技有限公司
广州全民伙伴互联网科技有限公司