【问题标题】:springboot map-underscore-to-camel-case invalidspringboot map-underscore-to-camel-case 无效
【发布时间】:2018-04-12 16:57:37
【问题描述】:

我的spring版本是1.5.2,spring-mybatis-start版本是1.3.2, 我在属性中设置了mybatis.configuration.map-underscore-to-camel-case=true。 但是我返回的 MAP 并没有转换为 Camel 命名

这是我的配置

mybatis.configuration.map-underscore-to-camel-case=true

【问题讨论】:

  • 为什么您认为此设置会影响地图? Mybatis 文档说 Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn.
  • 金额....因为有时候我连表查询都会返回很多列,这个时候不适合javabean的定义,那样会显得很尴尬,所以我想返回一个map,然后把key里面的map转换成Camel命名

标签: java mybatis spring-mybatis


【解决方案1】:

问题已解决,计划如下

public class CustomWrapper extends MapWrapper{

public CustomWrapper(MetaObject metaObject, Map<String, Object> map) {
    super(metaObject, map);
}

// useCamelCaseMapping is map-underscore-to-camel-case field
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
    if(useCamelCaseMapping){
        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,name);
    }
    return name;
}
}

public class MapWrapperFactory implements ObjectWrapperFactory {

@Override
public boolean hasWrapperFor(Object object) {
    return object != null && object instanceof Map;
}

@Override
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
    return new CustomWrapper(metaObject,(Map)object);
}
}

@Configuration
public class MybatisConfig {

@Bean
public ConfigurationCustomizer mybatisConfigurationCustomizer(){
    return new ConfigurationCustomizer() {
        @Override
        public void customize(org.apache.ibatis.session.Configuration configuration) {
            configuration.setObjectWrapperFactory(new MapWrapperFactory());
        }
    };
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 2018-09-17
    • 2018-12-19
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多