本文介绍了Spring Boot + MyBatis读写分离,有需要了解Spring+MyBatis读写分离的朋友可参考。希望此文章对各位有所帮助。

其最终实现功能:
其实现原理如下:
在此直接写死使用HikariCP作为数据源
其实现步骤如下:
1.配置及解析类
其配置参数直接使用HikariCP的配置,其具体参数可以参考HikariCP。
在此使用yaml格式,名称为datasource.yaml,内容如下:
dds: write: jdbcUrl: jdbc:mysql://localhost:3306/order password: liu123 username: root maxPoolSize: 10 minIdle: 3 poolName: master read: - jdbcUrl: jdbc:mysql://localhost:3306/test password: liu123 username: root maxPoolSize: 10 minIdle: 3 poolName: slave1 - jdbcUrl: jdbc:mysql://localhost:3306/test2 password: liu123 username: root maxPoolSize: 10 minIdle: 3 poolName: slave2
定义该配置所对应的Bean,名称为DBConfig,内容如下:
@Component
@ConfigurationProperties(locations = "classpath:datasource.yaml", prefix = "dds")
public class DBConfig {
private List<HikariConfig> read;
private HikariConfig write;
public List<HikariConfig> getRead() {
return read;
}
public void setRead(List<HikariConfig> read) {
this.read = read;
}
public HikariConfig getWrite() {
return write;
}
public void setWrite(HikariConfig write) {
this.write = write;
}
}
把配置转换为DataSource的工具类,名称:DataSourceUtil,内容如下:
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
public class DataSourceUtil {
public static DataSource getDataSource(HikariConfig config) {
return new HikariDataSource(config);
}
public static List<DataSource> getDataSource(List<HikariConfig> configs) {
List<DataSource> result = null;
if (configs != null && configs.size() > 0) {
result = new ArrayList<>(configs.size());
for (HikariConfig config : configs) {
result.add(getDataSource(config));
}
} else {
result = new ArrayList<>(0);
}
return result;
}
}
2.注解及动态数据源
定义注解@DataSource,其用于需要对个别方法指定其要使用的数据源(如某个读操作需要在master上执行,但另一读方法b需要在读数据源的具体一台上面执行)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {
/**
* 类型,代表是使用读还是写
* @return
*/
DataSourceType type() default DataSourceType.WRITE;
/**
* 指定要使用的DataSource的名称
* @return
*/
String name() default "";
}
定义数据源类型,分为两种:READ,WRITE,内容如下:
public enum DataSourceType {
READ, WRITE;
}
定义保存这此共享信息的类DynamicDataSourceHolder,在其中定义了两个ThreadLocal和一个map,holder用于保存当前线程的数据源类型(读或者写),pool用于保存数据源名称(如果指定),其内容如下:
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class DynamicDataSourceHolder {
private static final Map<String, DataSourceType> cache = new ConcurrentHashMap<>();
private static final ThreadLocal<DataSourceType> holder = new ThreadLocal<>();
private static final ThreadLocal<String> pool = new ThreadLocal<>();
public static void putToCache(String key, DataSourceType dataSourceType) {
cache.put(key,dataSourceType);
}
public static DataSourceType getFromCach(String key) {
return cache.get(key);
}
public static void putDataSource(DataSourceType dataSourceType) {
holder.set(dataSourceType);
}
public static DataSourceType getDataSource() {
return holder.get();
}
public static void putPoolName(String name) {
if (name != null && name.length() > 0) {
pool.set(name);
}
}
public static String getPoolName() {
return pool.get();
}
public static void clearDataSource() {
holder.remove();
pool.remove();
}
}
动态数据源类为DynamicDataSoruce,其继承自AbstractRoutingDataSource,可以根据返回的key切换到相应的数据源,其内容如下:
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
public class DynamicDataSource extends AbstractRoutingDataSource {
private DataSource writeDataSource;
private List<DataSource> readDataSource;
private int readDataSourceSize;
private Map<String, String> dataSourceMapping = new ConcurrentHashMap<>();
@Override
public void afterPropertiesSet() {
if (this.writeDataSource == null) {
throw new IllegalArgumentException("Property 'writeDataSource' is required");
}
setDefaultTargetDataSource(writeDataSource);
Map<Object, Object> targetDataSource = new HashMap<>();
targetDataSource.put(DataSourceType.WRITE.name(), writeDataSource);
String poolName = ((HikariDataSource)writeDataSource).getPoolName();
if (poolName != null && poolName.length() > 0) {
dataSourceMapping.put(poolName,DataSourceType.WRITE.name());
}
if (this.readDataSource == null) {
readDataSourceSize = 0;
} else {
for (int i = 0; i < readDataSource.size(); i++) {
targetDataSource.put(DataSourceType.READ.name() + i, readDataSource.get(i));
poolName = ((HikariDataSource)readDataSource.get(i)).getPoolName();
if (poolName != null && poolName.length() > 0) {
dataSourceMapping.put(poolName,DataSourceType.READ.name() + i);
}
}
readDataSourceSize = readDataSource.size();
}
setTargetDataSources(targetDataSource);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
DataSourceType dataSourceType = DynamicDataSourceHolder.getDataSource();
String dataSourceName = null;
if (dataSourceType == null ||dataSourceType == DataSourceType.WRITE || readDataSourceSize == 0) {
dataSourceName = DataSourceType.WRITE.name();
} else {
String poolName = DynamicDataSourceHolder.getPoolName();
if (poolName == null) {
int idx = ThreadLocalRandom.current().nextInt(0, readDataSourceSize);
dataSourceName = DataSourceType.READ.name() + idx;
} else {
dataSourceName = dataSourceMapping.get(poolName);
}
}
DynamicDataSourceHolder.clearDataSource();
return dataSourceName;
}
public void setWriteDataSource(DataSource writeDataSource) {
this.writeDataSource = writeDataSource;
}
public void setReadDataSource(List<DataSource> readDataSource) {
this.readDataSource = readDataSource;
}
}
3.AOP拦截
如果在相应的dao层做了自定义配置(指定数据源),则在些处理。解析相应方法上的@DataSource注解,如果存在,并把相应的信息保存至上面的DynamicDataSourceHolder中。在此对com.hfjy.service.order.dao包进行做拦截。内容如下:
import com.hfjy.service.order.anno.DataSource;
import com.hfjy.service.order.wr.DynamicDataSourceHolder;
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.stereotype.Component;
import java.lang.reflect.Method;
/**
* 使用AOP拦截,对需要特殊方法可以指定要使用的数据源名称(对应为连接池名称)
*/
@Aspect
@Component
public class DynamicDataSourceAspect {
@Pointcut("execution(public * com.hfjy.service.order.dao.*.*(*))")
public void dynamic(){}
@Before(value = "dynamic()")
public void beforeOpt(JoinPoint point) {
Object target = point.getTarget();
String methodName = point.getSignature().getName();
Class<?>[] clazz = target.getClass().getInterfaces();
Class<?>[] parameterType = ((MethodSignature)point.getSignature()).getMethod().getParameterTypes();
try {
Method method = clazz[0].getMethod(methodName,parameterType);
if (method != null && method.isAnnotationPresent(DataSource.class)) {
DataSource datasource = method.getAnnotation(DataSource.class);
DynamicDataSourceHolder.putDataSource(datasource.type());
String poolName = datasource.name();
DynamicDataSourceHolder.putPoolName(poolName);
DynamicDataSourceHolder.putToCache(clazz[0].getName() + "." + methodName, datasource.type());
}
} catch (Exception e) {
e.printStackTrace();
}
}
@After(value = "dynamic()")
public void afterOpt(JoinPoint point) {
DynamicDataSourceHolder.clearDataSource();
}
}
4.MyBatis插件
如果在dao层没有指定相应的要使用的数据源,则在此进行拦截,根据是更新还是查询设置数据源的类型,内容如下:
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.Properties;
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class})
})
public class DynamicDataSourcePlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement ms = (MappedStatement)invocation.getArgs()[0];
DataSourceType dataSourceType = null;
if ((dataSourceType = DynamicDataSourceHolder.getFromCach(ms.getId())) == null) {
if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
dataSourceType = DataSourceType.READ;
} else {
dataSourceType = DataSourceType.WRITE;
}
DynamicDataSourceHolder.putToCache(ms.getId(), dataSourceType);
}
DynamicDataSourceHolder.putDataSource(dataSourceType);
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
@Override
public void setProperties(Properties properties) {
}
}
5.整合
在里面定义MyBatis要使用的内容及DataSource,内容如下:
import com.hfjy.service.order.wr.DBConfig;
import com.hfjy.service.order.wr.DataSourceUtil;
import com.hfjy.service.order.wr.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(value = "com.hfjy.service.order.dao", sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {
@Resource
private DBConfig dbConfig;
@Bean(name = "dataSource")
public DynamicDataSource dataSource() {
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setWriteDataSource(DataSourceUtil.getDataSource(dbConfig.getWrite()));
dataSource.setReadDataSource(DataSourceUtil.getDataSource(dbConfig.getRead()));
return dataSource;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
sessionFactoryBean.setDataSource(dataSource);
return sessionFactoryBean.getObject();
}
}
如果不清楚,可以查看github上源码orderdemo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Spring
# MyBatis读写分离
# MyBatis数据读写分离
# SpringMVC4+MyBatis+SQL Server2014实现数据库读写分离
# Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例
# Spring+MyBatis实现数据库读写分离方案
# Spring+Mybatis 实现aop数据库读写分离与多数据库源配置操作
# 要使
# 在此
# 则在
# 两种
# 一台
# 不清楚
# 自定义
# 并对
# 在里面
# 可以根据
# 及其它
# 并把
# 转换为
# 则会
# 配置文件
# 大家多多
# 切换到
# 可以查看
# 所对应
# 情况下
相关文章:
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
建站之星logo尺寸如何设置最合适?
如何快速配置高效服务器建站软件?
油猴 教程,油猴搜脚本为什么会网页无法显示?
平台云上自主建站:模板化设计与智能工具打造高效网站
网页设计网站制作软件,microsoft office哪个可以创建网页?
如何在阿里云通过域名搭建网站?
在线制作视频的网站有哪些,电脑如何制作视频短片?
免费ppt制作网站,有没有值得推荐的免费PPT网站?
如何通过WDCP绑定主域名及创建子域名站点?
惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?
制作企业网站建设方案,怎样建设一个公司网站?
网站制作公司排行榜,四大门户网站排名?
正规网站制作公司有哪些,目前国内哪家网页网站制作设计公司比较专业靠谱?口碑好?
5种Android数据存储方式汇总
宝塔面板创建网站无法访问?如何快速排查修复?
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
常州企业建站如何选择最佳模板?
电商平台网站制作流程,电商网站如何制作?
建站之星如何实现网站加密操作?
建站之星ASP如何实现CMS高效搭建与安全管理?
如何基于PHP生成高效IDC网络公司建站源码?
定制建站流程步骤详解:一站式方案设计与开发指南
东莞专业制作网站的公司,东莞大学生网的网址是什么?
Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解
黑客如何利用漏洞与弱口令入侵网站服务器?
南宁网站建设制作定制,南宁网站建设可以定制吗?
如何通过商城免费建站系统源码自定义网站主题?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
一键网站制作软件,义乌购一件代发流程?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
如何选择高效响应式自助建站源码系统?
如何正确下载安装西数主机建站助手?
如何撰写建站申请书?关键要点有哪些?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
如何用美橙互联一键搭建多站合一网站?
如何快速生成可下载的建站源码工具?
网站制作大概多少钱一个,做一个平台网站大概多少钱?
微信小程序制作网站有哪些,微信小程序需要做网站吗?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
如何在阿里云高效完成企业建站全流程?
php8.4新语法match怎么用_php8.4match表达式替代switch【方法】
如何选择域名并搭建高效网站?
制作国外网站的软件,国外有哪些比较优质的网站推荐?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
香港服务器部署网站为何提示未备案?
,石家庄四十八中学官网?
如何正确选择百度移动适配建站域名?
*请认真填写需求信息,我们会在24小时内与您取得联系。