Mybatis返回map自动转驼峰

配置

MapWrapper.java

import com.google.common.base.CaseFormat;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;

import java.util.Map;

public class CustomWrapper extends MapWrapper {


    public CustomWrapper(MetaObject metaObject, Map map) {
        super(metaObject, map);
    }

    @Override
    public String findProperty(String name, boolean useCamelCaseMapping) {
        if(useCamelCaseMapping){
           return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,name);
        }
        return name;
    }
}
ObjectWrapperFactory.java

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;

import java.util.Map;

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 CustomWrapper(metaObject,(Map)object);
    }
}
mybatis-config.xml


<configuration>
    <settings>
        <setting name="callSettersOnNulls" value="true"/>
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
configuration>

 <bean id="lotterySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
        <property name="mapperLocations" value>classpath:mapping/*.xml property>
        
        <property name="objectWrapperFactory" ref="mapWrapperFactory" />
    bean>

配置完成,跑一遍看效果!!!

你可能感兴趣的:(java开发)