Home > Java > javaTutorial > body text

Example code for Mybatis result generation of key-value pairs

黄舟
Release: 2017-03-07 10:13:55
Original
1565 people have browsed it

This article mainly introduces the example code of Mybatis result generation key-value pair, and the implementation method of MyBatis returning Map key-value pair data. It is very good and has reference value. Friends who need it can refer to it

The following will introduce to you the example code for generating key-value pairs from mybatis results. The specific content is as follows:

In practical applications, we often encounter such situations where we need to assign values ​​to the drop-down boxes. At this time, The key value needs to be correct. The specific usage method is as follows

1. Define the result type (resultType) in the maper.xml file as hashmap, as shown below

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

2, use List> in the corresponding mapper class to accept this type, as shown below

public List<Map<String,String>> selectSuperUnitInfo();
Copy after login

In fact, the most powerful thing is the resultMap type. You can customize the extension type in the mapper.xml file and then add this type to the package. It is really powerful.

Let me add that MyBatis returns Map key-value pair data

List<Map<String, String>> getMtypeList();
<select id="getMtypeList" resultType="java.util.HashMap">
  select code,`name` from jk_control_measure
</select>
Copy after login

[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, 道路积尘
Copy after login

[{"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"}
]
Copy after login

The returned result has uppercase and lowercase keys

If the column name in the sql statement is used What happens if it is capitalized? Test it yourself! If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the Script House website!

The above is the content of the example code for Mybatis result generation of key-value pairs. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template