解决使用 Mybatis-puls 返回Map类型时数据`非驼峰`

项目场景:

使用 Mybatis-puls 返回Map类型时数据非驼峰


问题描述

使用 Mybatis-Plus 返回Map类型时数据非驼峰,配置map-underscore-to-camel-case: true不生效
有点不符合正常Java规范的驼峰命名法
在这里插入图片描述


原因分析:

经过多方百度,原来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/employee/**/*.xml
  configuration:
    # 映射下划线到驼峰式大小写
    map-underscore-to-camel-case: true
    # 空值调用设置器
    call-setters-on-nulls: true
    object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory

经过测试 , 果然和 百度得到的一样,他报错了~~
在这里插入图片描述
大致意思:无法将“mybatis-plus.configuration.object-wrapper-factory”下的属性绑定到 org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory

按照百度得到的说法,需要使用springboot提供的一种扩展机制,允许你来写一个Converter来完成你想要的转换工作。于是需要写了一个Converter

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<String, ObjectWrapperFactory> {
    
    
    @Override
    public ObjectWrapperFactory convert(String source) {
    
    
        try {
    
    
            return (ObjectWrapperFactory) Class.forName(source).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    
    
            throw new RuntimeException(e);
        }
    }
}

不改变yml配置文件,再次启动,居然真的没有报错,而且Map的空值也没有抛弃(call-setters-on-nulls: true)

在这里插入图片描述
等等还没有结束~~~~~

贴心的大佬提供了另一种简单的方式不用写Converter


第二种方式:不自定义Converter,那就不能在yml中配置

object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory

在配置Mybatis-Plus分页的Config类中直接注入Bean

@Configuration
@EnableTransactionManagement //开启使用事务
public class MybatisPlusConfig {
    
    

    /**
     * 旧版
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
    
    
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
         paginationInterceptor.setOverflow(true);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
         paginationInterceptor.setLimit(1000);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
    
    /**
     * 解决Map映射非驼峰
     * @return
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer(){
    
    
        return new ConfigurationCustomizer() {
    
    
            /**
             * Customize the given a {@link MybatisConfiguration} object.
             *
             * @param configuration the configuration object to customize
             */
            @Override
            public void customize(MybatisConfiguration configuration) {
    
    
                configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
            }
        };
    }
}

至此 就算是解决完毕了~~

猜你喜欢

转载自blog.csdn.net/u013494827/article/details/123810606