温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何将mybatis-plus返回map的值转为驼峰命名法

发布时间:2020-11-30 16:30:34 来源:亿速云 阅读:2194 作者:Leah 栏目:开发技术

这篇文章给大家介绍如何将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<String,ObjectWrapperFactory> {
  @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返回map的值转为驼峰命名法

补充知识:解决spring boot整合mybatis时 返回map value为空字段不显示

这两天公司从YMP框架换到了spring boot   在整合mybatis时多表联查,返回map的时候,发现map里面的value是空的情况下

字段也不显示了 导致页面取值报错,如下图

如何将mybatis-plus返回map的值转为驼峰命名法

上网查了一下,在yml文件中加入一个配置就可以了

mybatis:
 configuration:
  call-setters-on-nulls: true

关于如何将mybatis-plus返回map的值转为驼峰命名法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI