Mybatis-Plus多数据源之使用spel 从参数获取数据源

  1. 配置数据源
  datasource:
    dynamic:
      #设置默认的数据源或者数据源组,默认值即为sqlserver
      primary: sqlserver 
      datasource:
        sqlserver:
          url: ...
          username: ...
          password: ...
          driver-class-name: ...
        mysql:
          ...
        oracle:
          ...
  1. DataSourceEnum
@Getter
@AllArgsConstructor
public enum DataSourceEnum {
    MYSQL(1, "mysql"),

    ORACLE(2, "oracle"),

    SQLSERVER(3, "sqlserver");

    private Integer id;

    private String dataSourceName;

    public static DataSourceEnum getDataSourceById(Integer dateSourceId) {
        for (DataSourceEnum value : DataSourceEnum.values()) {
            if (Objects.equals(value.getId(), dateSourceId)) {
                return value;
            }
        }
        return null;
    }
}
  1. DataSourceService
public interface DataSourceService {
    
    void test(DataSourceEnum dataSourceEnum);
}	
  1. DataSourceServiceImpl
public class DataSourceServiceImpl implements DataSourceService {

    @Override
    @DS(value = "#dataSourceEnum.dataSourceName")
    public void test(DataSourceEnum dataSourceEnum){
       
    }

}

你可能感兴趣的:(笔记)