首頁  >  文章  >  Java  >  Mybatis結果產生鍵值對

Mybatis結果產生鍵值對

高洛峰
高洛峰原創
2017-02-27 16:21:191813瀏覽

下面要跟大家介紹下mybatis結果產生鍵值對的實例程式碼,具體內容如下所示:

在實際應用程式中我們常常會遇到這樣的情況,需要給下拉框賦值,這個時候就需要鍵值對了,具體使用方法如下

1,在maper.xml檔中定義結果型別(resultType)定義為hashmap,如下所示

#
<select id="selectSuperUnitInfo" resultType="hashmap">
  SELECT unit_id ,unit_name from unit_info
 </select>

2,在對應的mapper類別中用Listb9ce0bbc1c208c036cee4db71a23ea6a>來接受這個類型,如下所示

public List<Map<String,String>> selectSuperUnitInfo();

其實最強大的莫過於resultMap類型了,這個可以在mapper.xml檔案中自訂擴充類型,然後在套件中加入這個類型即可,真是強大

下面給大家補充下MyBatis回傳Map鍵值對資料

##

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, 扬尘覆盖
[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":"扬尘覆盖","name":"扬尘覆盖","code":"nine","CODE":"nine"},
{"NAME":"车辆撒漏","name":"车辆撒漏","code":"ten","CODE":"ten"},
{"NAME":"车辆黑烟","name":"车辆黑烟","code":"eleven","CODE":"eleven"},
{"NAME":"道路积尘","name":"道路积尘","code":"twelve","CODE":"twelve"}
]

#傳回的結果帶有大寫與小寫的key

如果在sql語句中的列名使用大寫會怎麼樣呢?自己測試一下吧!如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對PHP中文網的支持!

更多Mybatis結果產生鍵值對相關文章請關注PHP中文網!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn