1、第一步,先定义一个自定义的Map对象,其继承HashMap并且实现Map接口,然后重写其put方法,其意义就是将put方法里面的key中带有下划线的字段给自动转为驼峰格式。这里的弊端就是整个项目当中所有的Map对象再put一个key的时候,如果带有下划线都会自动转为驼峰,所以这块一定要根据你的实际项目来
/**
*
*/
package com.letsun.framework.core.domain;
import java.util.HashMap;
import com.google.common.base.CaseFormat;
/**
* @author chen.kai
*
*/
@SuppressWarnings("serial")
public class Map<K, V> extends HashMap<K, V> implements java.util.Map<K, V>{
@SuppressWarnings("unchecked")
@Override
public V put(K key, V value) {
if (key instanceof String) {
String myKey = (String)key;
if (myKey.contains("_")) {
return super.put((K)CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, myKey), value);
}
}
return super.put(key, value);
}
}
2、mapper.xml的写法:第一个parameterType里面的Map参数是java.util.Map。返回的resultType的map对象是我们自定义的Map对象这里一定要注意。
<select id="selectByNames" parameterType="Map" resultType="com.letsun.framework.core.domain.Map">
select <include refid="BaseColumnComplex"/>,B.product_name,B.product_num,B.model
from shop_service_rework_record A
left join centre_product B on A.product_id = B.id and B.deleted=0
</select>
3、总结:该方式弊端较大,请谨慎使用,如果你项目当中用Map保存了一些常量或者静态变量就请谨慎使用