From 9e6f4fe357f34bc8e879ea3bc8ca4a786916e4f0 Mon Sep 17 00:00:00 2001 From: ArthurWang Date: Thu, 12 Sep 2024 10:24:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(mf):=20=E5=A2=9E=E5=8A=A0=E6=9C=AA?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=88=97=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=8B=93=E5=B1=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/FlexGlobalConfig.java | 14 ++++++++++++++ .../core/mybatis/FlexDefaultResultSetHandler.java | 14 ++++++++++++++ .../core/mybatis/UnMappedColumnHandler.java | 15 +++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index acd6dc35..f8ccfd7f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -23,6 +23,7 @@ import com.mybatisflex.annotation.UpdateListener; import com.mybatisflex.core.datasource.FlexDataSource; import com.mybatisflex.core.dialect.DbType; import com.mybatisflex.core.exception.FlexAssert; +import com.mybatisflex.core.mybatis.UnMappedColumnHandler; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; @@ -109,6 +110,11 @@ public class FlexGlobalConfig { */ private String versionColumn; + /** + * 未匹配列处理器 + */ + private static UnMappedColumnHandler unMappedColumnHandler; + public boolean isPrintBanner() { return printBanner; } @@ -323,6 +329,14 @@ public class FlexGlobalConfig { this.versionColumn = versionColumn; } + public static UnMappedColumnHandler getUnMappedColumnHandler() { + return unMappedColumnHandler; + } + + public void setUnMappedColumnHandler(UnMappedColumnHandler unMappedColumnHandler) { + this.unMappedColumnHandler = unMappedColumnHandler; + } + public FlexDataSource getDataSource() { return (FlexDataSource) getConfiguration().getEnvironment().getDataSource(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexDefaultResultSetHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexDefaultResultSetHandler.java index a182b9ef..da08d0ec 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexDefaultResultSetHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexDefaultResultSetHandler.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.mybatis; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.util.MapUtil; import org.apache.ibatis.annotations.AutomapConstructor; import org.apache.ibatis.annotations.Param; @@ -42,6 +43,7 @@ import org.apache.ibatis.session.*; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; +import org.apache.ibatis.type.UnknownTypeHandler; import java.lang.reflect.Constructor; import java.lang.reflect.Executable; @@ -592,6 +594,18 @@ public class FlexDefaultResultSetHandler extends DefaultResultSetHandler { } } } + else { + if (FlexGlobalConfig.getUnMappedColumnHandler() != null){ + // 增加未匹配列自定义处理 + final List unmappedColumnNames = rsw.getUnmappedColumnNames(resultMap, columnPrefix); + for (String unmappedColumnName : unmappedColumnNames) { + // 不明确类型,直接取object + final Object value = typeHandlerRegistry.getMappingTypeHandler(UnknownTypeHandler.class).getResult(rsw.getResultSet(), unmappedColumnName); + // 自定义处理未匹配列 + FlexGlobalConfig.getUnMappedColumnHandler().handleUnMappedColumn(metaObject, unmappedColumnName, value); + } + } + } return foundValues; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java new file mode 100644 index 00000000..113bfc2b --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java @@ -0,0 +1,15 @@ +package com.mybatisflex.core.mybatis; + +import org.apache.ibatis.reflection.MetaObject; + +/** + * UnMappedColumnHandler + * 自定义未匹配列处理 + * @author ArthurWang + * @version 1.0 + * @date 2024/9/12 9:16 + **/ +public interface UnMappedColumnHandler { + + void handleUnMappedColumn(MetaObject metaObject, String unmappedColumnName, Object value); +} From 23d50e898ca5acbab4cff4c29ffb7bcacf66d174 Mon Sep 17 00:00:00 2001 From: ArthurWang Date: Thu, 12 Sep 2024 14:39:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(mf):=20=E5=A2=9E=E5=8A=A0=E6=9C=AA?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=88=97=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=8B=93=E5=B1=95=E6=8E=A5=E5=8F=A3=20,?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/mybatis/UnMappedColumnHandler.java | 15 +++ .../test/MyConfigurationCustomizer.java | 2 + .../test/model/UnmappedBaseEntity.java | 35 +++++++ .../mybatisflex/test/model/UnmappedUser.java | 53 +++++++++++ .../unmapped/MyUnMappedColumnHandler.java | 46 +++++++++ .../src/main/resources/unmapped_user.sql | 21 ++++ .../test/mapper/UnmappedUserMapperTest.java | 95 +++++++++++++++++++ 7 files changed, 267 insertions(+) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedBaseEntity.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedUser.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/unmapped/MyUnMappedColumnHandler.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/unmapped_user.sql create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UnmappedUserMapperTest.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java index 113bfc2b..a47b91c6 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/UnMappedColumnHandler.java @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2022-2025, 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; import org.apache.ibatis.reflection.MetaObject; diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/MyConfigurationCustomizer.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/MyConfigurationCustomizer.java index 802282e1..b5d7cdf0 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/MyConfigurationCustomizer.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/MyConfigurationCustomizer.java @@ -23,6 +23,7 @@ import com.mybatisflex.core.datasource.DataSourceProperty; import com.mybatisflex.core.mybatis.FlexConfiguration; import com.mybatisflex.spring.boot.ConfigurationCustomizer; import com.mybatisflex.spring.boot.MyBatisFlexCustomizer; +import com.mybatisflex.test.unmapped.MyUnMappedColumnHandler; import org.apache.ibatis.logging.stdout.StdOutImpl; import org.springframework.context.annotation.Configuration; @@ -55,6 +56,7 @@ public class MyConfigurationCustomizer implements ConfigurationCustomizer, MyBat return value; }; DataSourceManager.setDecipher(decipher); + globalConfig.setUnMappedColumnHandler(new MyUnMappedColumnHandler()); } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedBaseEntity.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedBaseEntity.java new file mode 100644 index 00000000..6dd27c42 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedBaseEntity.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022-2025, 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 lombok.Getter; +import lombok.Setter; + +import java.util.Map; + +/** + * UnMappedBaseEntity + * + * @author wy + * @version 1.0 + * @date 2024/9/12 11:36 + **/ +@Getter +@Setter +public class UnmappedBaseEntity { + + protected Map unmappedMap; +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedUser.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedUser.java new file mode 100644 index 00000000..b5f33c97 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/UnmappedUser.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022-2025, 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.KeyType; +import com.mybatisflex.annotation.Table; +import lombok.Getter; +import lombok.Setter; +import org.springframework.util.CollectionUtils; + +/** + * UnMapped + * + * @author wy + * @version 1.0 + * @date 2024/9/12 11:28 + **/ +@Getter +@Setter +@Table("tb_unmapped_user") +public class UnmappedUser extends UnmappedBaseEntity { + + @Id(keyType = KeyType.Auto) + private Long id; + + private Integer age; + + private String name; + + @Override + public String toString() { + return "UnmappedUser{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + (CollectionUtils.isEmpty(unmappedMap) ? "" : ", unmappedMap=" + unmappedMap.toString()) + + '}'; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/unmapped/MyUnMappedColumnHandler.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/unmapped/MyUnMappedColumnHandler.java new file mode 100644 index 00000000..09f782be --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/unmapped/MyUnMappedColumnHandler.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022-2025, 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.unmapped; + +import com.mybatisflex.core.mybatis.UnMappedColumnHandler; +import com.mybatisflex.test.model.UnmappedBaseEntity; +import org.apache.ibatis.reflection.MetaObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * MyUnMappedColumnHandler + * + * @author wy + * @version 1.0 + * @date 2024/9/12 11:34 + **/ +public class MyUnMappedColumnHandler implements UnMappedColumnHandler { + @Override + public void handleUnMappedColumn(MetaObject metaObject, String unmappedColumnName, Object value) { + if (metaObject.getOriginalObject() instanceof UnmappedBaseEntity){ + Object object = metaObject.getValue("unmappedMap"); + if(object == null){ + Map map = new HashMap<>(); + map.put(unmappedColumnName, value); + metaObject.setValue("unmappedMap", map); + }else { + ((Map)object).put(unmappedColumnName, value); + } + } + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/unmapped_user.sql b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/unmapped_user.sql new file mode 100644 index 00000000..1749d75c --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/unmapped_user.sql @@ -0,0 +1,21 @@ +-- ---------------------------- +-- Table structure for tb_unmapped_user +-- ---------------------------- +DROP TABLE IF EXISTS `tb_unmapped_user`; +CREATE TABLE `tb_unmapped_user` +( + `id` bigint primary key AUTO_INCREMENT, + `name` varchar(100) NULL DEFAULT NULL, + `age` int NULL DEFAULT NULL, + `code` int NULL DEFAULT NULL +) + +-- ---------------------------- +-- Records of tb_unmapped_user +-- ---------------------------- +INSERT INTO `tb_unmapped_user` +VALUES (1, '张三', 28, 64); +INSERT INTO `tb_unmapped_user` +VALUES (2, '李四', 15, 128); +INSERT INTO `tb_unmapped_user` +VALUES (3, '王五', 9, 256); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UnmappedUserMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UnmappedUserMapperTest.java new file mode 100644 index 00000000..ef2728fa --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/UnmappedUserMapperTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2022-2025, 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.mybatisflex.core.query.QueryColumn; +import com.mybatisflex.core.query.QueryMethods; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.test.model.UnmappedUser; +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.test.model.table.UnmappedUserTableDef.UNMAPPED_USER; + +/** + * UnMappedUserMapperTest + * + * @author wy + * @version 1.0 + * @date 2024/9/12 11:39 + **/ +@SpringBootTest +@SuppressWarnings("all") +public class UnmappedUserMapperTest { + + @Autowired + private UnmappedUserMapper unmappedUserMapper; + + /** + * 额外字段查询,数据库中有,但是实体类中没有 + * 应用:前端具有一定查询数据库能力时,不改后端代码情况下,返回新增字段数据 + */ + @Test + void testExtraColumn() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(UNMAPPED_USER.ID, UNMAPPED_USER.AGE, UNMAPPED_USER.NAME, + // 额外字段查询 + new QueryColumn("code")) + .where(UNMAPPED_USER.ID.in(1, 2, 3)); + List unmappedUserList = unmappedUserMapper.selectListByQuery(queryWrapper); + System.out.println(unmappedUserList); + } + + /** + * 同名字段 + * 多数据源下同表同名字段不同含义或者需要同时展示 + */ + @Test + void testSameColumn() { + // 可能多数据源下会存在同表同名字段不同值 + QueryWrapper queryWrapper = QueryWrapper.create() + .select(UNMAPPED_USER.ID, UNMAPPED_USER.AGE, UNMAPPED_USER.NAME, + // 同名字段重置 + UNMAPPED_USER.NAME.as("ext_name")) + .where(UNMAPPED_USER.ID.in(1, 2, 3)); + List unmappedUserList = unmappedUserMapper.selectListByQuery(queryWrapper); + System.out.println(unmappedUserList); + } + + /** + * 计算或者处理的字段 + * sql中进行处理的字段,不直接映射到实体类上的域 + */ + @Test + void testCalColumn() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(UNMAPPED_USER.ID, UNMAPPED_USER.NAME, UNMAPPED_USER.AGE, + // 字段计算结果 + QueryMethods.case_() + .when(UNMAPPED_USER.AGE.ge(18)).then("adult") + .when(UNMAPPED_USER.AGE.le(14)).then("child") + .else_("juvenile") + .end() + .as("age_group")) + .where(UNMAPPED_USER.ID.in(1, 2, 3)); + List unmappedUserList = unmappedUserMapper.selectListByQuery(queryWrapper); + System.out.println(unmappedUserList); + } + +}