mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
optimize WrapperUtil.getValues
This commit is contained in:
parent
663ee88313
commit
499366c4e0
@ -18,7 +18,6 @@ package com.mybatisflex.core.query;
|
|||||||
|
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
import com.mybatisflex.core.util.EnumWrapper;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
@ -99,12 +98,6 @@ public class QueryCondition implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setValue(Object value) {
|
public void setValue(Object value) {
|
||||||
if (value != null && value.getClass().isEnum()) {
|
|
||||||
EnumWrapper enumWrapper = new EnumWrapper(value.getClass());
|
|
||||||
if (enumWrapper.hasEnumValueAnnotation()) {
|
|
||||||
value = enumWrapper.getEnumValue((Enum) value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,7 @@ package com.mybatisflex.core.query;
|
|||||||
|
|
||||||
import com.mybatisflex.core.util.ClassUtil;
|
import com.mybatisflex.core.util.ClassUtil;
|
||||||
import com.mybatisflex.core.util.CollectionUtil;
|
import com.mybatisflex.core.util.CollectionUtil;
|
||||||
|
import com.mybatisflex.core.util.EnumWrapper;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
@ -82,14 +83,14 @@ class WrapperUtil {
|
|||||||
return NULL_PARA_ARRAY;
|
return NULL_PARA_ARRAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Object> paras = new ArrayList<>();
|
List<Object> params = new ArrayList<>();
|
||||||
getValues(condition, paras);
|
getValues(condition, params);
|
||||||
|
|
||||||
return paras.isEmpty() ? NULL_PARA_ARRAY : paras.toArray();
|
return params.isEmpty() ? NULL_PARA_ARRAY : params.toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void getValues(QueryCondition condition, List<Object> paras) {
|
private static void getValues(QueryCondition condition, List<Object> params) {
|
||||||
if (condition == null) {
|
if (condition == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -98,29 +99,35 @@ class WrapperUtil {
|
|||||||
if (value == null
|
if (value == null
|
||||||
|| value instanceof QueryColumn
|
|| value instanceof QueryColumn
|
||||||
|| value instanceof RawValue) {
|
|| value instanceof RawValue) {
|
||||||
getValues(condition.next, paras);
|
getValues(condition.next, params);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.getClass().isArray()) {
|
addParam(params, value);
|
||||||
Object[] values = (Object[]) value;
|
getValues(condition.next, params);
|
||||||
for (Object object : values) {
|
}
|
||||||
if (object != null && ClassUtil.isArray(object.getClass())) {
|
|
||||||
for (int i = 0; i < Array.getLength(object); i++) {
|
private static void addParam(List<Object> paras, Object value) {
|
||||||
paras.add(Array.get(object, i));
|
if (value == null) {
|
||||||
}
|
paras.add(null);
|
||||||
} else {
|
} else if (ClassUtil.isArray(value.getClass())) {
|
||||||
paras.add(object);
|
for (int i = 0; i < Array.getLength(value); i++) {
|
||||||
}
|
addParam(paras, Array.get(value, i));
|
||||||
}
|
}
|
||||||
} else if (value instanceof QueryWrapper) {
|
} else if (value instanceof QueryWrapper) {
|
||||||
Object[] valueArray = ((QueryWrapper) value).getValueArray();
|
Object[] valueArray = ((QueryWrapper) value).getValueArray();
|
||||||
paras.addAll(Arrays.asList(valueArray));
|
paras.addAll(Arrays.asList(valueArray));
|
||||||
|
} else if (value.getClass().isEnum()) {
|
||||||
|
EnumWrapper enumWrapper = new EnumWrapper(value.getClass());
|
||||||
|
if (enumWrapper.hasEnumValueAnnotation()) {
|
||||||
|
paras.add(enumWrapper.getEnumValue((Enum) value));
|
||||||
|
} else {
|
||||||
|
paras.add(value);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
paras.add(value);
|
paras.add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
getValues(condition.next, paras);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,9 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
|||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.select;
|
||||||
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
||||||
import static com.mybatisflex.test.table.Tables.ARTICLE;
|
import static com.mybatisflex.test.table.Tables.ARTICLE;
|
||||||
|
|
||||||
@ -145,14 +147,15 @@ public class EntityTestStarter {
|
|||||||
// .or(SYS_CONFIG.TYPE.like(word).when(StrChecker.isNotBlank(word)))
|
// .or(SYS_CONFIG.TYPE.like(word).when(StrChecker.isNotBlank(word)))
|
||||||
// );
|
// );
|
||||||
|
|
||||||
// List<Account> accounts = accountMapper.selectListByQuery(
|
List<Account> accounts = accountMapper.selectListByQuery(
|
||||||
// select().where(ACCOUNT.AGE.ge(18).when(false))
|
select().where(ACCOUNT.AGE.ge(18).when(false))
|
||||||
// .and(ACCOUNT.USER_NAME.like("aaaa").when(false)
|
.and(ACCOUNT.USER_NAME.like("aaaa").when(false)
|
||||||
// .or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
.or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
||||||
// .or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
.or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
||||||
// .or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
.or(ACCOUNT.USER_NAME.like("aaaa").when(false))
|
||||||
// )
|
)
|
||||||
// );
|
);
|
||||||
|
System.out.println(accounts);
|
||||||
|
|
||||||
|
|
||||||
// Page<Account> paginate = accountMapper.paginate(1, 10, QueryWrapper.create());
|
// Page<Account> paginate = accountMapper.paginate(1, 10, QueryWrapper.create());
|
||||||
|
|||||||
@ -18,6 +18,7 @@ package com.mybatisflex.test;
|
|||||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||||
import com.mybatisflex.core.audit.AuditManager;
|
import com.mybatisflex.core.audit.AuditManager;
|
||||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.tenant.TenantFactory;
|
import com.mybatisflex.core.tenant.TenantFactory;
|
||||||
import com.mybatisflex.core.tenant.TenantManager;
|
import com.mybatisflex.core.tenant.TenantManager;
|
||||||
import com.mybatisflex.mapper.TenantAccountMapper;
|
import com.mybatisflex.mapper.TenantAccountMapper;
|
||||||
@ -26,6 +27,10 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.select;
|
||||||
|
import static com.mybatisflex.test.table.Tables.ACCOUNT;
|
||||||
|
import static com.mybatisflex.test.table.Tables.TENANT_ACCOUNT;
|
||||||
|
|
||||||
public class TenantTester {
|
public class TenantTester {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -50,21 +55,21 @@ public class TenantTester {
|
|||||||
TenantManager.setTenantFactory(new TenantFactory() {
|
TenantManager.setTenantFactory(new TenantFactory() {
|
||||||
@Override
|
@Override
|
||||||
public Object[] getTenantIds() {
|
public Object[] getTenantIds() {
|
||||||
return new Object[]{1};
|
return new Object[]{1,2};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
TenantAccountMapper mapper = MybatisFlexBootstrap.getInstance().getMapper(TenantAccountMapper.class);
|
TenantAccountMapper mapper = MybatisFlexBootstrap.getInstance().getMapper(TenantAccountMapper.class);
|
||||||
|
|
||||||
// mapper.selectListByQuery(QueryWrapper.create()
|
mapper.selectListByQuery(QueryWrapper.create()
|
||||||
// .select(TENANT_ACCOUNT.ALL_COLUMNS)
|
.select(TENANT_ACCOUNT.ALL_COLUMNS)
|
||||||
// .from(TENANT_ACCOUNT.as("c"), ACCOUNT.as("b"))
|
.from(TENANT_ACCOUNT.as("c"), ACCOUNT.as("b"))
|
||||||
// .where(TENANT_ACCOUNT.ID.eq(ACCOUNT.ID))
|
.where(TENANT_ACCOUNT.ID.eq(ACCOUNT.ID))
|
||||||
// .and(TENANT_ACCOUNT.ID.eq(1))
|
.and(TENANT_ACCOUNT.ID.eq(1))
|
||||||
// .unionAll(select(TENANT_ACCOUNT.ALL_COLUMNS).from(TENANT_ACCOUNT)
|
.unionAll(select(TENANT_ACCOUNT.ALL_COLUMNS).from(TENANT_ACCOUNT)
|
||||||
// .where(TENANT_ACCOUNT.ID.eq(2))
|
.where(TENANT_ACCOUNT.ID.eq(2))
|
||||||
// )
|
)
|
||||||
// );
|
);
|
||||||
|
|
||||||
// mapper.deleteBatchByIds(Arrays.asList(1, 2));
|
// mapper.deleteBatchByIds(Arrays.asList(1, 2));
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user