apache camel 相关配置_MyBatis-Plus返回map自动转驼峰配置object-wrapper-factory
## 問題場景:
很多時候我們工作中查詢很多字段的時候一般是返回一個VO來接收,這個時候我們只要在yml中配置了
map-underscore-to-camel-case: true就會自動將查詢數(shù)據(jù)庫的字段帶下劃線的屬性轉(zhuǎn)成對應實體類VO中駝峰命名的屬性。
但是會經(jīng)常有這種場景:例如我們只查詢2個字段要返回給前端,這時候我們還需要新建一個VO,很是麻煩,我們只需要查詢返回一個Map來接收就可以了 ,但是返回到控制臺的屬性結果卻不是駝峰命名。如下圖 ,這就是為何你yml中配置了map-underscore-to-camel-case: true也不生效的原因。(對返回map不生效)
查詢返回map屬性不是駝峰命名
怎么解決這個問題呢?解決方案:
mybatis-plus其實已經(jīng)幫我們寫好了MybatisMapWrapperFactory類(開啟返回map結果集的下劃線轉(zhuǎn)駝峰)
在mybatis-plus-extension.jar下有一個類com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory和com.baomidou.mybatisplus.extension.handlers.MybatisMapWrapper
mybatis-plus自帶map下劃線轉(zhuǎn)駝峰配置類
重點:
我們只需要在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轉(zhuǎn)化為ObjectWrapperFactory對象。這又是什么鬼呢?
看字面意思,應該是缺少對應的converter,難道m(xù)ybatis沒有提供這個converter嗎?簡直有點坑。而且springboot也不提供用反射機制來構件對象的converter?是的,springboot沒有這樣做。通過查資料得知springboot提供了一種擴展機制,允許你來寫一個converter來完成你想要的轉(zhuǎn)換工作。于是,我又寫了一個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@ConfigurationPropertiesBindingpublic 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自動轉(zhuǎn)成駝峰命名。果然自動轉(zhuǎn)了
返回map自動轉(zhuǎn)駝峰命名
第二種方式:如果嫌配置Converter麻煩,不自定義Converter,那就不能在yml中配置object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory
教你第二種方式:直接這樣配置就搞定了
@Beanpublic ConfigurationCustomizer mybatisConfigurationCustomizer(){ return new ConfigurationCustomizer() { @Override public void customize(org.apache.ibatis.session.Configuration configuration) { configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory()); } };}總結
以上是生活随笔為你收集整理的apache camel 相关配置_MyBatis-Plus返回map自动转驼峰配置object-wrapper-factory的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue点击按钮之后置成不可用_2020.
- 下一篇: 延时消息_手把手实现一条延时消息