aop配置:
自定义注解--query:
@Target({ METHOD })
@Retention(RUNTIME)
public @interface Query {
DataSourceType[] dataSourceType() default DataSourceType.MASTER;
QueryStrategy queryStrategy() default QueryStrategy.ONE;
}
public class DataSourceAutoSwitchAdvice {
public Object queryData(ProceedingJoinPoint joinPoint, Query query) throws Throwable {
try {
List
辅助类DataSourceTypeHolder和DataSourceType:
public abstract class DataSourceTypeHolder {
private static ThreadLocal dataSourceTypeHolder = new ThreadLocal();
public static void switchTo(DataSourceType dataSourceType) {
dataSourceTypeHolder.set(dataSourceType);
}
public static DataSourceType getDataSourceType() {
return dataSourceTypeHolder.get();
}
public static void clear() {
dataSourceTypeHolder.remove();
}
}
public enum DataSourceType {
MASTER("master", "master db"),
SLAVE("slave", "slave db"),
ARCHIVE("archive", "archive db");
/** The enumeration's code. */
private String code;
/** The enumeration's description. */
private String desc;
/**
* @param code the enumeration's code
* @param desc the enumeration's description
*/
private DataSourceType(String code, String desc) {
this.code = code;
this.desc = desc;
}
/**
* Get an enumeration by it's code.
*
* @param code the enumeration's code
* @return {@link DataSourceType}
*/
public static DataSourceType getEnumByCode(String code) {
if (StringUtils.isBlank(code)) {
return null;
}
for (DataSourceType ele : values()) {
if (StringUtils.equals(code, ele.getCode())) {
return ele;
}
}
return null;
}
/**
* @return the code
*/
public String getCode() {
return code;
}
/**
* @return the desc
*/
public String getDesc() {
return desc;
}
}
DataSourceAutoSwitchSqlMapClientDaoSupport类代码如下:
public abstract class DataSourceAutoSwitchSqlMapClientDaoSupport {
private DataSourceType defaultDataSourceType = DataSourceType.MASTER;
private final Map sqlMapClientTemplateMap = new HashMap();
public SqlMapClientTemplate getSqlMapClientTemplate() {
DataSourceType dataSourceType = DataSourceTypeHolder.getDataSourceType();
if (null == dataSourceType) {
dataSourceType = defaultDataSourceType;
}
SqlMapClientTemplate sqlMapClientTemplate = sqlMapClientTemplateMap.get(dataSourceType.getCode());
Assert.notNull(sqlMapClientTemplate, "SqlMapClientTemplate does not exist: dataSourceType=" + dataSourceType.getCode());
return sqlMapClientTemplate;
}
/**
* @param defaultDataSourceType the defaultDataSourceType to set
*/
public void setDefaultDataSourceType(String defaultDataSourceType) {
DataSourceType dataSourceType = DataSourceType.getEnumByCode(defaultDataSourceType);
Assert.notNull(dataSourceType, "defaultDataSourceType must not be null");
this.defaultDataSourceType = dataSourceType;
}
/**
* @param sqlMapClientTemplateMap the sqlMapClientTemplateMap to set
*/
public void setSqlMapClientTemplateMap(Map sqlMapClientTemplateMap) {
Assert.notEmpty(sqlMapClientTemplateMap, "sqlMapClientTemplateMap must not be empty");
this.sqlMapClientTemplateMap.putAll(sqlMapClientTemplateMap);
}
public void setSqlMapClient(Map sqlMapClientMap) {
Assert.notEmpty(sqlMapClientMap, "sqlMapClientMap must not be empty");
for (Entry entry : sqlMapClientMap.entrySet()) {
if (!this.sqlMapClientTemplateMap.containsKey(entry.getKey())) {
this.sqlMapClientTemplateMap.put(entry.getKey(), new SqlMapClientTemplate(entry.getValue()));
}
}
}
}
实现类PosterBlacklistDaoImpl中的方法如下:
@Query(dataSourceType = DataSourceType.SLAVE)
@SuppressWarnings("unchecked")
@Override
public List getPosterBlacklistStatisticsFromSlaveDB(int companyId) {
return getSqlMapClientTemplate().queryForList("PosterBlacklist.getPosterBlacklistStatistics", companyId);
}