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
+ * 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
+ * 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