This commit is contained in:
michael 2024-01-20 20:10:43 +08:00
commit 925f702c0b
4 changed files with 75 additions and 47 deletions

View File

@ -18,6 +18,8 @@ package com.mybatisflex.core.dialect;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import com.mybatisflex.core.exception.MybatisFlexException;
import org.apache.ibatis.util.MapUtil; import org.apache.ibatis.util.MapUtil;
import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl; import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
@ -45,6 +47,7 @@ public class DialectFactory {
* 通过设置当前线程的数据库类型以达到在代码执行时随时切换方言的功能 * 通过设置当前线程的数据库类型以达到在代码执行时随时切换方言的功能
*/ */
private static final ThreadLocal<DbType> dbTypeThreadLocal = new ThreadLocal<>(); private static final ThreadLocal<DbType> dbTypeThreadLocal = new ThreadLocal<>();
private static DbType dbTypeGlobal = null ;
/** /**
@ -75,6 +78,19 @@ public class DialectFactory {
return dbTypeThreadLocal.get(); return dbTypeThreadLocal.get();
} }
public static DbType getGlobalDbType() {
return dbTypeGlobal;
}
public static void setGlobalDbType(DbType dbType) {
if(dbTypeGlobal == null&&dbType!=null){
dbTypeGlobal = dbType ;
}else if(dbTypeGlobal != null){
throw new MybatisFlexException("dbTypeGlobal is only set once");
}else if(dbType==null){
throw new MybatisFlexException("dbType can not be null");
}
}
/** /**
* 清除当前线程的 dbType * 清除当前线程的 dbType

View File

@ -76,6 +76,11 @@ public class MapperInvocationHandler implements InvocationHandler {
//优先获取用户自己配置的 dbType //优先获取用户自己配置的 dbType
DbType dbType = DialectFactory.getHintDbType(); DbType dbType = DialectFactory.getHintDbType();
DbType dbTypeGlobal = DialectFactory.getGlobalDbType();
//当前线程没有设置dbType,但是全局设置了dbTypeGlobal那么就使用全局的dbTypeGlobal
if(dbTypeGlobal!=null&&dbType==null){
dbType = dbTypeGlobal ;
}
if (dbType == null) { if (dbType == null) {
if (dataSourceKey != null && dataSource != null) { if (dataSourceKey != null && dataSource != null) {
dbType = dataSource.getDbType(dataSourceKey); dbType = dataSource.getDbType(dataSourceKey);

View File

@ -2609,24 +2609,24 @@ public class QueryMethods {
} }
/** /**
* SELECT 1 FROM table * SELECT 1 as temp_one FROM table
*/ */
public static QueryWrapper selectOne() { public static QueryWrapper selectOne() {
return select(column("1")); return select(column("1").as("temp_one"));
} }
/** /**
* SELECT COUNT(*) FROM table * SELECT COUNT(*) as temp_count FROM table
*/ */
public static QueryWrapper selectCount() { public static QueryWrapper selectCount() {
return select(count()); return select(count().as("temp_count"));
} }
/** /**
* SELECT COUNT(1) FROM table * SELECT COUNT(1) as temp_count_one FROM table
*/ */
public static QueryWrapper selectCountOne() { public static QueryWrapper selectCountOne() {
return select(count("1")); return select(count(new RawQueryColumn("1")).as("temp_count_one"));
} }
/** /**

View File

@ -254,8 +254,6 @@ public class AccountSqlTester {
} }
@Test @Test
public void testWhereCond1Sql() { public void testWhereCond1Sql() {
boolean flag = false; boolean flag = false;
@ -323,6 +321,15 @@ public class AccountSqlTester {
System.out.println(query.toSQL()); System.out.println(query.toSQL());
} }
@Test
public void testSelectCountOne() {
QueryWrapper query = selectCountOne()
.from(ARTICLE);
Assert.assertEquals("SELECT COUNT(1) AS `temp_count_one` FROM `tb_article`"
, query.toSQL());
System.out.println(query.toSQL());
}
@Test @Test
public void testWhereAndOrSql() { public void testWhereAndOrSql() {