mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
commit
2c75ded0f7
@ -26,7 +26,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;
|
||||
@ -34,6 +33,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.*;
|
||||
|
||||
@ -358,36 +360,33 @@ public class CommonsDialectImpl implements IDialect {
|
||||
|
||||
List<QueryColumn> 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 -> StringUtil.isNotBlank(e.getValue().getName()))
|
||||
.filter(e -> StringUtil.isBlank(e.getValue().getAlias()))
|
||||
.filter(e -> !"*".equals(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 -> 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();
|
||||
|
||||
@ -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<String> resultMapIds, Set<String> existMappingColumns, boolean isNested, String nestedPrefix) {
|
||||
private ResultMap doBuildResultMap(Configuration configuration, Set<String> 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<ResultMapping> resultMappings = new ArrayList<>();
|
||||
|
||||
List<ResultMapping> resultMappings = new ArrayList<>();
|
||||
|
||||
// <resultMap> 标签下的 <id> 标签映射
|
||||
for (IdInfo idInfo : primaryKeyList) {
|
||||
doBuildColumnResultMapping(configuration, existMappingColumns, resultMappings, idInfo, CollectionUtil.newArrayList(ResultFlag.ID), isNested);
|
||||
doBuildColumnResultMapping(configuration, resultMappings, idInfo, CollectionUtil.newArrayList(ResultFlag.ID));
|
||||
}
|
||||
|
||||
// <resultMap> 标签下的 <result> 标签映射
|
||||
for (ColumnInfo columnInfo : columnInfoList) {
|
||||
doBuildColumnResultMapping(configuration, existMappingColumns, resultMappings, columnInfo, Collections.emptyList(), isNested);
|
||||
doBuildColumnResultMapping(configuration, resultMappings, columnInfo, Collections.emptyList());
|
||||
}
|
||||
|
||||
|
||||
// <resultMap> 标签下的 <association> 标签映射
|
||||
if (associationType != null) {
|
||||
associationType.forEach((fieldName, fieldType) -> {
|
||||
// 获取嵌套类型的信息,也就是 javaType 属性
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType);
|
||||
// 构建嵌套类型的 ResultMap 对象,也就是 <association> 标签下的内容
|
||||
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<String> List<Integer> 等
|
||||
String columnName = TableInfoFactory.getColumnName(camelToUnderline, field, field.getAnnotation(Column.class));
|
||||
// 映射 <result column="..."/>
|
||||
|
||||
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);
|
||||
// 映射 <collection property="..." ofType="genericClass">
|
||||
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
|
||||
@ -1064,7 +1059,7 @@ public class TableInfo {
|
||||
// 获取集合泛型类型的信息,也就是 ofType 属性
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
|
||||
// 构建嵌套类型的 ResultMap 对象,也就是 <collection> 标签下的内容
|
||||
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<ResultMapping> resultMappings
|
||||
, ColumnInfo columnInfo, List<ResultFlag> flags) {
|
||||
|
||||
private void doBuildColumnResultMapping(Configuration configuration, Set<String> existMappingColumns, List<ResultMapping> resultMappings
|
||||
, ColumnInfo columnInfo, List<ResultFlag> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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<SysRole> roleList;
|
||||
private List<SysDept> deptList;
|
||||
|
||||
public List<SysRole> getRoleList() {
|
||||
return roleList;
|
||||
}
|
||||
|
||||
public void setRoleList(List<SysRole> roleList) {
|
||||
this.roleList = roleList;
|
||||
}
|
||||
|
||||
public List<SysDept> getDeptList() {
|
||||
return deptList;
|
||||
}
|
||||
|
||||
public void setDeptList(List<SysDept> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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<SysUser> 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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -32,6 +32,9 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.vertical-blank</groupId>
|
||||
<artifactId>sql-formatter</artifactId>
|
||||
<version>2.0.4</version>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user