<resultMap id ="UserInfoMap" type="com.example.mybaitsxml.dao.entity.User"> <result column="name_" property="name"/> <result column="sex" property="sex"/> <result column="age" property="age"/> <result column="class_no" property="classNo"/> </resultMap> <resultMap id ="UserInfoMap" type="map"> <result column="name_" property="name"/> <result column="sex" property="sex"/> <result column="age" property="age"/> <result column="class_no" property="classNo"/> </resultMap> <select id="find" parameterType="map' resultType="UserInfoMap"> </select>
第二種:
application配置:
map-underscore-to-camel-case: true
mybatis-plus: mapper-locations: classpath*:org/jeecg/modules/**/xml/*Mapper.xml global-config: # 關閉MP3.0自帶的banner banner: false db-config: #主鍵類型 0:"數據庫ID自增",1:"該類型為未設置主鍵類型", 2:"用戶輸入ID",3:"全局唯一ID (數字類型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)"; id-type: 4 # 默認數據庫表下划線命名 table-underline: true configuration: #返回Map的時候,將Map內的Key轉換為駝峰的命名表達式 map-underscore-to-camel-case: true # 這個配置會將執行的sql打印出來,在開發或測試的時候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
新建工具類:
package org.jeecg.config.mybatis; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.wrapper.MapWrapper; import org.apache.ibatis.reflection.wrapper.ObjectWrapper; import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Map; @Configuration public class MybatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setObjectWrapperFactory(new MapWrapperFactory()); } static 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 MyMapWrapper(metaObject, (Map) object); } } static class MyMapWrapper extends MapWrapper { MyMapWrapper(MetaObject metaObject, Map<String, Object> map) { super(metaObject, map); } @Override public String findProperty(String name, boolean useCamelCaseMapping) { if (useCamelCaseMapping && ((name.charAt(0) >= 'A' && name.charAt(0) <= 'Z') || name.contains("_"))) { return underlineToCamelhump(name); } return name; } /** * 將下划線風格替換為駝峰風格 * * @param inputString * @return */ private String underlineToCamelhump(String inputString) { StringBuilder sb = new StringBuilder(); boolean nextUpperCase = false; for (int i = 0; i < inputString.length(); i++) { char c = inputString.charAt(i); if (c == '_') { if (sb.length() > 0) { nextUpperCase = true; } } else { if (nextUpperCase) { sb.append(Character.toUpperCase(c)); nextUpperCase = false; } else { sb.append(Character.toLowerCase(c)); } } } return sb.toString(); } } }