首頁 > 軟體

基於mybatis plus實現資料來源動態新增、刪除、切換,自定義資料來源的範例程式碼

2022-03-10 16:00:59

簡介

基於springboot,mybatis plus整合了一套多資料來源的解決方案,在使用時引入相應的外掛dynamic-datasource-spring-boot-starter,可以實現資料來源的動態新增、刪除等功能,對於多租戶或者分庫等操作可以根據AOP切面代理到不同的資料來源、實現單一系統資料隔離的目的。

程式碼範例

mavne依賴

<!--mybatis-plus-->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.4.3.4</version>
</dependency>
 
<!--dynamic-datasource-->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
	<version>3.4.1</version>
</dependency>

資料來源增加、移除

@RestController
@RequestMapping("/datasources")
public class DataSourceController {
 
    @Resource
    private DataSource dataSource;
    @Resource
    private DefaultDataSourceCreator dataSourceCreator;
    
 
    @GetMapping("list")
    public Set<String> list() {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        return ds.getDataSources().keySet();
    }
 
    @PostMapping("add")
    public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPollName(), dataSource);
        return ds.getDataSources().keySet();
    }
 
    @DeleteMapping("remove")
    public void remove(String name) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        ds.removeDataSource(name);
    }
}

預設的資料來源連線池載入順序為: druid>hikaricp>beecp>dbcp>spring basic

資料來源切換

基於AOP切換

新增註解,排除不做切換的介面

package com.starsray.dynamic.datasource.annotation;
 
import java.lang.annotation.*;
 
/**
 * <p>
 * 使用者標識僅可以使用預設資料來源
 * </p>
 *
 * @author starsray
 * @since 2021-11-10
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DefaultDs {
}

切面具體實現

package com.starsray.dynamic.datasource.interceptor;
 
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.starsray.dynamic.datasource.annotation.DefaultDs;
import com.starsray.dynamic.datasource.exception.ExceptionEnum;
import com.starsray.dynamic.datasource.exception.GlobalException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
 
/**
 * <p>
 * 資料來源選擇器切面
 * </p>
 *
 * @author starsray
 * @since 2021-11-10
 */
@Aspect
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DsInterceptor implements HandlerInterceptor {
 
    @Pointcut("execution(public * com.starsray.dynamic.datasource.controller.*.*(..))")
    public void datasourcePointcut() {
    }
 
    /**
     * 前置操作,攔截具體請求,獲取header裡的資料來源id,設定執行緒變數裡,用於後續切換資料來源
     */
    @Before("datasourcePointcut()")
    public void doBefore(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
 
        // 排除不可切換資料來源的方法
        DefaultDs annotation = method.getAnnotation(DefaultDs.class);
        if (null != annotation) {
            DynamicDataSourceContextHolder.push("master");
        } else {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
            assert attributes != null;
            HttpServletRequest request = attributes.getRequest();
            String header = request.getHeader("tenantName");
            if (StringUtils.isNotBlank(header)) {
                DynamicDataSourceContextHolder.push(header);
            } else {
                throw new GlobalException(ExceptionEnum.NOT_TENANT);
            }
        }
    }
 
    /**
     * 後置操作,設定回預設的資料來源id
     */
    @AfterReturning("datasourcePointcut()")
    public void doAfter() {
        DynamicDataSourceContextHolder.push("master");
    }
 
}

基於重寫處理器

mybatis plus提供了預設處理器來決定使用的資料來源,可以重寫處理器實現自定義引數,比如從請求header裡面獲取引數切換資料來源。

@DS("#header.tenantId")

自定義處理器

public class HeaderProcessor extends DsProcessor {
 
    private static final String HEADER = "#header";
 
    @Override
    public boolean matches(String key) {
        return key.startsWith(HEADER);
    }
 
    @Override
    public String doDetermineDatasource(MethodInvocation invocation, String key) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getHeader(key.substring(8));
    }
}

註冊自定義處理器

@Configuration
public class CustomerDynamicDataSourceConfig{
 
   @Bean
   public DsProcessor dsProcessor() {
        DsHeaderProcessor headerProcessor = new DsHeaderProcessor();
        DsSessionProcessor sessionProcessor = new DsSessionProcessor();
        DsSpelExpressionProcessor spelExpressionProcessor = new DsSpelExpressionProcessor();
        headerProcessor.setNextProcessor(sessionProcessor);
        sessionProcessor.setNextProcessor(spelExpressionProcessor);
        return headerProcessor;
   }
}

