feat: 添加对 ignore 字段自动映射的支持

This commit is contained in:
开源海哥 2023-08-23 17:57:55 +08:00
parent 7f3de5a21a
commit 9edc7545f9
5 changed files with 36 additions and 21 deletions

View File

@ -16,7 +16,6 @@
package com.mybatisflex.core.activerecord.query;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.core.constant.SqlConsts;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.table.TableDef;
@ -39,7 +38,6 @@ import com.mybatisflex.core.util.LambdaUtil;
@SuppressWarnings({"unused", "unchecked"})
public abstract class QueryModel<T extends QueryModel<T>> {
@Column(ignore = true)
private QueryWrapper queryWrapper;
protected QueryWrapper queryWrapper() {

View File

@ -74,6 +74,7 @@ public interface MapperQueryChain<T> extends ChainQuery<T> {
/**
* {@inheritDoc}
*/
@Override
default T one() {
return baseMapper().selectOneByQuery(toQueryWrapper());
}
@ -81,6 +82,7 @@ public interface MapperQueryChain<T> extends ChainQuery<T> {
/**
* {@inheritDoc}
*/
@Override
default <R> R oneAs(Class<R> asType) {
return baseMapper().selectOneByQueryAs(toQueryWrapper(), asType);
}

View File

@ -19,6 +19,10 @@ import com.mybatisflex.annotation.*;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.Reflectors;
@ -61,6 +65,10 @@ public class TableInfoFactory {
char.class, String.class, Character.class
);
static final Set<Class<?>> ignoreColumnTypes = CollectionUtil.newHashSet(
QueryWrapper.class, QueryColumn.class, QueryCondition.class, QueryChain.class
);
private static final Map<Class<?>, TableInfo> mapperTableInfoMap = new ConcurrentHashMap<>();
private static final Map<Class<?>, TableInfo> entityTableMap = new ConcurrentHashMap<>();
@ -206,6 +214,9 @@ public class TableInfoFactory {
// }
Class<?> fieldType = reflector.getGetterType(field.getName());
if (ignoreColumnTypes.contains(fieldType)) {
continue;
}
//满足以下 3 种情况不支持该类型
if ((column == null || column.typeHandler() == UnknownTypeHandler.class) // 未配置 typeHandler

View File

@ -69,6 +69,10 @@ public class AccountController {
DataSourceKey.use("ds2");
accountMapper.insert(account);
// if (true) {
// throw new RuntimeException("ex");
// }
return "add ok!";
}

View File

@ -1,13 +1,13 @@
# DataSource Config
spring:
# h2:
# console:
# enabled: true
datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 123456
#spring:
# # h2:
# # console:
# # enabled: true
# datasource:
## driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/flex_test
# username: root
# password: 123456
# driver-class-name:
# datasource:
# driver-class-name: org.h2.Driver
@ -36,13 +36,13 @@ spring:
# url: jdbc:mysql://localhost:3306/flex_test
# username: root
# password: 12345678
#mybatis-flex:
# datasource:
# ds1:
# url: jdbc:mysql://127.0.0.1:3306/flex_test
# username: root
# password: 123456
# ds2:
# url: jdbc:mysql://127.0.0.1:3306/flex_test
# username: root
# password: 123456
mybatis-flex:
datasource:
ds1:
url: jdbc:mysql://127.0.0.1:3306/flex_test
username: root
password: 123456
ds2:
url: jdbc:mysql://127.0.0.1:3306/flex_test
username: root
password: 123456