Case of key value when processing MyBatis return map in Springboot

Source: Internet
Author: User
Tags object object

In order to unify the different database returns the key value case inconsistency question, the special custom objectwrapperfactory to do the unified processing

1, first custom Mapwrapper

/** * 将Map的key全部转换为小写 * */public class MapKeyLowerWrapper extends MapWrapper {    public MapKeyLowerWrapper(MetaObject metaObject, Map<String, Object> map) {        super(metaObject, map);    }    @Override    public String findProperty(String name, boolean useCamelCaseMapping) {        return name==null?"":name.toLowerCase() ;    }}

2, Custom Objectwrapperfactory

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 MapKeyLowerWrapper(metaObject, (Map) object);    }}

3, add mapwrapperfactory configuration in MyBatis configuration

@Bean (name = "Sqlsessionfactory") @ConditionalOnBean (name = "DataSource") public sqlsessionfactory Sqlsessionfactorybean (@Qualifier ("DataSource") DataSource dataSource) {Sqlsessionfactorybean bean = new sqlsession        Factorybean ();        Bean.setobjectwrapperfactory (New Mapwrapperfactory ());        Bean.setdatasource (DataSource);        Add XML directory Resourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();            try {bean.setmapperlocations (resolver.getresources ("Classpath*:com/ultrapower/ioss/**/mapper/**/*.xml"));        return Bean.getobject ();            } catch (Exception e) {e.printstacktrace ();        throw new RuntimeException (e); }} @Bean ("Sqlsessiontemplate") @ConditionalOnBean (name = "Sqlsessionfactory") public sqlsessiontemplate Sqlsessiontemplate (@Qualifier ("Sqlsessionfactory") Sqlsessionfactory sqlsessionfactory) {return new Sq Lsessiontemplate (sqlsessionfactory);} @ConditionalOnBean (name = "DaTasource ") @Bean (name =" TransactionManager ") public Platformtransactionmanager TransactionManager (@Qualifier (" DataSource ") DataSource dataSource) {Datasourcetransactionmanager Datasourcetransactionmanager = new Datasourcetra        Nsactionmanager (DataSource); return Datasourcetransactionmanager;}

Springboot The case of the key value when MyBatis returns a map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.