如果有場景需要手動切換資料來源,可以使用元件提供的工具來實現。

DynamicDataSourceContextHolder.push("master");

自定義資料來源

mybatis plus提供了一個介面來載入資料來源資訊。

public interface DynamicDataSourceProvider {
    Map<String, DataSource> loadDataSources();
}

這個介面有一個抽象實現類AbstractDataSourceProvider,通過模板方法定義了載入資料來源來源的方式,mybatis plus通過YmlDynamicDataSourceProvider實現了讀取yml檔案設定來初始化資料來源的方式。

public abstract class AbstractDataSourceProvider implements DynamicDataSourceProvider {
    private static final Logger log = LoggerFactory.getLogger(AbstractDataSourceProvider.class);
    @Autowired
    private DefaultDataSourceCreator defaultDataSourceCreator;
 
    public AbstractDataSourceProvider() {
    }
 
    protected Map<String, DataSource> createDataSourceMap(Map<String, DataSourceProperty> dataSourcePropertiesMap) {
        Map<String, DataSource> dataSourceMap = new HashMap(dataSourcePropertiesMap.size() * 2);
        Iterator var3 = dataSourcePropertiesMap.entrySet().iterator();
 
        while(var3.hasNext()) {
            Entry<String, DataSourceProperty> item = (Entry)var3.next();
            String dsName = (String)item.getKey();
            DataSourceProperty dataSourceProperty = (DataSourceProperty)item.getValue();
            String poolName = dataSourceProperty.getPoolName();
            if (poolName == null || "".equals(poolName)) {
                poolName = dsName;
            }
 
            dataSourceProperty.setPoolName(poolName);
            dataSourceMap.put(dsName, this.defaultDataSourceCreator.createDataSource(dataSourceProperty));
        }
        return dataSourceMap;
    }
}

如果有需要從資料庫載入資料來源資訊,可以重寫AbstractJdbcDataSourceProvider中的executeStmt方法來載入資料庫設定資訊。範例:

package com.digital.cnzz.dynamic.ds.provider;
 
import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.digital.cnzz.dynamic.ds.config.DefaultDsConfig;
import com.digital.cnzz.dynamic.ds.constant.DsDriverEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
 
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
 
@Primary
@Configuration
public class DsProvider {
 
    @Resource
    private DefaultDsConfig defaultDsConfig;
 
    @Bean
    public DynamicDataSourceProvider jdbcDynamicDataSourceProvider() {
        return new AbstractJdbcDataSourceProvider(defaultDsConfig.getDriverClassName(), defaultDsConfig.getUrl(), defaultDsConfig.getUsername(), defaultDsConfig.getPassword()) {
            @Override
            protected Map<String, DataSourceProperty> executeStmt(Statement statement) {
                Map<String, DataSourceProperty> dataSourcePropertiesMap = null;
                ResultSet rs = null;
                try {
                    dataSourcePropertiesMap = new HashMap<>();
                    rs = statement.executeQuery("SELECT * FROM DYNAMIC_DATASOURCE_INSTANCE");
                    while (rs.next()) {
                        String name = rs.getString("name");
                        DataSourceProperty property = new DataSourceProperty();
                        property.setDriverClassName(rs.getString("driver"));
                        property.setUrl(rs.getString("url"));
                        property.setUsername(rs.getString("username"));
                        property.setPassword(rs.getString("password"));
                        dataSourcePropertiesMap.put(name, property);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (rs != null) {
                            rs.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                return dataSourcePropertiesMap;
            }
        };
    }
}

通過讀取原始碼可以發現,如果還有其他需要自定義載入資料來源的方式,只需要繼承AbstractDataSourceProvider抽象類,實現DynamicDataSourceProvider介面,重寫loadDataSources方法就可以實現自定義資料來源。

完整程式碼範例:https://gitee.com/starsray/dynamic-datasource

到此這篇關於基於mybatis plus實現資料來源動態新增、刪除、切換,自定義資料來源的文章就介紹到這了,更多相關mybatis plus自定義資料來源內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com