springboot多数据源动态切换和自定义mybatis分页插件

1、配置多数据源

  增加druid依赖

    springboot多数据源动态切换和自定义mybatis分页插件

  完整pom文件

  数据源配置文件

   

route.datasource.driver-class-name= com.mysql.jdbc.Driver
route.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
route.datasource.username= root
route.datasource.password= 123456 operate.datasource.driver-class-name= com.mysql.jdbc.Driver
operate.datasource.url= jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8
operate.datasource.username= root
operate.datasource.password= 123456

multiple-datasource.properties

  初始化数据源

/**
* 多数据源配置
* @author zhouliang
* @date 2017年9月20日
*/
@Configuration
@PropertySource("classpath:multiple-datasource.properties")
public class MyBatisConfig {
@Autowired
private Environment env;
@Autowired MybatisSpringPageInterceptor inteceptor; /**
* 创建数据源(数据源的名称:方法名可以取为XXXDataSource(),XXX为数据库名称,该名称也就是数据源的名称)
*/
@Bean
public DataSource operateDataSource() throws Exception {
Properties props = new Properties();
props.put("driverClassName", env.getProperty("operate.datasource.driver-class-name"));
props.put("url", env.getProperty("operate.datasource.url"));
props.put("username", env.getProperty("operate.datasource.username"));
props.put("password", env.getProperty("operate.datasource.password"));
return DruidDataSourceFactory.createDataSource(props);
}
@Bean
public DataSource routeDataSource() throws Exception {
Properties props = new Properties();
props.put("driverClassName", env.getProperty("route.datasource.driver-class-name"));
props.put("url", env.getProperty("route.datasource.url"));
props.put("username", env.getProperty("route.datasource.username"));
props.put("password", env.getProperty("route.datasource.password"));
return DruidDataSourceFactory.createDataSource(props);
} /**
* @Primary 该注解表示在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让@autowire注解报错
* @Qualifier 根据名称进行注入,通常是在具有相同的多个类型的实例的一个注入(例如有多个DataSource类型的实例)
*/
@Bean
@Primary
public DynamicDataSource dataSource(
@Qualifier("routeDataSource") DataSource routeDataSource,
@Qualifier("operateDataSource") DataSource operateDataSource) {
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
targetDataSources.put(DatabaseType.routeDS, routeDataSource);
targetDataSources.put(DatabaseType.operateDS, operateDataSource); DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);// 该方法是AbstractRoutingDataSource的方法
return dataSource;
} /**
* 根据数据源创建SqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DynamicDataSource ds)
throws Exception {
SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
fb.setDataSource(ds);// 指定数据源(这个必须有,否则报错)
// 下边两句仅仅用于*.xml文件,如果整个持久层操作不需要使用到xml文件的话(只用注解就可以搞定),则不加
fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));// 指定基包
fb.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(env.getProperty("mybatis.mapperLocations")));//
fb.setPlugins(new Interceptor[]{inteceptor});
return fb.getObject();
} }

MyBatisConfig.java

  @Bean是分别注入两个数据源,

  当自动注入多个同样的bean时需要指定一个默认额,所以这里指定了一个默认的抽象数据源@primary

  数据源注入之后需要创建SqlSessionFactory

springboot多数据源动态切换和自定义mybatis分页插件

  创建抽象数据源类集成AbstractRoutingDataSource

  

 public class DynamicDataSource extends AbstractRoutingDataSource {

     @Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DatabaseContextHolder.getDatabaseType();
} }

DynamicDataSource.java

 public enum DatabaseType {
routeDS,operateDS
}

DatabaseType.java

 public class DatabaseContextHolder {
private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<DatabaseType>(); public static void setDatabaseType(DatabaseType type){
contextHolder.set(type);
} public static DatabaseType getDatabaseType(){
return contextHolder.get();
}
public static void clearDatabaseType(){
contextHolder.remove();
}

DatabaseContextHolder.java

上述配置完成之后需要在配置文件中指定mybatis映射的xml文件的位置

  springboot多数据源动态切换和自定义mybatis分页插件

  指定mybatis映射文件之后需要在项目的启动类上排除springboot默认的数据库的配置以及指定mybatis映射文件对应的接口

  springboot多数据源动态切换和自定义mybatis分页插件

  不排除springboot默认的数据库配置类,项目启动的时候会报错启动失败

  @MapperScan指定了mybatis映射文件对应的接口所在的目录,这样避免了再每个接口上都加上@Mapper的注解

  到此两个数据源已经配置完成

2、数据源的动态切换

    这里设置了所有的实体bean都继承了一个父类,父类信息如下

    

 public class BaseBean implements Serializable{
private int pageSize=10;
private int pageNo=0;
private long totalNum;
private String totalMappedStatementId; private long shardValue = 0l; public BaseBean() {
super();
// TODO Auto-generated constructor stub
} public BaseBean(int pageSize, int pageNo, int totalNum) {
super();
this.pageSize = pageSize;
this.pageNo = pageNo;
this.totalNum = totalNum;
} public int getPageSize() {
return pageSize;
} public long getShardValue() {
return shardValue;
} public void setShardValue(long shardValue) {
this.shardValue = shardValue;
} public String getTotalMappedStatementId() {
return totalMappedStatementId;
} public void setTotalMappedStatementId(String totalMappedStatementId) {
this.totalMappedStatementId = totalMappedStatementId;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getPageNo() {
return pageNo;
} public void setPageNo(int pageNo) {
this.pageNo = pageNo;
} public long getTotalNum() {
return totalNum;
} public void setTotalNum(long totalNum) {
this.totalNum = totalNum;
} }

BaseBean.java

  BaseBean中的shardValue属性是用来指定数据源的,默认值为0,其余属性是分页相关的。

  数据源的动态切换是通过spring的切面编程来实现的,通过对mybatis的映射文件对应的接口进行监控,代码如下

  

 @Aspect
@Component
public class DataSourceAspect {
Logger logger = LoggerFactory.getLogger(DataSourceAspect.class); @Before("execution(* zl.mybatis.mapper.*.*(..))")
public void setDataSourcePgKey(JoinPoint point) {
Object args[] = point.getArgs();
for(Object obj:args){
if(obj instanceof BaseBean){
BaseBean bean = (BaseBean) obj;
if(Common.DB_0==bean.getShardValue()){
logger.info("===========================使用数据源DB_route=======================");
DatabaseContextHolder.setDatabaseType(DatabaseType.routeDS);
}else{
logger.info("===========================使用数据源DB_operate=======================");
DatabaseContextHolder.setDatabaseType(DatabaseType.operateDS);
}
break;
}
}
} }

DataSourceAspect.java

  @Before("execution(* zl.mybatis.mapper.*.*(..))")是对mybatis的映射文件对应的接口进行监控,根据获取到的参数实体类判断里面的shardValue的值来决定使用哪个数据源

  springboot多数据源动态切换和自定义mybatis分页插件

  完成这一步之后springboot的多数据源动态切换完成了,接下来继续

3、实现自定义mybatis的分页插件

    mybatis本身提供了对数据库操作的拦截器,所以实现自定义分页的时候只需要实现这个接口自定义里面的拦截方法。这里我是只拦截了查询的方法

    springboot多数据源动态切换和自定义mybatis分页插件

    具体代码如下

    

 @Component
@Intercepts({ @Signature(type = Executor.class, method = "query",
args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })
public class MybatisSpringPageInterceptor implements Interceptor { private static final Logger logger = LoggerFactory.getLogger(MybatisSpringPageInterceptor.class.getName()); @SuppressWarnings("unused")
public Object intercept(Invocation arg0) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) arg0.getArgs()[0];
Object parameter = arg0.getArgs()[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
if (null == boundSql || StringUtils.isBlank(boundSql.getSql())) {
return null;
}
RowBounds rowBounds = (RowBounds) arg0.getArgs()[2];
Object parameterObject = boundSql.getParameterObject();
BaseBean model = null;
if (parameterObject instanceof BaseBean) {
model = (BaseBean) parameterObject;
} else {
BoundSql newBoundSql = copyFromBoundSql(mappedStatement, boundSql, boundSql.getSql());
arg0.getArgs()[0] = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
return arg0.proceed();
}
if (null == model) {
throw new Exception("无法获取分页参数.");
}
if (model.getPageNo() == -1) {
BoundSql newBoundSql = copyFromBoundSql(mappedStatement, boundSql, boundSql.getSql());
arg0.getArgs()[0] = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
return arg0.proceed();
}
String shardSql = boundSql.getSql();
queryTotal(mappedStatement, shardSql, parameterObject, boundSql,model); if (null == rowBounds || rowBounds == RowBounds.DEFAULT) {
rowBounds = new RowBounds(model.getPageSize() * (model.getPageNo() - 1), model.getPageSize());
}
String pagesql = getLimitSql(shardSql, rowBounds.getOffset(), rowBounds.getLimit());
arg0.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
BoundSql newBoundSql = copyFromBoundSql(mappedStatement, boundSql, pagesql);
arg0.getArgs()[0] = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
return arg0.proceed();
} public static class BoundSqlSqlSource implements SqlSource {
BoundSql boundSql; public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
} public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
private String getLimitSql(String sql, int start, int end) throws Exception{
if(sql ==null){
throw new Exception("execute sql is empty.");
}
StringBuffer sqlBuffer = new StringBuffer(sql.length()+300);
sqlBuffer.append(sql);
sqlBuffer.append(" LIMIT ").append(start).append(",").append(end);
return sqlBuffer.toString();
}
private void queryTotal(MappedStatement mappedStatement, String replaceSql, Object parameterObject, BoundSql boundSql,BaseBean model) throws Exception{
StringBuffer countSql = new StringBuffer(); if(model.getTotalMappedStatementId()!=null && model.getTotalMappedStatementId().length()>0){
MappedStatement totalMappedStatement=mappedStatement.getConfiguration().getMappedStatement(model.getTotalMappedStatementId());
BoundSql totalBoundSql = totalMappedStatement.getBoundSql(parameterObject); countSql.append(totalBoundSql.getSql());
}else{
// 未指定,自动拼装
countSql.append("SELECT COUNT(1) FROM (").append(replaceSql).append(") as total");
} Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
if (logger.isDebugEnabled()) {
logger.debug(countSql.toString());
}
ps = conn.prepareStatement(countSql.toString());
BoundSql countBS = copyFromBoundSql(mappedStatement, boundSql, countSql.toString());
setParameters(ps, mappedStatement, countBS, parameterObject);
rs = ps.executeQuery();
if (rs.next()) {
model.setTotalNum(rs.getLong(1));
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new Exception(e.getMessage(), e);
} finally {
try {
if (null != rs) {
rs.close();
}
} catch (Exception e) {
logger.error("rs.close() error!", e);
}
try {
if (null != ps) {
ps.close();
}
} catch (Exception e) {
logger.error("ps.close() error!", e);
}
try {
if (null != conn) {
conn.close();
}
} catch (Exception e) {
logger.error("conn.close() error!", e);
}
}
}
protected MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
// builder.keyProperty(ms.getKeyProperties());
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.cache(ms.getCache());
MappedStatement newMs = builder.build();
return newMs;
} /**
*
* @param ps
* @param mappedStatement
* @param boundSql
* @param parameterObject
* @throws SQLException
*/
private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException {
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
if (parameterMappings != null) {
Configuration configuration = mappedStatement.getConfiguration();
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
for (int i = 0; i < parameterMappings.size(); i++) {
ParameterMapping parameterMapping = parameterMappings.get(i);
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
if (parameterObject == null) {
value = null;
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
value = parameterObject;
} else if (boundSql.hasAdditionalParameter(propertyName)) {
value = boundSql.getAdditionalParameter(propertyName);
} else {
value = metaObject == null ? null : metaObject.getValue(propertyName);
}
TypeHandler typeHandler = parameterMapping.getTypeHandler();
if (typeHandler == null) {
throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
}
logger.debug(i + 1 + ":" + value);
typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
}
}
}
}
@Override
public Object plugin(Object arg0) {
return Plugin.wrap(arg0, this);
} public void setProperties(Properties arg0) { } private BoundSql copyFromBoundSql(MappedStatement ms, BoundSql boundSql, String sql) {
BoundSql newBoundSql = new BoundSql(ms.getConfiguration(),sql, boundSql.getParameterMappings(), boundSql.getParameterObject());
for (ParameterMapping mapping : boundSql.getParameterMappings()) {
String prop = mapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
}
}
return newBoundSql;
} }

MybatisSpringPageInterceptor.java

  插件重写完成之后需要在注入SqlSessionFactory的时候指定这个插件,下面的代码是在MyBatisConfig.java中

    springboot多数据源动态切换和自定义mybatis分页插件

现在已经完成了多数据源的动态切换以及自定义mybatis的分页查询了,剩下的测试步骤就不再啰嗦了,排版比较乱大家就将就着看吧!

项目的完整代码

上一篇:告诉你们!我是怎么与Linux系统接触的!


下一篇:C语言之父Dennis Ritchie告诉你:如何成为世界上最好的程序员?