From 78214c11f1411547c4980a3a005d8e1635256e6f Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 7 Jun 2023 15:03:24 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=98=A0=E5=B0=84=E5=AE=9E=E4=BD=93=E7=B1=BB=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=20association=20=E5=92=8C=20collection=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/mybatis/FlexConfiguration.java | 81 ++++++--- .../com/mybatisflex/core/table/TableInfo.java | 172 ++++++++++++++++-- .../core/table/TableInfoFactory.java | 16 +- 3 files changed, 225 insertions(+), 44 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 99ad428b..5c700fa0 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -1,17 +1,17 @@ -/** - * 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. +/* + * 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.core.mybatis; @@ -27,7 +27,6 @@ import com.mybatisflex.core.row.RowMapper; import com.mybatisflex.core.table.EntityWrapperFactory; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; -import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.executor.CachingExecutor; import org.apache.ibatis.executor.Executor; @@ -36,15 +35,15 @@ import org.apache.ibatis.executor.keygen.NoKeyGenerator; import org.apache.ibatis.executor.keygen.SelectKeyGenerator; import org.apache.ibatis.executor.parameter.ParameterHandler; import org.apache.ibatis.executor.statement.StatementHandler; -import org.apache.ibatis.mapping.BoundSql; -import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.mapping.MappedStatement; -import org.apache.ibatis.mapping.ResultMap; +import org.apache.ibatis.mapping.*; import org.apache.ibatis.session.*; import org.apache.ibatis.transaction.Transaction; import org.apache.ibatis.util.MapUtil; import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -178,14 +177,40 @@ public class FlexConfiguration extends Configuration { String resultMapId = tableInfo.getEntityClass().getName(); - ResultMap resultMap; + /*ResultMap resultMap; if (hasResultMap(resultMapId)) { resultMap = getResultMap(resultMapId); } else { resultMap = tableInfo.buildResultMap(this); this.addResultMap(resultMap); + }*/ + + // 变量名与属性名区分开 + List resultMapList; + if (hasResultMap(resultMapId)) { + resultMapList = new ArrayList<>(); + fillResultMapList(resultMapId, resultMapList); + } else { + resultMapList = tableInfo.buildResultMapList(this); + for (ResultMap resultMap : resultMapList) { + if (!hasResultMap(resultMap.getId())) { + addResultMap(resultMap); + } + } } + /* + * 这里解释一下为什么要反转这个集合: + * + * MyBatis 在解析 ResultMaps 的时候,是按照顺序一个一个进行解析的,对于有嵌套 + * 的 ResultMap 对象,也就是 nestResultMap 需要放在靠前的位置,首先解析。 + * + * 而我们进行递归 buildResultMapList 也好,fillResultMapList 也好,都是 + * 非嵌套 ResultMap 在集合最开始的位置,所以要反转一下集合,将 hasNestedResultMaps + * 的 ResultMap 对象放到集合的最前面。 + */ + Collections.reverse(resultMapList); + return new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), ms.getSqlSource(), ms.getSqlCommandType()) .resource(ms.getResource()) .fetchSize(ms.getFetchSize()) @@ -198,7 +223,8 @@ public class FlexConfiguration extends Configuration { .lang(ms.getLang()) .resultOrdered(ms.isResultOrdered()) .resultSets(ms.getResultSets() == null ? null : String.join(",", ms.getResultSets())) - .resultMaps(CollectionUtil.newArrayList(resultMap)) // 替换resultMap + //.resultMaps(CollectionUtil.newArrayList(resultMap)) // 替换resultMap + .resultMaps(resultMapList) .resultSetType(ms.getResultSetType()) .flushCacheRequired(ms.isFlushCacheRequired()) .useCache(ms.isUseCache()) @@ -206,6 +232,19 @@ public class FlexConfiguration extends Configuration { .build(); } + private void fillResultMapList(String resultMapId, List resultMapList) { + ResultMap resultMap = this.getResultMap(resultMapId); + resultMapList.add(resultMap); + if (resultMap.hasNestedResultMaps()) { + for (ResultMapping resultMapping : resultMap.getResultMappings()) { + String nestedResultMapId = resultMapping.getNestedResultMapId(); + if (nestedResultMapId != null) { + fillResultMapList(nestedResultMapId, resultMapList); + } + } + } + } + /** * 生成新的、已替换主键生成器的 MappedStatement * 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 28c1db8f..13730c7a 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 @@ -1,17 +1,17 @@ -/** - * 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. +/* + * 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.core.table; @@ -39,6 +39,7 @@ import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.util.MapUtil; +import java.lang.reflect.Field; import java.lang.reflect.Proxy; import java.sql.ResultSet; import java.sql.SQLException; @@ -96,9 +97,24 @@ public class TableInfo { private List onUpdateListeners; private List onSetListeners; + /** + * @deprecated 该功能有更好的方式实现,此属性可能会被移除。 + */ + @Deprecated private Map> joinTypes; + /** + * 对应 MapperXML 配置文件中 {@code } 标签下的 {@code } 标签。 + */ + private Map> associationType; + + /** + * 对应 MapperXML 配置文件中 {@code } 标签下的 {@code } 标签。 + */ + private Map> collectionType; + + private final ReflectorFactory reflectorFactory = new BaseReflectorFactory() { @Override public Reflector findForClass(Class type) { @@ -119,11 +135,11 @@ public class TableInfo { return tableName; } - public String getWrapSchemaAndTableName(IDialect dialect){ - if (StringUtil.isNotBlank(schema)){ - return dialect.wrap(dialect.getRealSchema(schema)) +"." + dialect.wrap(dialect.getRealTable(tableName)); - }else { - return dialect.wrap(dialect.getRealTable(tableName)); + public String getWrapSchemaAndTableName(IDialect dialect) { + if (StringUtil.isNotBlank(schema)) { + return dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(tableName)); + } else { + return dialect.wrap(dialect.getRealTable(tableName)); } } @@ -296,6 +312,36 @@ public class TableInfo { joinTypes.put(fieldName, clazz); } + public Map> getAssociationType() { + return associationType; + } + + public void setAssociationType(Map> associationType) { + this.associationType = associationType; + } + + public void addAssociationType(String fieldName, Class clazz) { + if (associationType == null) { + associationType = new HashMap<>(); + } + associationType.put(fieldName, clazz); + } + + public Map> getCollectionType() { + return collectionType; + } + + public void setCollectionType(Map> collectionType) { + this.collectionType = collectionType; + } + + public void addCollectionType(Field field, Class genericClass) { + if (collectionType == null) { + collectionType = new HashMap<>(); + } + collectionType.put(field, genericClass); + } + void setColumnInfoList(List columnInfoList) { this.columnInfoList = columnInfoList; this.columns = new String[columnInfoList.size()]; @@ -660,6 +706,10 @@ public class TableInfo { .collect(Collectors.toList()); } + /** + * @deprecated 该功能有更好的方式实现,此方法可能会被移除。 + */ + @Deprecated public ResultMap buildResultMap(Configuration configuration) { String resultMapId = entityClass.getName(); List resultMappings = new ArrayList<>(); @@ -727,6 +777,90 @@ public class TableInfo { return new ResultMap.Builder(configuration, resultMapId, entityClass, resultMappings).build(); } + public List buildResultMapList(Configuration configuration) { + String resultMapId = entityClass.getName(); + List resultMaps = new ArrayList<>(); + List resultMappings = new ArrayList<>(); + + // 标签下的 标签映射 + for (ColumnInfo columnInfo : columnInfoList) { + 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: select xxx as property ... + if (!Objects.equals(columnInfo.getColumn(), columnInfo.getProperty())) { + ResultMapping propertyMapping = new ResultMapping.Builder(configuration, columnInfo.property, + columnInfo.property, columnInfo.propertyType) + .jdbcType(columnInfo.getJdbcType()) + .typeHandler(columnInfo.buildTypeHandler()) + .build(); + resultMappings.add(propertyMapping); + } + } + + // 标签下的 标签映射 + for (IdInfo idInfo : primaryKeyList) { + ResultMapping mapping = new ResultMapping.Builder(configuration, idInfo.property, + idInfo.column, idInfo.propertyType) + .flags(CollectionUtil.newArrayList(ResultFlag.ID)) + .jdbcType(idInfo.getJdbcType()) + .typeHandler(idInfo.buildTypeHandler()) + .build(); + resultMappings.add(mapping); + } + + // 标签下的 标签映射 + if (associationType != null) { + associationType.forEach((fieldName, fieldType) -> { + // 获取嵌套类型的信息,也就是 javaType 属性 + TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType); + // 构建嵌套类型的 ResultMap 对象,也就是 标签下的内容 + // 这里是递归调用,直到嵌套类型里面没有其他嵌套类型或者集合类型为止 + List resultMapList = tableInfo.buildResultMapList(configuration); + // 寻找是否有嵌套 ResultMap 引用 + Optional nestedResultMap = resultMapList.stream() + .filter(e -> fieldType.getName().equals(e.getId())) + .findFirst(); + // 处理嵌套类型 ResultMapping 引用 + nestedResultMap.ifPresent(resultMap -> resultMappings.add(new ResultMapping.Builder(configuration, fieldName) + .javaType(fieldType) + .nestedResultMapId(resultMap.getId()) + .build())); + // 全部添加到 ResultMap 集合当中 + resultMaps.addAll(resultMapList); + }); + } + + // 标签下的 标签映射 + if (collectionType != null) { + collectionType.forEach((field, genericClass) -> { + // 获取集合泛型类型的信息,也就是 ofType 属性 + TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass); + // 构建嵌套类型的 ResultMap 对象,也就是 标签下的内容 + // 这里是递归调用,直到集合类型里面没有其他嵌套类型或者集合类型为止 + List resultMapList = tableInfo.buildResultMapList(configuration); + // 寻找是否有嵌套 ResultMap 引用 + Optional nestedResultMap = resultMapList.stream() + .filter(e -> genericClass.getName().equals(e.getId())) + .findFirst(); + // 处理嵌套类型 ResultMapping 引用 + nestedResultMap.ifPresent(resultMap -> resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) + .javaType(field.getType()) + .nestedResultMapId(resultMap.getId()) + .build())); + // 全部添加到 ResultMap 集合当中 + resultMaps.addAll(resultMapList); + }); + } + + resultMaps.add(new ResultMap.Builder(configuration, resultMapId, entityClass, resultMappings).build()); + + return resultMaps; + } private static boolean existColumn(List resultMappings, String name) { for (ResultMapping resultMapping : resultMappings) { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java index 61e1ebc5..4c1ab6aa 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java @@ -212,11 +212,19 @@ public class TableInfoFactory { && !fieldType.isEnum() // 类型不是枚举 && !defaultSupportColumnTypes.contains(fieldType) //默认的自动类型不包含该类型 ) { - if (!Map.class.isAssignableFrom(fieldType) - && !Collection.class.isAssignableFrom(fieldType) - && !fieldType.isArray()) { - tableInfo.addJoinType(field.getName(), fieldType); + // 集合嵌套 + if (Collection.class.isAssignableFrom(fieldType)) { + ParameterizedType genericType = (ParameterizedType) field.getGenericType(); + Type actualTypeArgument = genericType.getActualTypeArguments()[0]; + tableInfo.addCollectionType(field, (Class) actualTypeArgument); } + // 实体类嵌套 + else if (!Map.class.isAssignableFrom(fieldType) + && !fieldType.isArray()) { + // tableInfo.addJoinType(field.getName(), fieldType); + tableInfo.addAssociationType(field.getName(), fieldType); + } + // 不支持的类型直接跳过 continue; } From 7b606a0d48292c555db767d1ebcf17c62999e26a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 7 Jun 2023 15:04:06 +0800 Subject: [PATCH 2/6] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E8=BF=9E?= =?UTF-8?q?=E8=A1=A8=E6=9F=A5=E8=AF=A2=E8=87=AA=E5=8A=A8=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis-flex-spring-boot-test/pom.xml | 13 ++- .../java/com/mybatisflex/test/model/Role.java | 88 ++++++++++++++ .../java/com/mybatisflex/test/model/User.java | 77 +++++++++++++ .../com/mybatisflex/test/model/UserRole.java | 67 +++++++++++ .../com/mybatisflex/test/model/UserVO.java | 97 ++++++++++++++++ .../com/mybatisflex/test/model/UserVO1.java | 85 ++++++++++++++ .../com/mybatisflex/test/model/UserVO2.java | 85 ++++++++++++++ .../com/mybatisflex/test/model/UserVO3.java | 108 ++++++++++++++++++ .../src/main/resources/application.yml | 21 ++-- .../main/resources/mapper/accountMapper.xml | 15 +++ .../test/mapper/UserMapperTest.java | 73 ++++++++++++ 11 files changed, 715 insertions(+), 14 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Role.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserRole.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO1.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO2.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO3.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UserMapperTest.java diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index fe496067..46e643cd 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -45,13 +45,18 @@ spring-boot-starter-data-jdbc - - com.h2database - h2 - 2.1.214 + mysql + mysql-connector-java + + + + + + + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Role.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Role.java new file mode 100644 index 00000000..f3dbca0e --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Role.java @@ -0,0 +1,88 @@ +/* + * 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.model; + +import com.mybatisflex.annotation.Id; +import com.mybatisflex.annotation.Table; + +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ +@Table("sys_role") +public class Role { + + @Id + private Integer roleId; + private String roleKey; + private String roleName; + + public Integer getRoleId() { + return roleId; + } + + public void setRoleId(Integer roleId) { + this.roleId = roleId; + } + + 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 boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Role role = (Role) o; + + if (!Objects.equals(roleId, role.roleId)) return false; + if (!Objects.equals(roleKey, role.roleKey)) return false; + return Objects.equals(roleName, role.roleName); + } + + @Override + public int hashCode() { + int result = roleId != null ? roleId.hashCode() : 0; + result = 31 * result + (roleKey != null ? roleKey.hashCode() : 0); + result = 31 * result + (roleName != null ? roleName.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Role{" + + "roleId=" + roleId + + ", roleKey='" + roleKey + '\'' + + ", roleName='" + roleName + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.java new file mode 100644 index 00000000..df93f45b --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/User.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.model; + +import com.mybatisflex.annotation.Id; +import com.mybatisflex.annotation.Table; + +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ + +@Table("sys_user") +public class User { + + @Id + private Integer userId; + private String userName; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + User user = (User) o; + + if (!Objects.equals(userId, user.userId)) return false; + return Objects.equals(userName, user.userName); + } + + @Override + public int hashCode() { + int result = userId != null ? userId.hashCode() : 0; + result = 31 * result + (userName != null ? userName.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "User{" + + "userId=" + userId + + ", userName='" + userName + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserRole.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserRole.java new file mode 100644 index 00000000..ae2f9b8c --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserRole.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.model; + +import com.mybatisflex.annotation.Table; + +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ +@Table("sys_user_role") +public class UserRole { + + private Integer userId; + private Integer roleId; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getRoleId() { + return roleId; + } + + public void setRoleId(Integer roleId) { + this.roleId = roleId; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserRole userRole = (UserRole) o; + + if (!Objects.equals(userId, userRole.userId)) return false; + return Objects.equals(roleId, userRole.roleId); + } + + @Override + public int hashCode() { + int result = userId != null ? userId.hashCode() : 0; + result = 31 * result + (roleId != null ? roleId.hashCode() : 0); + return result; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO.java new file mode 100644 index 00000000..98c3ce4a --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO.java @@ -0,0 +1,97 @@ +/* + * 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.model; + +import java.util.List; +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ + +public class UserVO { + + private String userId; + private String userName; + private List roleList; + private Role role; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public List getRoleList() { + return roleList; + } + + public void setRoleList(List roleList) { + this.roleList = roleList; + } + + public Role getRole() { + return role; + } + + public void setRole(Role role) { + this.role = role; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserVO userVO = (UserVO) o; + + if (!Objects.equals(userId, userVO.userId)) return false; + if (!Objects.equals(userName, userVO.userName)) return false; + if (!Objects.equals(roleList, userVO.roleList)) return false; + return Objects.equals(role, userVO.role); + } + + @Override + public int hashCode() { + int result = userId != null ? userId.hashCode() : 0; + result = 31 * result + (userName != null ? userName.hashCode() : 0); + result = 31 * result + (roleList != null ? roleList.hashCode() : 0); + result = 31 * result + (role != null ? role.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UserVO{" + + "userId='" + userId + '\'' + + ", userName='" + userName + '\'' + + ", roleList=" + roleList + + ", role=" + role + + '}'; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO1.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO1.java new file mode 100644 index 00000000..5ebd5271 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO1.java @@ -0,0 +1,85 @@ +/* + * 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.model; + +import java.util.List; +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ + +public class UserVO1 { + + private String userId; + private String userName; + private List roleList; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public List getRoleList() { + return roleList; + } + + public void setRoleList(List roleList) { + this.roleList = roleList; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserVO1 userVO1 = (UserVO1) o; + + if (!Objects.equals(userId, userVO1.userId)) return false; + if (!Objects.equals(userName, userVO1.userName)) return false; + return Objects.equals(roleList, userVO1.roleList); + } + + @Override + public int hashCode() { + int result = userId != null ? userId.hashCode() : 0; + result = 31 * result + (userName != null ? userName.hashCode() : 0); + result = 31 * result + (roleList != null ? roleList.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UserVO{" + + "userId='" + userId + '\'' + + ", userName='" + userName + '\'' + + ", roleList=" + roleList + + '}'; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO2.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO2.java new file mode 100644 index 00000000..46682763 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO2.java @@ -0,0 +1,85 @@ +/* + * 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.model; + +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ + +public class UserVO2 { + + private String userId; + private String userName; + private Role role; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + + public Role getRole() { + return role; + } + + public void setRole(Role role) { + this.role = role; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserVO2 userVO = (UserVO2) o; + + if (!Objects.equals(userId, userVO.userId)) return false; + if (!Objects.equals(userName, userVO.userName)) return false; + return Objects.equals(role, userVO.role); + } + + @Override + public int hashCode() { + int result = userId != null ? userId.hashCode() : 0; + result = 31 * result + (userName != null ? userName.hashCode() : 0); + result = 31 * result + (role != null ? role.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UserVO{" + + "userId='" + userId + '\'' + + ", userName='" + userName + '\'' + + ", role=" + role + + '}'; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO3.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO3.java new file mode 100644 index 00000000..6e36df50 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UserVO3.java @@ -0,0 +1,108 @@ +/* + * 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.model; + +import java.util.Objects; + +/** + * @author 王帅 + * @since 2.0 + */ + +public class UserVO3 { + + private String userId; + private String userName; + private Integer roleId; + private String roleKey; + private String roleName; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public Integer getRoleId() { + return roleId; + } + + public void setRoleId(Integer roleId) { + this.roleId = roleId; + } + + 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 boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserVO3 userVO3 = (UserVO3) o; + + if (!Objects.equals(userId, userVO3.userId)) return false; + if (!Objects.equals(userName, userVO3.userName)) return false; + if (!Objects.equals(roleId, userVO3.roleId)) return false; + if (!Objects.equals(roleKey, userVO3.roleKey)) return false; + return Objects.equals(roleName, userVO3.roleName); + } + + @Override + public int hashCode() { + int result = userId != null ? userId.hashCode() : 0; + result = 31 * result + (userName != null ? userName.hashCode() : 0); + result = 31 * result + (roleId != null ? roleId.hashCode() : 0); + result = 31 * result + (roleKey != null ? roleKey.hashCode() : 0); + result = 31 * result + (roleName != null ? roleName.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UserVO3{" + + "userId='" + userId + '\'' + + ", userName='" + userName + '\'' + + ", roleId=" + roleId + + ", roleKey='" + roleKey + '\'' + + ", roleName='" + roleName + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index 7e354699..03ce12f1 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -1,16 +1,17 @@ # DataSource Config spring: - h2: - console: - enabled: true +# h2: +# console: +# enabled: true datasource: - driver-class-name: org.h2.Driver - username: sa - password: - sql: - init: - schema-locations: classpath:schema.sql - data-locations: classpath:data.sql + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true + username: root + password: 12345678 +# sql: +# init: +# schema-locations: classpath:schema.sql +# data-locations: classpath:data.sql mybatis-flex: mapper-locations: - classpath*:/mapper/*.xml diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml index 255f9407..9538ea33 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml @@ -2,6 +2,21 @@ + + + + + + + + + + + + + + +