mybatis-plus返回map自动转驼峰配置

mybatis-plus返回map自动转驼峰配置object-wrapper-factory不生效问题解决;配置map-underscore-to-camel-case: true不生效问题解决

 

很多时候我们工作中查询很多字段的时候一般是返回一个VO来接收,这个时候我们只要在yml中配置了

map-underscore-to-camel-case: true

 

就会自动将查询数据库的字段带下划线的属性转成对应实体类VO中驼峰命名的属性。

但是会经常有这种场景:例如我们只查询2个字段要返回给前端,这时候我们还需要新建一个VO,很是麻烦,我们只需要查询返回一个Map来接收就可以了 ,但是返回到控制台的属性结果却不是驼峰命名。如下图 ,这就是为何你yml中配置了map-underscore-to-camel-case: true也不生效的原因。(对返回map不生效

 

怎么解决这个问题呢?解决方案:
mybatis-plus其实已经帮我们写好了MybatisMapWrapperFactory类(开启返回map结果集的下划线转驼峰)

在mybatis-plus-extension.jar下有一个类com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory和com.baomidou.mybatisplus.extension.handlers.MybatisMapWrapper

 

mybatis-plus自带map下划线转驼峰配置类

重点:
我们只需要在yml中配置一下object-wrapper-factory指定MybatisMapWrapperFactory就可以了

mybatis-plus:  mapper-locations: classpath:mapper/*Mapper.xml
   configuration:    call-setters-on-nulls: true
     map-underscore-to-camel-case: true
     object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory


然后启动项目,我去竟然报错了:

***************************
 
APPLICATION FAILED TO START
 
***************************
 
Description:
 
 

Failed to bind properties under 'mybatis-plus.configuration.object-wrapper-factory' to org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory:
 
Property: mybatis-plus.configuration.object-wrapper-factory
 
Value: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory
 
Origin: class path resource [application.yml]:99:29
 
Reason: No converter found capable of converting from type [java.lang.String] to type [org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory]
 
Action:
 
Update your application's configuration

启动报错详情

 

提示找不到合适的converter将string转化为ObjectWrapperFactory对象。这又是什么鬼呢?

看字面意思,应该是缺少对应的converter,难道mybatis没有提供这个converter吗?简直有点坑。而且springboot也不提供用反射机制来构件对象的converter?是的,springboot没有这样做。通过查资料得知springboot提供了一种扩展机制,允许你来写一个converter来完成你想要的转换工作。于是,我又写了一个converter:

package com.bytedance.douyin.config;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
 
 
@Component
@ConfigurationPropertiesBinding
public class ObjectWrapperFactoryConverter implements Converter {
    @Override    
    public ObjectWrapperFactory convert(String source) {
        try {
            return (ObjectWrapperFactory) Class.forName(source).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}


再次启动 ok不报错了,这时候来看看结果是不是返回map自动转成驼峰命名。果然自动转了

 

返回map自动转驼峰命名

 

第二种方式:如果嫌配置Converter麻烦,不自定义Converter,那就不能在yml中配置object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory

教你第二种方式:直接这样配置就搞定了

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

你可能感兴趣的:(mybatis-plus,java,mysql)