mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
add auto join mapping support
This commit is contained in:
parent
81d676dcbb
commit
f1c97d7dcd
@ -95,6 +95,8 @@ public class TableInfo {
|
||||
private List<UpdateListener> onUpdateListeners;
|
||||
private List<SetListener> onSetListeners;
|
||||
|
||||
private Map<String, Class<?>> joinTypes;
|
||||
|
||||
|
||||
private final ReflectorFactory reflectorFactory = new BaseReflectorFactory() {
|
||||
@Override
|
||||
@ -270,6 +272,20 @@ public class TableInfo {
|
||||
return propertyColumnMapping.get(property);
|
||||
}
|
||||
|
||||
public Map<String, Class<?>> getJoinTypes() {
|
||||
return joinTypes;
|
||||
}
|
||||
|
||||
public void setJoinTypes(Map<String, Class<?>> joinTypes) {
|
||||
this.joinTypes = joinTypes;
|
||||
}
|
||||
|
||||
public void addJoinType(String fieldName, Class<?> clazz) {
|
||||
if (joinTypes == null) {
|
||||
joinTypes = new HashMap<>();
|
||||
}
|
||||
joinTypes.put(fieldName, clazz);
|
||||
}
|
||||
|
||||
void setColumnInfoList(List<ColumnInfo> columnInfoList) {
|
||||
this.columnInfoList = columnInfoList;
|
||||
@ -640,17 +656,17 @@ public class TableInfo {
|
||||
List<ResultMapping> resultMappings = new ArrayList<>();
|
||||
|
||||
for (ColumnInfo columnInfo : columnInfoList) {
|
||||
ResultMapping mapping = new ResultMapping.Builder(configuration, columnInfo.getProperty(),
|
||||
columnInfo.getColumn(), columnInfo.getPropertyType())
|
||||
ResultMapping mapping = new ResultMapping.Builder(configuration, columnInfo.property,
|
||||
columnInfo.column, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.typeHandler(columnInfo.buildTypeHandler())
|
||||
.build();
|
||||
resultMappings.add(mapping);
|
||||
|
||||
//add property mapper for sql as ...
|
||||
//add property mapper for sql: select xxx as property ...
|
||||
if (!Objects.equals(columnInfo.getColumn(), columnInfo.getProperty())) {
|
||||
ResultMapping propertyMapping = new ResultMapping.Builder(configuration, columnInfo.getProperty(),
|
||||
columnInfo.getProperty(), columnInfo.getPropertyType())
|
||||
ResultMapping propertyMapping = new ResultMapping.Builder(configuration, columnInfo.property,
|
||||
columnInfo.property, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.typeHandler(columnInfo.buildTypeHandler())
|
||||
.build();
|
||||
@ -659,8 +675,8 @@ public class TableInfo {
|
||||
}
|
||||
|
||||
for (IdInfo idInfo : primaryKeyList) {
|
||||
ResultMapping mapping = new ResultMapping.Builder(configuration, idInfo.getProperty(),
|
||||
idInfo.getColumn(), idInfo.getPropertyType())
|
||||
ResultMapping mapping = new ResultMapping.Builder(configuration, idInfo.property,
|
||||
idInfo.column, idInfo.propertyType)
|
||||
.flags(CollectionUtil.newArrayList(ResultFlag.ID))
|
||||
.jdbcType(idInfo.getJdbcType())
|
||||
.typeHandler(idInfo.buildTypeHandler())
|
||||
@ -668,10 +684,51 @@ public class TableInfo {
|
||||
resultMappings.add(mapping);
|
||||
}
|
||||
|
||||
if (joinTypes != null && !joinTypes.isEmpty()) {
|
||||
joinTypes.forEach((fieldName, fieldType) -> {
|
||||
|
||||
TableInfo joinTableInfo = TableInfoFactory.ofEntityClass(fieldType);
|
||||
List<ColumnInfo> joinTableInfoColumnInfoList = joinTableInfo.getColumnInfoList();
|
||||
|
||||
for (ColumnInfo joinColumnInfo : joinTableInfoColumnInfoList) {
|
||||
if (!existColumn(resultMappings, joinColumnInfo.column)) {
|
||||
ResultMapping mapping = new ResultMapping.Builder(configuration, fieldName + "." + joinColumnInfo.property,
|
||||
joinColumnInfo.column, joinColumnInfo.propertyType)
|
||||
.jdbcType(joinColumnInfo.jdbcType)
|
||||
.typeHandler(joinColumnInfo.buildTypeHandler())
|
||||
.build();
|
||||
resultMappings.add(mapping);
|
||||
}
|
||||
|
||||
//add property mapper for sql: select xxx as property ...
|
||||
if (!existColumn(resultMappings, joinColumnInfo.property)) {
|
||||
if (!Objects.equals(joinColumnInfo.column, joinColumnInfo.property)) {
|
||||
ResultMapping propertyMapping = new ResultMapping.Builder(configuration, fieldName + "." + joinColumnInfo.property,
|
||||
joinColumnInfo.property, joinColumnInfo.propertyType)
|
||||
.jdbcType(joinColumnInfo.jdbcType)
|
||||
.typeHandler(joinColumnInfo.buildTypeHandler())
|
||||
.build();
|
||||
resultMappings.add(propertyMapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new ResultMap.Builder(configuration, resultMapId, entityClass, resultMappings).build();
|
||||
}
|
||||
|
||||
|
||||
private static boolean existColumn(List<ResultMapping> resultMappings, String name) {
|
||||
for (ResultMapping resultMapping : resultMappings) {
|
||||
if (resultMapping.getColumn().equalsIgnoreCase(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private Object buildColumnSqlArg(MetaObject metaObject, String column) {
|
||||
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||
Object value = getPropertyValue(metaObject, columnInfo.property);
|
||||
|
||||
@ -195,7 +195,7 @@ public class TableInfoFactory {
|
||||
Set<String> defaultColumns = new LinkedHashSet<>();
|
||||
|
||||
|
||||
List<Field> entityFields = getAllFields(entityClass);
|
||||
List<Field> entityFields = getColumnFields(entityClass);
|
||||
|
||||
for (Field field : entityFields) {
|
||||
|
||||
@ -204,21 +204,21 @@ public class TableInfoFactory {
|
||||
continue; // ignore
|
||||
}
|
||||
|
||||
Class<?> fieldType = field.getType();
|
||||
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
//ignore static field
|
||||
//满足一下 3 中情况,不支持该类型
|
||||
if ((column == null || column.typeHandler() == UnknownTypeHandler.class) // 未配置 typeHandler
|
||||
&& !fieldType.isEnum() // 类型不是枚举
|
||||
&& !defaultSupportColumnTypes.contains(fieldType) //默认的自动类型不包含该类型
|
||||
) {
|
||||
if (!Map.class.isAssignableFrom(fieldType)
|
||||
&& !Collection.class.isAssignableFrom(fieldType)
|
||||
&& !fieldType.isArray()) {
|
||||
tableInfo.addJoinType(field.getName(), fieldType);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
//未配置 typeHandler 的情况下,只支持基本数据类型,不支持比如 list set 或者自定义的类等
|
||||
if ((column == null || column.typeHandler() == UnknownTypeHandler.class)
|
||||
&& !field.getType().isEnum()
|
||||
&& !defaultSupportColumnTypes.contains(field.getType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
//列名
|
||||
String columnName = column != null && StringUtil.isNotBlank(column.value())
|
||||
? column.value()
|
||||
@ -351,7 +351,7 @@ public class TableInfoFactory {
|
||||
}
|
||||
|
||||
|
||||
public static List<Field> getAllFields(Class<?> entityClass) {
|
||||
public static List<Field> getColumnFields(Class<?> entityClass) {
|
||||
List<Field> fields = new ArrayList<>();
|
||||
doGetFields(entityClass, fields);
|
||||
return fields;
|
||||
@ -365,9 +365,11 @@ public class TableInfoFactory {
|
||||
|
||||
Field[] declaredFields = entityClass.getDeclaredFields();
|
||||
for (Field declaredField : declaredFields) {
|
||||
if (!existName(fields, declaredField)) {
|
||||
fields.add(declaredField);
|
||||
if (Modifier.isStatic(declaredField.getModifiers())
|
||||
|| existName(fields, declaredField)) {
|
||||
continue;
|
||||
}
|
||||
fields.add(declaredField);
|
||||
}
|
||||
|
||||
doGetFields(entityClass.getSuperclass(), fields);
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
public class ArticleDTO01 {
|
||||
|
||||
private Long id;
|
||||
|
||||
private Long accountId;
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
|
||||
private Account account;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(Long accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Account getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(Account account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ArticleDTO01{" +
|
||||
"id=" + id +
|
||||
", accountId=" + accountId +
|
||||
", title='" + title + '\'' +
|
||||
", content='" + content + '\'' +
|
||||
", account=" + account +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -21,6 +21,8 @@ import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.row.Db;
|
||||
import com.mybatisflex.core.row.RowUtil;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@ -112,20 +114,35 @@ public class EntityTestStarter {
|
||||
// Object object = accountMapper.selectObjectByQuery(wrapper2);
|
||||
// System.out.println(object);
|
||||
//
|
||||
// QueryWrapper asWrapper = QueryWrapper.create()
|
||||
// .select(ARTICLE.ALL_COLUMNS)
|
||||
// .select(ACCOUNT.USER_NAME.as(ArticleDTO::getAuthorName)
|
||||
// , ACCOUNT.AGE.as(ArticleDTO::getAuthorAge)
|
||||
// , ACCOUNT.BIRTHDAY
|
||||
// )
|
||||
// .from(ARTICLE)
|
||||
// .leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||
//// .where(ACCOUNT.ID.ge(0));
|
||||
// .where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
||||
////
|
||||
//// List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
||||
//// System.out.println(articleDTOS);
|
||||
// Page<ArticleDTO> paginate = accountMapper.paginateAs(Page.of(1, 1), asWrapper, ArticleDTO.class);
|
||||
// System.out.println(paginate);
|
||||
|
||||
|
||||
|
||||
QueryWrapper asWrapper = QueryWrapper.create()
|
||||
.select(ARTICLE.ALL_COLUMNS)
|
||||
.select(ACCOUNT.USER_NAME.as(ArticleDTO::getAuthorName)
|
||||
, ACCOUNT.AGE.as(ArticleDTO::getAuthorAge)
|
||||
, ACCOUNT.BIRTHDAY
|
||||
)
|
||||
.select(ARTICLE.ALL_COLUMNS,ACCOUNT.ALL_COLUMNS)
|
||||
.from(ARTICLE)
|
||||
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||
// .where(ACCOUNT.ID.ge(0));
|
||||
.where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
||||
|
||||
RowUtil.printPretty(Db.selectListByQuery(asWrapper));
|
||||
//
|
||||
// List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
||||
// System.out.println(articleDTOS);
|
||||
Page<ArticleDTO> paginate = accountMapper.paginateAs(Page.of(1, 1), asWrapper, ArticleDTO.class);
|
||||
Page<ArticleDTO01> paginate = accountMapper.paginateAs(Page.of(1, 10), asWrapper, ArticleDTO01.class);
|
||||
System.out.println(paginate);
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user