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