From 6288556076635bbc5818bfbce281bb0e8fb7c849 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 17 Nov 2023 23:37:53 +0800 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=E4=B8=BA=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A=E7=9A=84=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/table/TableInfo.java | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 6f18d52a..8a541158 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -989,14 +989,14 @@ public class TableInfo { public ResultMap buildResultMap(Configuration configuration) { - return doBuildResultMap(configuration, new HashSet<>(), new HashSet<>(), false, getTableNameWithSchema()); + return doBuildResultMap(configuration, new HashSet<>(), false, getTableNameWithSchema()); } - private ResultMap doBuildResultMap(Configuration configuration, Set resultMapIds, Set existMappingColumns, boolean isNested, String nestedPrefix) { + private ResultMap doBuildResultMap(Configuration configuration, Set resultMapIds, boolean isNested, String nestedPrefix) { String resultMapId = isNested ? "nested-" + nestedPrefix + ":" + entityClass.getName() : entityClass.getName(); - //是否有循环引用 + // 是否有循环引用 boolean withCircularReference = resultMapIds.contains(resultMapId) || resultMapIds.contains(entityClass.getName()); if (withCircularReference) { return null; @@ -1007,27 +1007,26 @@ public class TableInfo { if (configuration.hasResultMap(resultMapId)) { return configuration.getResultMap(resultMapId); } - List resultMappings = new ArrayList<>(); + List resultMappings = new ArrayList<>(); // 标签下的 标签映射 for (IdInfo idInfo : primaryKeyList) { - doBuildColumnResultMapping(configuration, existMappingColumns, resultMappings, idInfo, CollectionUtil.newArrayList(ResultFlag.ID), isNested); + doBuildColumnResultMapping(configuration, resultMappings, idInfo, CollectionUtil.newArrayList(ResultFlag.ID)); } // 标签下的 标签映射 for (ColumnInfo columnInfo : columnInfoList) { - doBuildColumnResultMapping(configuration, existMappingColumns, resultMappings, columnInfo, Collections.emptyList(), isNested); + doBuildColumnResultMapping(configuration, resultMappings, columnInfo, Collections.emptyList()); } - // 标签下的 标签映射 if (associationType != null) { associationType.forEach((fieldName, fieldType) -> { // 获取嵌套类型的信息,也就是 javaType 属性 TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType); // 构建嵌套类型的 ResultMap 对象,也就是 标签下的内容 - ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix); + ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, true, nestedPrefix); if (nestedResultMap != null) { resultMappings.add(new ResultMapping.Builder(configuration, fieldName) .javaType(fieldType) @@ -1044,16 +1043,12 @@ public class TableInfo { // List List 等 String columnName = TableInfoFactory.getColumnName(camelToUnderline, field, field.getAnnotation(Column.class)); // 映射 - ResultMapping resultMapping = new ResultMapping.Builder(configuration, null) .column(columnName) .typeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(genericClass)) .build(); - String nestedResultMapId = entityClass.getName() + "." + field.getName(); - ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass - , Collections.singletonList(resultMapping)).build(); - + ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass, Collections.singletonList(resultMapping)).build(); configuration.addResultMap(nestedResultMap); // 映射 resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) @@ -1064,7 +1059,7 @@ public class TableInfo { // 获取集合泛型类型的信息,也就是 ofType 属性 TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass); // 构建嵌套类型的 ResultMap 对象,也就是 标签下的内容 - ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix); + ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, true, nestedPrefix); if (nestedResultMap != null) { resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) .javaType(field.getType()) @@ -1081,42 +1076,55 @@ public class TableInfo { return resultMap; } + private void doBuildColumnResultMapping(Configuration configuration, List resultMappings + , ColumnInfo columnInfo, List flags) { - private void doBuildColumnResultMapping(Configuration configuration, Set existMappingColumns, List resultMappings - , ColumnInfo columnInfo, List flags, boolean isNested) { - String[] columns = ArrayUtil.concat(new String[]{columnInfo.column, columnInfo.property}, columnInfo.alias); - for (String column : columns) { - if (!existMappingColumns.contains(column)) { - ResultMapping mapping = new ResultMapping.Builder(configuration + // userName -> user_name + resultMappings.add(new ResultMapping.Builder(configuration + , columnInfo.property + , columnInfo.column + , columnInfo.propertyType) + .jdbcType(columnInfo.getJdbcType()) + .flags(flags) + .typeHandler(columnInfo.buildTypeHandler(configuration)) + .build()); + + // userName -> tb_user$user_name + resultMappings.add(new ResultMapping.Builder(configuration + , columnInfo.property + , tableName + "$" + columnInfo.column + , columnInfo.propertyType) + .jdbcType(columnInfo.getJdbcType()) + .flags(flags) + .typeHandler(columnInfo.buildTypeHandler(configuration)) + .build()); + + if (!Objects.equals(columnInfo.column, columnInfo.property)) { + // userName -> userName + resultMappings.add(new ResultMapping.Builder(configuration + , columnInfo.property + , columnInfo.property + , columnInfo.propertyType) + .jdbcType(columnInfo.getJdbcType()) + .flags(flags) + .typeHandler(columnInfo.buildTypeHandler(configuration)) + .build()); + } + + if (ArrayUtil.isNotEmpty(columnInfo.alias)) { + for (String alias : columnInfo.alias) { + // userName -> alias + resultMappings.add(new ResultMapping.Builder(configuration , columnInfo.property - , column + , alias , columnInfo.propertyType) .jdbcType(columnInfo.getJdbcType()) .flags(flags) .typeHandler(columnInfo.buildTypeHandler(configuration)) - .build(); - resultMappings.add(mapping); - existMappingColumns.add(mapping.getColumn()); + .build()); } } - if (isNested) { - for (String column : columns) { - column = tableName + "$" + column; - if (!existMappingColumns.contains(column)) { - ResultMapping mapping = new ResultMapping.Builder(configuration - , columnInfo.property - , column - , columnInfo.propertyType) - .jdbcType(columnInfo.getJdbcType()) - .flags(flags) - .typeHandler(columnInfo.buildTypeHandler(configuration)) - .build(); - resultMappings.add(mapping); - existMappingColumns.add(mapping.getColumn()); - } - } - } } From ea37895c6a0ae8714a07613effb695712ab680c4 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 17 Nov 2023 23:38:34 +0800 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=E5=A4=84=E7=90=86=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=88=97=E9=87=8D=E5=90=8D=E6=83=85=E5=86=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/dialect/impl/CommonsDialectImpl.java | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 165f8b44..2264d854 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -25,7 +25,6 @@ import com.mybatisflex.core.query.*; import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.RowCPI; import com.mybatisflex.core.table.TableInfo; -import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.update.RawValue; import com.mybatisflex.core.util.ArrayUtil; import com.mybatisflex.core.util.CollectionUtil; @@ -33,6 +32,9 @@ import com.mybatisflex.core.util.SqlUtil; import com.mybatisflex.core.util.StringUtil; import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static com.mybatisflex.core.constant.SqlConsts.*; @@ -351,36 +353,33 @@ public class CommonsDialectImpl implements IDialect { List selectColumns = CPI.getSelectColumns(queryWrapper); - int queryTablesCount = queryTables == null ? 0 : queryTables.size(); - int joinTablesCount = joinTables != null ? joinTables.size() : 0; - - //多表查询时,自动映射 - if (queryTablesCount > 0 && queryTablesCount + joinTablesCount > 1) { - QueryTable firstTable = queryTables.get(0); - if (!(firstTable instanceof SelectQueryTable)) { - TableInfo tableInfo = TableInfoFactory.ofTableName(firstTable.getName()); - if (tableInfo != null && selectColumns != null && !selectColumns.isEmpty()) { - String[] firstTableColumns = tableInfo.getAllColumns(); - for (int i = 0; i < selectColumns.size(); i++) { - QueryColumn selectColumn = selectColumns.get(i); - QueryTable selectColumnTable = selectColumn.getTable(); - String selectColumnName = selectColumn.getName(); - - //用户未配置别名的情况下,自动未用户添加别名 - if (selectColumnTable != null - && selectColumnName != null - && !"*".equals(selectColumnName) - && StringUtil.isBlank(selectColumn.getAlias()) - && !(selectColumnTable instanceof SelectQueryTable) - && !CPI.isSameTable(firstTable, selectColumnTable) - && ArrayUtil.contains(firstTableColumns, selectColumnName) - ) { - QueryColumn newSelectColumn = selectColumn.as(selectColumnTable.getName() + "$" + selectColumnName); - selectColumns.set(i, newSelectColumn); - } - } - } - } + // 多个表,需要处理重名字段 + if (allTables.size() > 1) { + IntStream.range(0, selectColumns.size()) + .boxed() + // 生成 索引-字段值 对应关系 + .collect(Collectors.toMap(Function.identity(), selectColumns::get)) + .entrySet() + .stream() + // 需要处理别名的情况 + .filter(e -> !"*".equals(e.getValue().getName())) + .filter(e -> StringUtil.isNotBlank(e.getValue().getName())) + // 将相同字段对象放在一个集合里 + .collect(Collectors.groupingBy(e -> e.getValue().getName(), + Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))) + .values() + .stream() + // 过滤出来重名的字段 + .filter(e -> e.size() > 1) + // 合并所有需要加别名的字段 + .flatMap(Collection::stream) + // 过滤出来需要添加别名的列 + .filter(e -> StringUtil.isBlank(e.getValue().getAlias())) + .filter(e -> e.getValue().getTable() != null) + .filter(e -> StringUtil.isNotBlank(e.getValue().getTable().getName())) + // 添加别名并放回原集合索引位置 + .forEach(e -> selectColumns.set(e.getKey(), + e.getValue().as(e.getValue().getTable().getName() + "$" + e.getValue().getName()))); } StringBuilder sqlBuilder = new StringBuilder(); From 4e06242d06a39f190b68b47150361bc602b4f013 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 17 Nov 2023 23:39:04 +0800 Subject: [PATCH 3/7] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20SQL=20?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=BE=93=E5=87=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/test/SampleApplication.java | 9 +++++---- mybatis-flex-test/pom.xml | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java index 31632080..91232449 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/SampleApplication.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.test; +import com.github.vertical_blank.sqlformatter.SqlFormatter; import com.mybatisflex.core.audit.AuditManager; import com.mybatisflex.core.audit.ConsoleMessageCollector; import com.mybatisflex.core.audit.MessageCollector; @@ -50,11 +51,11 @@ public class SampleApplication implements CommandLineRunner, ApplicationListener @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("onApplicationEvent"); - //开启审计功能 + // 开启审计功能 AuditManager.setAuditEnable(true); - -//设置 SQL 审计收集器 - MessageCollector collector = new ConsoleMessageCollector(); + // 设置 SQL 审计收集器 + MessageCollector collector = new ConsoleMessageCollector((sql, tookTimeMillis) -> + System.out.println(SqlFormatter.format(sql))); AuditManager.setMessageCollector(collector); } diff --git a/mybatis-flex-test/pom.xml b/mybatis-flex-test/pom.xml index edbd985c..4a10d6c7 100644 --- a/mybatis-flex-test/pom.xml +++ b/mybatis-flex-test/pom.xml @@ -26,6 +26,14 @@ 8 + + + com.github.vertical-blank + sql-formatter + 2.0.4 + + + From addf72d5cd982843660c7b72a1f3e01b4ce52842 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 17 Nov 2023 23:40:03 +0800 Subject: [PATCH 4/7] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E5=90=8D=E6=98=A0=E5=B0=84=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/test/alisa/BaseEntity.java | 77 ++++++++++++ .../com/mybatisflex/test/alisa/SysDept.java | 56 +++++++++ .../com/mybatisflex/test/alisa/SysRole.java | 67 ++++++++++ .../com/mybatisflex/test/alisa/SysUser.java | 109 ++++++++++++++++ .../src/main/resources/alisa.sql | 81 ++++++++++++ .../mybatisflex/test/mapper/AlisaTest.java | 117 ++++++++++++++++++ 6 files changed, 507 insertions(+) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/BaseEntity.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysDept.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysRole.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysUser.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/alisa.sql create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/BaseEntity.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/BaseEntity.java new file mode 100644 index 00000000..25ebe23e --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/BaseEntity.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test.alisa; + +import java.io.Serializable; +import java.util.Date; + +/** + * 父类。 + * + * @author 王帅 + * @since 2023-11-16 + */ +public abstract class BaseEntity implements Serializable { + + private Date createTime; + private String createBy; + private Date updateTime; + private String updateBy; + + public Date getCreateTime() { + return createTime; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + + public String getCreateBy() { + return createBy; + } + + public void setCreateBy(String createBy) { + this.createBy = createBy; + } + + public Date getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(Date updateTime) { + this.updateTime = updateTime; + } + + public String getUpdateBy() { + return updateBy; + } + + public void setUpdateBy(String updateBy) { + this.updateBy = updateBy; + } + + @Override + public String toString() { + return "BaseEntity{" + + "createTime=" + createTime + + ", createBy='" + createBy + '\'' + + ", updateTime=" + updateTime + + ", updateBy='" + updateBy + '\'' + + '}'; + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysDept.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysDept.java new file mode 100644 index 00000000..49e13bde --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysDept.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test.alisa; + +import com.mybatisflex.annotation.Table; + +/** + * 部门。 + * + * @author 王帅 + * @since 2023-11-16 + */ +@Table("sys_dept") +public class SysDept extends BaseEntity { + + private Integer id; + private String deptName; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + @Override + public String toString() { + return "SysDept{" + + "id=" + id + + ", deptName='" + deptName + '\'' + + '}' + super.toString(); + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysRole.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysRole.java new file mode 100644 index 00000000..107b159c --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysRole.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test.alisa; + +import com.mybatisflex.annotation.Table; + +/** + * 角色。 + * + * @author 王帅 + * @since 2023-11-16 + */ +@Table("sys_role") +public class SysRole extends BaseEntity { + + private Integer id; + private String roleKey; + private String roleName; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getRoleKey() { + return roleKey; + } + + public void setRoleKey(String roleKey) { + this.roleKey = roleKey; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + @Override + public String toString() { + return "SysRole{" + + "id=" + id + + ", roleKey='" + roleKey + '\'' + + ", roleName='" + roleName + '\'' + + '}' + super.toString(); + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysUser.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysUser.java new file mode 100644 index 00000000..6a8016ed --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/alisa/SysUser.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test.alisa; + +import com.mybatisflex.annotation.ColumnAlias; +import com.mybatisflex.annotation.Table; + +import java.util.Date; +import java.util.List; + +/** + * 用户。 + * + * @author 王帅 + * @since 2023-11-16 + */ +@Table("sys_user") +public class SysUser extends BaseEntity { + + private Integer id; + private String userName; + @ColumnAlias("user_age") + private Integer age; + private Date birthday; + + private List roleList; + private List deptList; + + public List getRoleList() { + return roleList; + } + + public void setRoleList(List roleList) { + this.roleList = roleList; + } + + public List getDeptList() { + return deptList; + } + + public void setDeptList(List deptList) { + this.deptList = deptList; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + @Override + @ColumnAlias("user_create_by") + public String getCreateBy() { + return super.getCreateBy(); + } + + @Override + public String toString() { + return "SysUser{" + + "id=" + id + + ", userName='" + userName + '\'' + + ", age=" + age + + ", birthday=" + birthday + + ", roleList=" + roleList + + ", deptList=" + deptList + + '}' + super.toString(); + } + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/alisa.sql b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/alisa.sql new file mode 100644 index 00000000..e41c91d2 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/alisa.sql @@ -0,0 +1,81 @@ +-- Alisa Test + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for sys_dept +-- ---------------------------- +DROP TABLE IF EXISTS `sys_dept`; +CREATE TABLE `sys_dept` +( + `id` int NOT NULL AUTO_INCREMENT, + `dept_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `create_time` datetime NULL DEFAULT NULL, + `update_time` datetime NULL DEFAULT NULL, + `create_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `update_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB + CHARACTER SET = utf8mb4 + COLLATE = utf8mb4_0900_ai_ci + ROW_FORMAT = DYNAMIC; + +-- ---------------------------- +-- Records of sys_dept +-- ---------------------------- +INSERT INTO `sys_dept` +VALUES (1, '开发岗', NULL, NULL, 'DEPT', 'DEPT'); + +-- ---------------------------- +-- Table structure for sys_role +-- ---------------------------- +DROP TABLE IF EXISTS `sys_role`; +CREATE TABLE `sys_role` +( + `id` int NOT NULL AUTO_INCREMENT, + `role_key` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `role_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `create_time` datetime NULL DEFAULT NULL, + `update_time` datetime NULL DEFAULT NULL, + `create_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `update_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB + CHARACTER SET = utf8mb4 + COLLATE = utf8mb4_0900_ai_ci + ROW_FORMAT = DYNAMIC; + +-- ---------------------------- +-- Records of sys_role +-- ---------------------------- +INSERT INTO `sys_role` +VALUES (1, 'ROOT', '超级管理员', '2000-11-17 22:15:20', '2000-11-17 22:54:14', 'ROLE', 'ROLE'); + +-- ---------------------------- +-- Table structure for sys_user +-- ---------------------------- +DROP TABLE IF EXISTS `sys_user`; +CREATE TABLE `sys_user` +( + `id` int NOT NULL AUTO_INCREMENT, + `user_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `age` int NULL DEFAULT NULL, + `birthday` datetime NULL DEFAULT NULL, + `create_time` datetime NULL DEFAULT NULL, + `update_time` datetime NULL DEFAULT NULL, + `create_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `update_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB + CHARACTER SET = utf8mb4 + COLLATE = utf8mb4_0900_ai_ci + ROW_FORMAT = DYNAMIC; + +-- ---------------------------- +-- Records of sys_user +-- ---------------------------- +INSERT INTO `sys_user` +VALUES (1, '张三', 18, '2023-11-17 22:14:40', '2023-11-17 22:54:34', '2023-11-21 22:14:54', 'USER', 'USER'); + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java new file mode 100644 index 00000000..37741dcb --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test.mapper; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.test.alisa.SysUser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +import static com.mybatisflex.core.query.QueryMethods.column; +import static com.mybatisflex.core.query.QueryMethods.select; +import static com.mybatisflex.test.alisa.table.SysDeptTableDef.SYS_DEPT; +import static com.mybatisflex.test.alisa.table.SysRoleTableDef.SYS_ROLE; +import static com.mybatisflex.test.alisa.table.SysUserTableDef.SYS_USER; + +/** + * 别名测试。 + * + * @author 王帅 + * @since 2023-11-16 + */ +@SpringBootTest +class AlisaTest { + + @Autowired + SysUserMapper userMapper; + + @Autowired + ObjectMapper objectMapper; + + void printList(QueryWrapper queryWrapper) { + List users = userMapper.selectListByQuery(queryWrapper); + Assertions.assertDoesNotThrow(() -> + System.out.println(objectMapper.writerWithDefaultPrettyPrinter() + .writeValueAsString(users))); + } + + @Test + void test01() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(SYS_USER.DEFAULT_COLUMNS) + .select(SYS_ROLE.DEFAULT_COLUMNS) + .from(SYS_USER.as("u")) + .leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID)); + + printList(queryWrapper); + } + + @Test + void test02() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(SYS_USER.DEFAULT_COLUMNS) + .select(SYS_ROLE.DEFAULT_COLUMNS) + .select(SYS_DEPT.DEFAULT_COLUMNS) + .from(SYS_USER.as("u")) + .leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID)) + .leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(SYS_DEPT.ID)); + + printList(queryWrapper); + } + + @Test + void test03() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(SYS_USER.ALL_COLUMNS) + .select(SYS_ROLE.ALL_COLUMNS) + .select(SYS_DEPT.ALL_COLUMNS) + .from(SYS_USER.as("u")) + .leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID)) + .leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(SYS_DEPT.ID)); + + printList(queryWrapper); + } + + @Test + void test04() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(column("`u`.`create_by` AS `sys_user$create_by`")) + .select(column("`u`.`update_by` AS `sys_user$update_by`")) + .from(SYS_USER.as("u")); + + printList(queryWrapper); + } + + @Test + void test05() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(column("`u`.`create_by`")) + .select(column("`u`.`update_by`")) + .select(column("`d`.`create_by`")) + .select(column("`d`.`update_by`")) + .from(select(column("*")).from(SYS_USER)).as("u") + .from(SYS_DEPT.as("d")); + + printList(queryWrapper); + } + +} From c492a6cd6b5ed8c120a3d3bb5c835344907ffb25 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 17 Nov 2023 23:52:36 +0800 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E9=87=8D?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E6=AE=B5=E8=BF=87=E6=BB=A4=E9=93=BE=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 2264d854..7f7ed072 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -362,6 +362,7 @@ public class CommonsDialectImpl implements IDialect { .entrySet() .stream() // 需要处理别名的情况 + .filter(e -> StringUtil.isBlank(e.getValue().getAlias())) .filter(e -> !"*".equals(e.getValue().getName())) .filter(e -> StringUtil.isNotBlank(e.getValue().getName())) // 将相同字段对象放在一个集合里 @@ -373,8 +374,7 @@ public class CommonsDialectImpl implements IDialect { .filter(e -> e.size() > 1) // 合并所有需要加别名的字段 .flatMap(Collection::stream) - // 过滤出来需要添加别名的列 - .filter(e -> StringUtil.isBlank(e.getValue().getAlias())) + // 过滤出来可以添加别名的列 .filter(e -> e.getValue().getTable() != null) .filter(e -> StringUtil.isNotBlank(e.getValue().getTable().getName())) // 添加别名并放回原集合索引位置 From affd38b0b7c978a7a86c83f4fed960ac89899db6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 17 Nov 2023 23:57:43 +0800 Subject: [PATCH 6/7] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E9=87=8D?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E6=AE=B5=E8=BF=87=E6=BB=A4=E9=93=BE=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 7f7ed072..e8a91998 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -362,9 +362,9 @@ public class CommonsDialectImpl implements IDialect { .entrySet() .stream() // 需要处理别名的情况 + .filter(e -> StringUtil.isNotBlank(e.getValue().getName())) .filter(e -> StringUtil.isBlank(e.getValue().getAlias())) .filter(e -> !"*".equals(e.getValue().getName())) - .filter(e -> StringUtil.isNotBlank(e.getValue().getName())) // 将相同字段对象放在一个集合里 .collect(Collectors.groupingBy(e -> e.getValue().getName(), Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))) From 9e9144937548807cee2fd4acf2803e267b77ee56 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 20 Nov 2023 22:12:36 +0800 Subject: [PATCH 7/7] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E6=98=A0?= =?UTF-8?q?=E5=B0=84=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mybatisflex/test/mapper/AlisaTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java index 37741dcb..b6e9426d 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AlisaTest.java @@ -114,4 +114,15 @@ class AlisaTest { printList(queryWrapper); } + @Test + void test06() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(SYS_USER.ID, SYS_USER.USER_NAME, SYS_USER.AGE, SYS_USER.BIRTHDAY) + .select(SYS_ROLE.CREATE_BY.as("sys_role$create_by")) + .from(SYS_USER.as("u")) + .leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID)); + + printList(queryWrapper); + } + }