mybatis中返回多個(gè)map結(jié)果問(wèn)題
mybatis返回多個(gè)map結(jié)果
如果返回一條結(jié)果,xml直接這樣寫(xiě):
<select id="searchncomedateByInvestID" resultMap="java.util.HashMap">
? ? select
? ? t1.invest_id ? ? ? ? ? ? ? ? ? ? ?,
? ? cast(t1.modify_time AS DATE) modify_time
? ? from t_c_wh_redeeminfo t1
? ? where 1=1
? ? and t1.invest_id =#{investId}
</select>dao中:
Map<String,Object> searchncomedateByInvestID(investId);
如果返回多條結(jié)果,xml這樣寫(xiě):
<resultMap id="getAllSetDaysResult" ? type="HashMap">
? ? ? ? <result property="investid" column="invest_id" jdbcType="VARCHAR" />
? ? ? ? <result property="modifytime" column="modify_time" jdbcType="DATE"/>
</resultMap>
<select id="searchncomedateByInvestID" parameterType="java.util.List" resultMap="getAllSetDaysResult">
? ? select
? ? t1.invest_id ? ? ? ? ? ? ? ? ? ? ?,
? ? cast(t1.modify_time AS DATE) modify_time
? ? from t_c_wh_redeeminfo t1
? ? where 1=1
? ? and t1.invest_id in
? ? <foreach collection="list" item="investId" index="index" ?open="(" close=")" separator=",">
? ? ? ? #{investId}
? ? </foreach>
</select>dao中:
List<Map<String, Object>> searchncomedateByInvestID(List<String> preinvestList);
mybatis返回map類型的注意事項(xiàng)及小技巧
項(xiàng)目中為了避免定義java實(shí)體Bean,Mapper中往往會(huì)返回map類型。而返回map類型有幾種定義方式:
1.resultType="java.util.Map"
接口中返回類型為Map<String,Object>;
例如:
<select id="getRoleInfo" resultType="java.util.Map">
? ? ? ? select sr.role_level as roleLevel,
? ? ? ? ? ? ? ?sr.id as roleId,
? ? ? ? ? ? ? ?sr.name as roleName,
? ? ? ? ? ? ? ?sr.code as roleCode
? ? ? ? from fqc_sys_role sr
? ? ? ? left join fqc_sys_user_role sur on sr.id=sur.role_id
? ? ? ? left join fqc_sys_user su on sur.user_id=su.id
? ? ? ? where su.id=#{userId}
? ? </select>
Map<String,Object> getRoleInfo(@Param("userId")Integer id);這種情況Map中的value返回值類型是由mysql數(shù)據(jù)庫(kù)字段類型 jdbcType與javaType的對(duì)應(yīng)關(guān)系決定具體是什么類型,其中role_level 數(shù)據(jù)庫(kù)為int,對(duì)應(yīng)的javaType就是Integer;name,code字段為varchar,對(duì)應(yīng)的javaType就是String。
在調(diào)用接口獲得返回值后還需要轉(zhuǎn)換為對(duì)應(yīng)類型。
2.定義一個(gè)resultMap標(biāo)簽,
在resultMap的type屬性指定type="java.util.Map";
例如:
<resultMap id="roleResultMap" type="java.util.Map"> ?? ?<result property="roleLevel" column="role_level" javaType="java.lang.String"/> ?? ?<result property="roleId" column="id" javaType="java.lang.String"/> ?? ?<result property="roleName" column="name" javaType="java.lang.String"/> ?? ?<result property="roleCode" column="code" javaType="java.lang.String"/> </resultMap>
<select id="getRoleInfo" resultMap="roleResultMap">
? ? ? ? select sr.role_level,
? ? ? ? ? ? ? ?sr.id,
? ? ? ? ? ? ? ?sr.name,
? ? ? ? ? ? ? ?sr.code
? ? ? ? from fqc_sys_role sr
? ? ? ? ? ? ? ? ?left join fqc_sys_user_role sur on sr.id=sur.role_id
? ? ? ? ? ? ? ? ?left join fqc_sys_user su on sur.user_id=su.id
? ? ? ? where su.id=#{userId}
</select>Map<String,String> getRoleInfo(@Param("userId")Integer id);這種情況Map中的value返回值類型在resultMap中通過(guò)javaType指定了,在調(diào)用接口獲得返回值后可以直接使用。
3.返回的Map對(duì)象
第一列數(shù)據(jù)作為key,第二列數(shù)據(jù)作為value,跟列名就沒(méi)關(guān)系了。
需要用到ResultHandler,ResultHandler主要作用是用來(lái)做數(shù)據(jù)轉(zhuǎn)換的,這里不詳細(xì)展開(kāi)。
定義一個(gè)MapResultHandler
public class MapResultHandler<K,V> implements ResultHandler<Map<K,V>> {
? ? private final Map<K,V> mappedResults = new HashMap<>();
?
? ? @Override
? ? public void handleResult(ResultContext context) {
? ? ? ? Map map = (Map) context.getResultObject();
? ? ? ? mappedResults.put((K)map.get("key"), (V)map.get("value"));
? ? }
?
? ? public Map<K,V> getMappedResults() {
? ? ? ? return mappedResults;
? ? }
}mapper中定義的方法設(shè)置返回值為void
public interface AllMedicalRecordStatisticsMapper extends BaseMapper<AllMedicalRecordStatistics> {
?
? ? void queryAllAverageScoreList(@Param("params") List<String> params, MapResultHandler<String,Double> mapResultHandler);
?
? ? void queryFirstRateRecordList(@Param("params") List<String> params, MapResultHandler<String, Double> mapResultHandler);
}service調(diào)用時(shí)new一個(gè)自定義的MapResultHandler
public Map<String, Double> queryAllAverageScoreList(List<String> params) {
?? ?MapResultHandler<String, Double> resultHandler = new MapResultHandler<>();
?? ?allMedicalRecordStatisticsMapper.queryAllAverageScoreList(params,resultHandler);
?? ?return resultHandler.getMappedResults();
}xml中定義一個(gè)對(duì)應(yīng)的resultMap
<resultMap id="mapResultScore" type="java.util.HashMap">
? ? ? ? <result property="key" column="statis_date" jdbcType="VARCHAR"/>
? ? ? ? <result property="value" column="averageScore" jdbcType="DOUBLE" javaType="java.lang.Double"/>
? ? </resultMap>?
?
<select id="queryAllAverageScoreList" resultMap="mapResultScore">
? ? ? ? <foreach collection="params" item="item" index="index" ?separator="UNION ALL" >
? ? ? ? ? ? select DATE_FORMAT(statis_date,'%Y-%m') as statis_date,
? ? ? ? ? ? ? ? ? ?ROUND(IFNULL(record_score/NULLIF(record_count,0),0),2) as averageScore
? ? ? ? ? ? from all_medical_record_statistics
? ? ? ? ? ? where DATE_FORMAT(statis_date,'%Y-%m') = #{item}
? ? ? ? </foreach>
</select>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)列表數(shù)據(jù)導(dǎo)出為Excel文件
這篇文章主要為大家詳細(xì)介紹了在Spring?Boot框架中如何將列表數(shù)據(jù)導(dǎo)出為Excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-02-02
SpringBoot整合Spring?Security過(guò)濾器鏈加載執(zhí)行流程源碼分析(最新推薦)
Spring?Boot?對(duì)于?Spring?Security?提供了自動(dòng)化配置方案,可以使用更少的配置來(lái)使用?Spring?Security,這篇文章主要介紹了SpringBoot整合Spring?Security過(guò)濾器鏈加載執(zhí)行流程源碼分析,需要的朋友可以參考下2023-02-02
Java基于正則表達(dá)式獲取指定HTML標(biāo)簽指定屬性值的方法
這篇文章主要介紹了Java基于正則表達(dá)式獲取指定HTML標(biāo)簽指定屬性值的方法,涉及java基于正則的HTML元素匹配相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
Java編程IP地址和數(shù)字相互轉(zhuǎn)換代碼示例
這篇文章主要介紹了Java編程IP地址和數(shù)字相互轉(zhuǎn)換代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
MAC算法之消息摘要算法HmacMD5的實(shí)現(xiàn)
這篇文章主要介紹了MAC算法之消息摘要算法HmacMD5的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)例,幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下2017-08-08
Spring Security實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄"記住我"功能
登錄過(guò)程中經(jīng)常使用的“記住我”功能,也就是我們經(jīng)常會(huì)在各種網(wǎng)站登陸時(shí)見(jiàn)到的"兩周內(nèi)免登錄",“三天內(nèi)免登錄”的功能。今天小編給大家分享基于Spring Security實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄"記住我"功能,感興趣的朋友一起看看吧2019-11-11

