【同步】Boot 和 Cloud 的功能同步
This commit is contained in:
@@ -43,4 +43,6 @@ public class ServiceErrorCodeRange {
|
||||
|
||||
// 模块 crm 错误码区间 [1-020-000-000 ~ 1-021-000-000)
|
||||
|
||||
// 模块 ai 错误码区间 [1-022-000-000 ~ 1-023-000-000)
|
||||
|
||||
}
|
||||
|
||||
@@ -247,15 +247,15 @@ id,name,type,parentId
|
||||
246,英属印度洋领地,1,0
|
||||
247,东萨摩亚,1,0
|
||||
248,诺福克岛,1,0
|
||||
110000,北京,2,1
|
||||
120000,天津,2,1
|
||||
110000,北京市,2,1
|
||||
120000,天津市,2,1
|
||||
130000,河北省,2,1
|
||||
140000,山西省,2,1
|
||||
150000,内蒙古自治区,2,1
|
||||
210000,辽宁省,2,1
|
||||
220000,吉林省,2,1
|
||||
230000,黑龙江省,2,1
|
||||
310000,上海,2,1
|
||||
310000,上海市,2,1
|
||||
320000,江苏省,2,1
|
||||
330000,浙江省,2,1
|
||||
340000,安徽省,2,1
|
||||
@@ -268,7 +268,7 @@ id,name,type,parentId
|
||||
440000,广东省,2,1
|
||||
450000,广西壮族自治区,2,1
|
||||
460000,海南省,2,1
|
||||
500000,重庆,2,1
|
||||
500000,重庆市,2,1
|
||||
510000,四川省,2,1
|
||||
520000,贵州省,2,1
|
||||
530000,云南省,2,1
|
||||
|
||||
|
@@ -28,7 +28,7 @@ public class AreaUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testFormat() {
|
||||
assertEquals(AreaUtils.format(110105), "北京 北京市 朝阳区");
|
||||
assertEquals(AreaUtils.format(110105), "北京市 北京市 朝阳区");
|
||||
assertEquals(AreaUtils.format(1), "中国");
|
||||
assertEquals(AreaUtils.format(2), "蒙古");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package cn.iocoder.yudao.framework.mybatis.core.enums;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 针对 MyBatis Plus 的 {@link DbType} 增强,补充更多信息
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum DbTypeEnum {
|
||||
|
||||
/**
|
||||
* MySQL
|
||||
*/
|
||||
MY_SQL( DbType.MYSQL, "MySQL", "FIND_IN_SET('#{value}', #{column}) <> 0"),
|
||||
|
||||
/**
|
||||
* Oracle
|
||||
*/
|
||||
ORACLE(DbType.ORACLE, "Oracle", "FIND_IN_SET('#{value}', #{column}) <> 0"),
|
||||
|
||||
/**
|
||||
* PostgreSQL
|
||||
*
|
||||
* 华为 openGauss 使用 ProductName 与 PostgreSQL 相同
|
||||
*/
|
||||
POSTGRE_SQL(DbType.POSTGRE_SQL,"PostgreSQL", "POSITION('#{value}' IN #{column}) <> 0"),
|
||||
|
||||
/**
|
||||
* SQL Server
|
||||
*/
|
||||
SQL_SERVER(DbType.SQL_SERVER, "Microsoft SQL Server", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0"),
|
||||
|
||||
/**
|
||||
* 达梦
|
||||
*/
|
||||
DM(DbType.DM, "DM DBMS", "FIND_IN_SET('#{value}', #{column}) <> 0"),
|
||||
|
||||
/**
|
||||
* 人大金仓
|
||||
*/
|
||||
KINGBASE_ES(DbType.KINGBASE_ES, "KingbaseES", "POSITION('#{value}' IN #{column}) <> 0"),
|
||||
;
|
||||
|
||||
public static final Map<String, DbTypeEnum> MAP_BY_NAME = Arrays.stream(values())
|
||||
.collect(Collectors.toMap(DbTypeEnum::getProductName, Function.identity()));
|
||||
|
||||
public static final Map<DbType, DbTypeEnum> MAP_BY_MP = Arrays.stream(values())
|
||||
.collect(Collectors.toMap(DbTypeEnum::getMpDbType, Function.identity()));
|
||||
|
||||
/**
|
||||
* MyBatis Plus 类型
|
||||
*/
|
||||
private final DbType mpDbType;
|
||||
/**
|
||||
* 数据库产品名
|
||||
*/
|
||||
private final String productName;
|
||||
/**
|
||||
* SQL FIND_IN_SET 模板
|
||||
*/
|
||||
private final String findInSetTemplate;
|
||||
|
||||
public static DbType find(String databaseProductName) {
|
||||
if (StrUtil.isBlank(databaseProductName)) {
|
||||
return null;
|
||||
}
|
||||
return MAP_BY_NAME.get(databaseProductName).getMpDbType();
|
||||
}
|
||||
|
||||
public static String getFindInSetTemplate(DbType dbType) {
|
||||
return Optional.of(MAP_BY_MP.get(dbType).getFindInSetTemplate())
|
||||
.orElseThrow(() -> new IllegalArgumentException("FIND_IN_SET not supported"));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
package cn.iocoder.yudao.framework.mybatis.core.util;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum;
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* JDBC 工具类
|
||||
@@ -35,8 +40,22 @@ public class JdbcUtils {
|
||||
* @return DB 类型
|
||||
*/
|
||||
public static DbType getDbType(String url) {
|
||||
String name = com.alibaba.druid.util.JdbcUtils.getDbType(url, null);
|
||||
return DbType.getDbType(name);
|
||||
return com.baomidou.mybatisplus.extension.toolkit.JdbcUtils.getDbType(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过当前数据库连接获得对应的 DB 类型
|
||||
*
|
||||
* @return DB 类型
|
||||
*/
|
||||
public static DbType getDbType() {
|
||||
DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
||||
DataSource dataSource = dynamicRoutingDataSource.determineDataSource();
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName());
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalArgumentException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package cn.iocoder.yudao.framework.mybatis.core.util;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.SortingField;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
@@ -56,7 +59,7 @@ public class MyBatisUtils {
|
||||
|
||||
/**
|
||||
* 获得 Table 对应的表名
|
||||
*
|
||||
* <p>
|
||||
* 兼容 MySQL 转义表名 `t_xxx`
|
||||
*
|
||||
* @param table 表
|
||||
@@ -85,4 +88,19 @@ public class MyBatisUtils {
|
||||
return new Column(tableName + StringPool.DOT + column);
|
||||
}
|
||||
|
||||
/**
|
||||
* 跨数据库的 find_in_set 实现
|
||||
*
|
||||
* @param column 字段名称
|
||||
* @param value 查询值(不带单引号)
|
||||
* @return sql
|
||||
*/
|
||||
public static String findInSet(String column, Object value) {
|
||||
// 这里不用SqlConstants.DB_TYPE,因为它是使用 primary 数据源的 url 推断出来的类型
|
||||
DbType dbType = JdbcUtils.getDbType();
|
||||
return DbTypeEnum.getFindInSetTemplate(dbType)
|
||||
.replace("#{column}", column)
|
||||
.replace("#{value}", StrUtil.toString(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user