From 465fbde56c8077e7c64fa9d500002f4bb11710e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 23 Aug 2023 15:02:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BF=94=E5=9B=9E=20m?= =?UTF-8?q?ap=20=E6=97=B6=EF=BC=8C=E9=85=8D=E7=BD=AE=20map-underscore-to-c?= =?UTF-8?q?amel-case=20=E4=B8=8D=E8=B5=B7=E4=BD=9C=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/mybatis/FlexConfiguration.java | 5 ++- .../FlexWrapperFactory.java} | 34 ++++++++++++++++--- .../test/controller/AccountController.java | 9 +++++ .../test/mapper/MyAccountMapper.java | 5 +++ .../src/main/resources/application.yml | 2 +- 5 files changed, 46 insertions(+), 9 deletions(-) rename mybatis-flex-core/src/main/java/com/mybatisflex/core/{table/EntityWrapperFactory.java => mybatis/FlexWrapperFactory.java} (63%) 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 f6a78b3c..f7c629d1 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 @@ -24,7 +24,6 @@ import com.mybatisflex.core.mybatis.executor.FlexBatchExecutor; import com.mybatisflex.core.mybatis.executor.FlexReuseExecutor; import com.mybatisflex.core.mybatis.executor.FlexSimpleExecutor; 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.StringUtil; @@ -58,13 +57,13 @@ public class FlexConfiguration extends Configuration { public FlexConfiguration(Environment environment) { super(environment); setMapUnderscoreToCamelCase(true); - setObjectWrapperFactory(new EntityWrapperFactory()); + setObjectWrapperFactory(new FlexWrapperFactory()); initDefaultMappers(); } public FlexConfiguration() { setMapUnderscoreToCamelCase(true); - setObjectWrapperFactory(new EntityWrapperFactory()); + setObjectWrapperFactory(new FlexWrapperFactory()); initDefaultMappers(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/EntityWrapperFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java similarity index 63% rename from mybatis-flex-core/src/main/java/com/mybatisflex/core/table/EntityWrapperFactory.java rename to mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java index 6198fdf4..bcac90eb 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/EntityWrapperFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexWrapperFactory.java @@ -13,32 +13,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mybatisflex.core.table; +package com.mybatisflex.core.mybatis; +import com.mybatisflex.core.table.TableInfo; +import com.mybatisflex.core.table.TableInfoFactory; +import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.property.PropertyTokenizer; import org.apache.ibatis.reflection.wrapper.BeanWrapper; +import org.apache.ibatis.reflection.wrapper.MapWrapper; import org.apache.ibatis.reflection.wrapper.ObjectWrapper; import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; import java.util.Collection; import java.util.Map; -public class EntityWrapperFactory implements ObjectWrapperFactory { +/** + * @author michael + */ +public class FlexWrapperFactory implements ObjectWrapperFactory { @Override public boolean hasWrapperFor(Object object) { Class objectClass = object.getClass(); - if (Map.class.isAssignableFrom(objectClass) || - Collection.class.isAssignableFrom(objectClass)) { + if (Collection.class.isAssignableFrom(objectClass)) { return false; + } else if (Map.class.isAssignableFrom(objectClass)) { + return true; } return TableInfoFactory.ofEntityClass(objectClass) != null; } @Override public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) { - return new FlexBeanWrapper(metaObject, object); + if (Map.class.isAssignableFrom(object.getClass())) { + return new FlexMapWrapper(metaObject, (Map) object); + } else { + return new FlexBeanWrapper(metaObject, object); + } } static class FlexBeanWrapper extends BeanWrapper { @@ -57,7 +69,19 @@ public class EntityWrapperFactory implements ObjectWrapperFactory { Object v = tableInfo.invokeOnSetListener(entity, prop.getName(), value); super.set(prop, v); } + } + + static class FlexMapWrapper extends MapWrapper { + + public FlexMapWrapper(MetaObject metaObject, Map map) { + super(metaObject, map); + } + + @Override + public String findProperty(String name, boolean useCamelCaseMapping) { + return useCamelCaseMapping ? StringUtil.underlineToCamel(name) : name; + } } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index 294e7699..20ed3ae2 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; +import java.util.Map; @RestController @UseDataSource("ds1") @@ -152,4 +153,12 @@ public class AccountController { return "ok"; } + + + @GetMapping("/map/{id}") +// @Transactional + public Map map(@PathVariable("id") Long id) { + return myAccountMapper.selectMapById(id); + } + } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java index 7d867b01..0145ac5e 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java @@ -20,6 +20,8 @@ import com.mybatisflex.test.model.Account; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; +import java.util.Map; + public interface MyAccountMapper extends AccountMapper { @@ -28,4 +30,7 @@ public interface MyAccountMapper extends AccountMapper { @Select("select * from tb_account where id = #{id} and id =#{id}") Account selectById(@Param("id") Object id); + @Select("select * from tb_account where id = #{id}") + Map selectMapById(@Param("id") Object id); + } 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 518df747..a3e0f77e 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 @@ -7,7 +7,7 @@ spring: # driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/flex_test username: root - password: 12345678 + password: 123456 # driver-class-name: # datasource: # driver-class-name: org.h2.Driver