您好,登錄后才能下訂單哦!
下面給大家介紹下mybatis結(jié)果生成鍵值對(duì)的實(shí)例代碼,具體內(nèi)容如下所示:
在實(shí)際應(yīng)用中我們經(jīng)常會(huì)遇到這樣的情況,需要給下拉框賦值,這個(gè)時(shí)候就需要鍵值對(duì)了,具體使用方法如下
1,在maper.xml文件中定義結(jié)果類型(resultType)定義為hashmap,如下所示
<select id="selectSuperUnitInfo" resultType="hashmap"> SELECT unit_id ,unit_name from unit_info </select>
2,在對(duì)應(yīng)的mapper類中用List<Map<String,String>>來(lái)接受這個(gè)類型,如下所示
public List<Map<String,String>> selectSuperUnitInfo();
其實(shí)最強(qiáng)大的莫過(guò)于resultMap類型了,這個(gè)可以在mapper.xml文件中自定義擴(kuò)展類型,然后在包中添加這個(gè)類型即可,真是強(qiáng)大
下面給大家補(bǔ)充下MyBatis返回Map鍵值對(duì)數(shù)據(jù)
List<Map<String, String>> getMtypeList(); <select id="getMtypeList" resultType="java.util.HashMap"> select code,`name` from jk_control_measure </select>
[DEBUG] 2016-08-29 17:50:09 :==> Executing: select code,`name` from jk_control_measure [DEBUG] 2016-08-29 17:50:09 :==> Parameters: [DEBUG] 2016-08-29 17:50:10 :<== Columns: code, name [DEBUG] 2016-08-29 17:50:10 :<== Row: one, 地面沖洗 [DEBUG] 2016-08-29 17:50:10 :<== Row: two, 邊界圍擋 [DEBUG] 2016-08-29 17:50:10 :<== Row: three, 垃圾覆蓋 [DEBUG] 2016-08-29 17:50:10 :<== Row: four, 裸地覆蓋 [DEBUG] 2016-08-29 17:50:10 :<== Row: five, 灑水降塵 [DEBUG] 2016-08-29 17:50:10 :<== Row: six, 車輛沖洗 [DEBUG] 2016-08-29 17:50:10 :<== Row: seven, 建筑渣土 [DEBUG] 2016-08-29 17:50:10 :<== Row: eight, 車輛冒裝 [DEBUG] 2016-08-29 17:50:10 :<== Row: nine, 揚(yáng)塵覆蓋 [DEBUG] 2016-08-29 17:50:10 :<== Row: ten, 車輛撒漏 [DEBUG] 2016-08-29 17:50:10 :<== Row: eleven, 車輛黑煙 [DEBUG] 2016-08-29 17:50:10 :<== Row: twelve, 道路積塵
[{"NAME":"地面沖洗","name":"地面沖洗","code":"one","CODE":"one"}, {"NAME":"邊界圍擋","name":"邊界圍擋","code":"two","CODE":"two"}, {"NAME":"垃圾覆蓋","name":"垃圾覆蓋","code":"three","CODE":"three"}, {"NAME":"裸地覆蓋","name":"裸地覆蓋","code":"four","CODE":"four"}, {"NAME":"灑水降塵","name":"灑水降塵","code":"five","CODE":"five"}, {"NAME":"車輛沖洗","name":"車輛沖洗","code":"six","CODE":"six"}, {"NAME":"建筑渣土","name":"建筑渣土","code":"seven","CODE":"seven"}, {"NAME":"車輛冒裝","name":"車輛冒裝","code":"eight","CODE":"eight"}, {"NAME":"揚(yáng)塵覆蓋","name":"揚(yáng)塵覆蓋","code":"nine","CODE":"nine"}, {"NAME":"車輛撒漏","name":"車輛撒漏","code":"ten","CODE":"ten"}, {"NAME":"車輛黑煙","name":"車輛黑煙","code":"eleven","CODE":"eleven"}, {"NAME":"道路積塵","name":"道路積塵","code":"twelve","CODE":"twelve"} ]
返回的結(jié)果帶有大寫與小寫的key
如果在sql語(yǔ)句中的列名使用大寫會(huì)怎樣呢?自己測(cè)試一下吧!如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。