您好,登錄后才能下訂單哦!
大家創建這三個類,配合> https://blog.51cto.com/12133802/2418053 使用即可
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DynamicDataSourceHolder.getDBType();
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DynamicDataSourceHolder {
private static Logger logger=LoggerFactory.getLogger(DynamicDataSourceHolder.class);
private static ThreadLocal<String> contextHolder=new ThreadLocal<String>();
public static final String DB_MASTER = "master";
public static final String DB_SLAVE="slave";
public static String getDBType() {
String db=contextHolder.get();
if(db==null) {
db=DB_MASTER;
}
return db;
}
/**
* 設置線程的DBType
* @param str
*/
public static void setDBType(String str) {
logger.debug("所使用的數據源為:"+str);
contextHolder.set(str);
}
/**
* 清理線程
*/
public static void clearDBType() {
contextHolder.remove();
}
import java.util.Locale;
import java.util.Properties;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
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.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionSynchronizationManager;
//增刪改的操作都在update里面,RowBounds.class,ResultHandler.class是處理返回結果的
@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 DynamicDataSourceInterceptor implements Interceptor{
private static Logger logger=LoggerFactory.getLogger(DynamicDataSourceInterceptor.class);
//正則表達式,判斷是否是增刪改
private static final String REGEX=".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*";
//什么情況去做攔截
@Override
public Object intercept(Invocation invocation) throws Throwable {
//查看調用的方法是否使用了事物
boolean synchronizationActive=TransactionSynchronizationManager.isActualTransactionActive();
//獲取到方法的參數
Object[] objects=invocation.getArgs();
//增刪改查操作一般在頭部第一個會記錄,也就是object[0]中可以獲取的到
MappedStatement ms=(MappedStatement)objects[0];
String lookupKey=DynamicDataSourceHolder.DB_MASTER;
//讀方法
if(synchronizationActive!=true) {
if(ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//selectKey為自增ID查詢主鍵(SELECT LAST_INSERT_ID())方法,使用主庫
//就是添加例如商品信息,自增ID并返回這個ID的情況
if(ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
lookupKey=DynamicDataSourceHolder.DB_MASTER;
}else {
BoundSql boundSql=ms.getSqlSource().getBoundSql(objects[1]);
String sql=boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]"," ");
//使用正則判斷
if(sql.matches(REGEX)) {
//主庫
lookupKey=DynamicDataSourceHolder.DB_MASTER;
}else {
//從庫
lookupKey=DynamicDataSourceHolder.DB_SLAVE;
}
}
}
}else {
lookupKey=DynamicDataSourceHolder.DB_MASTER;
}
logger.debug("設置方法 [{}] use [{}] Strategy,SqlCommanType [{}]",ms.getId(),lookupKey,
ms.getSqlCommandType().name());
DynamicDataSourceHolder.setDBType(lookupKey);
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) {
// TODO Auto-generated method stub
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。