SpringBoot實(shí)現(xiàn)分庫分表
方案:可以使用攔截器攔截mybatis框架,在執(zhí)行SQL前對SQL語句根據(jù)路由字段進(jìn)行分庫分表操作,下例只做分表功能
@Intercepts:申明需要攔截的方法
攔截StatementHandler對象
一、statementHandler對象的定義
首先我們先來看看statementHandler接口的定義:
首先約定文中將的四大對象是指:executor, statementHandler,parameterHandler,resultHandler對象。
SimpleStatementHandler:對應(yīng)我們JDBC中常用的Statement接口,用于簡單SQL的處理;PreparedStatementHandler:對應(yīng)JDBC中的PreparedStatement,預(yù)編譯SQL的接口;CallableStatementHandler:對應(yīng)JDBC中CallableStatement,用于執(zhí)行存儲(chǔ)過程相關(guān)的接口;RoutingStatementHandler:這個(gè)接口是以上三個(gè)接口的路由,沒有實(shí)際操作,只是負(fù)責(zé)上面三個(gè)StatementHandler的創(chuàng)建及調(diào)用。
講到statementHandler,毫無疑問它是我們四大對象最重要的一個(gè),它的任務(wù)就是和數(shù)據(jù)庫對話。在它這里會(huì)使用parameterHandler和ResultHandler對象為我們綁定SQL參數(shù)和組裝最后的結(jié)果返回。
public interface StatementHandler {
Statement prepare(Connection connection)
throws SQLException;
void parameterize(Statement statement)
throws SQLException;
void batch(Statement statement)
throws SQLException;
int update(Statement statement)
throws SQLException;
<E> List<E> query(Statement statement, ResultHandler resultHandler)
throws SQLException;
BoundSql getBoundSql();
ParameterHandler getParameterHandler();
}
二、prepare方法
1、首先prepare方法是用來編譯SQL
讓我們看看它的源碼實(shí)現(xiàn)。這里我們看到了BaseStatementHandler對prepare方法的實(shí)現(xiàn)
@Override
public Statement prepare(Connection connection) throws SQLException {
ErrorContext.instance().sql(boundSql.getSql());
Statement statement = null;
try {
statement = instantiateStatement(connection);
setStatementTimeout(statement);
setFetchSize(statement);
return statement;
} catch (SQLException e) {
closeStatement(statement);
throw e;
} catch (Exception e) {
closeStatement(statement);
throw new ExecutorException("Error preparing statement. Cause: " + e, e);
}
}
protected abstract Statement instantiateStatement(Connection connection) throws SQLException;
顯然我們通過源碼更加關(guān)注抽象方法instantiateStatement是做了什么事情。它依舊是一個(gè)抽象方法,那么它就有其實(shí)現(xiàn)類。
2、那就是之前說的那幾個(gè)具體的StatementHandler對象
讓我們看看PreparedStatementHandler:
@Override
protected Statement instantiateStatement(Connection connection) throws SQLException {
String sql = boundSql.getSql();
if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
String[] keyColumnNames = mappedStatement.getKeyColumns();
if (keyColumnNames == null) {
return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
} else {
return connection.prepareStatement(sql, keyColumnNames);
}
} else if (mappedStatement.getResultSetType() != null) {
return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
} else {
return connection.prepareStatement(sql);
}
}
好這個(gè)方法非常簡單,我們可以看到它主要是根據(jù)上下文來預(yù)編譯SQL,這是我們還沒有設(shè)置參數(shù)。設(shè)置參數(shù)的任務(wù)是交由,statement接口的parameterize方法來實(shí)現(xiàn)的。
3、parameterize方法
上面我們在prepare方法里面預(yù)編譯了SQL。那么我們這個(gè)時(shí)候希望設(shè)置參數(shù)。在Statement中我們是使用parameterize方法進(jìn)行設(shè)置參數(shù)的。
讓我們看看PreparedStatementHandler中的parameterize方法:
@Override ?
? public void parameterize(Statement statement) throws SQLException { ?
? ? parameterHandler.setParameters((PreparedStatement) statement); ?
? } ?很顯然這里很簡單是通過parameterHandler來實(shí)現(xiàn)的,我們這篇文章只是停留在statementhandler的程度,等我們講解parameterHandler的時(shí)候再來看它如何實(shí)現(xiàn)吧,期待一下吧。
4、query/update方法
我們用了prepare方法預(yù)編譯了SQL,用了parameterize方法設(shè)置參數(shù),那么我們接下來肯定是想執(zhí)行SQL,而SQL無非是兩種:
一種是進(jìn)行查詢——query,另外就是更新——update。
這些方法都很簡單,讓我們看看PreparedStatementHandler的實(shí)現(xiàn):
@Override
public int update(Statement statement) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
int rows = ps.getUpdateCount();
Object parameterObject = boundSql.getParameterObject();
KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject);
return rows;
}
@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
return resultSetHandler.<E> handleResultSets(ps);
}
例:動(dòng)態(tài)替換SQL中@TableID標(biāo)識符
package com.study.demo.interceptor;
import com.study.demo.exception.BaseException;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Component
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class DynamicSQLInterceptor implements Interceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(DynamicSQLInterceptor.class);
private static final String SHARD_TABLE_ID = "SHARD_TABLE_ID";
private static final String DEFAULT_TABLE_ID = "000";
@Override
@SuppressWarnings("unchecked")
public Object intercept(Invocation invocation) throws Throwable {
LOGGER.info("DynamicSQLInterceptor.intercept() exec.");
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
Object parameter = statementHandler.getParameterHandler().getParameterObject();
Map<String, Object> params = (Map)parameter;
if(CollectionUtils.isEmpty(params)){
throw new BaseException("SQL: 路由字段不能為空!");
}
String tableId = DEFAULT_TABLE_ID;
Set<String> keySet = params.keySet();
for (String key : keySet) {
if (SHARD_TABLE_ID.equals(key)) {
tableId = String.valueOf(params.get(key));
}
}
BoundSql boundSql = statementHandler.getBoundSql();
//獲取到原始sql語句
String sql = boundSql.getSql();
String newSql = sql.replaceAll("@TableID", tableId);
LOGGER.debug("[DynamicSQLInterceptor] Sql:{}", newSql);
//通過反射修改sql語句
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, newSql);
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
//只攔截Executor對象,減少目標(biāo)被代理的次數(shù)
if (target instanceof StatementHandler) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
@Override
public void setProperties(Properties properties) {
LOGGER.debug("[DynamicSQLInterceptor] SetProperties");
}
}
示例SQL:
SELECT * FROM ST_CLASS_@TableID WHERE ID = #{id}service層示例:
@Override
public Objcet queryByPrimaryKey(String id) {
? ? Map<String, Object> params = DbShardUtils.shardDBParamMap(id);
? ? params.put("id", id);
?? ?return testDao.queryByPrimaryKey(params);
}dao層示例:
@Repository
public interface TestDao {
Object queryByPrimaryKey(Map<String, Object> params);
}
package com.study.demo.utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* 分庫分表工具類 <br>
* 返回Map<String, Object>, 含有key:SHARD_TABLE_ID
*/
public class DbShardUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(DbShardUtils.class);
private static final String SHARD_TABLE_ID = "SHARD_TABLE_ID";
/**
* 私有構(gòu)造函數(shù)
*/
private DbShardUtils() {
}
public static Map<String, Object> shardDBParamMap(String id){
if (StringUtils.isBlank(id)) {
LOGGER.error("sharding id is null");
}
Map<String, Object> paramMap = new HashMap<>();
paramMap.put(SHARD_TABLE_ID, rout(id));
return paramMap;
}
private static String rout(String id) {
// 測試
return "000";
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot?如何使用sharding?jdbc進(jìn)行分庫分表
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)自定義分庫分表的實(shí)踐
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫分表與讀寫分離的示例
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫分表功能
- springboot jpa分庫分表項(xiàng)目實(shí)現(xiàn)過程詳解
- Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼
- SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫分表
- Spring Boot 分庫分表策略示例展示
相關(guān)文章
Java模板動(dòng)態(tài)生成word文件的方法步驟
最近項(xiàng)目中需要根據(jù)模板生成word文檔,模板文件也是word文檔。本文使用使用freemarker模板生成word文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案
這篇文章主要介紹了SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案,文中通過代碼示例和圖文講解的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下2024-05-05
SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
本文介紹了在SpringBoot中配置多個(gè)JdbcTemplate的方法,包括創(chuàng)建項(xiàng)目、添加依賴、配置數(shù)據(jù)源和多個(gè)JdbcTemplate的使用,感興趣的可以了解一下2024-11-11
詳細(xì)分析Java并發(fā)集合LinkedBlockingQueue的用法
這篇文章主要介紹了詳細(xì)分析Java并發(fā)集合LinkedBlockingQueue的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

