The Mybatis result generates the instance code for the key value pair

  • 2020-06-03 06:38:30
  • OfStack

Here is an example code of mybatis result generating key-value pairs, as shown below:

In practical applications, we often encounter such a situation that we need to assign a value to the drop-down box. In this case, we need to correct the key value. The specific usage 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>

2. Use List in the corresponding mapper class < Map < String,String > > To accept this type, as shown below


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

The most powerful is the resultMap type, which allows you to customize the extension type in the mapper.xml file and then add the type to the package

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>

[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,  Ground flush 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: two,  Boundary WeiDang 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: three,  Garbage covered 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: four,  Bare land cover 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: five,  Water spraying dust 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: six,  Car wash 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: seven,  Construction waste residue 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: eight,  Vehicles at loading 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: nine,  Dust cover 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: ten,  Vehicles and leakage 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: eleven,  Black smoke vehicle 
[DEBUG] 2016-08-29 17:50:10 :<==    Row: twelve,  Road dust 

[{"NAME":" Ground flush ","name":" Ground flush ","code":"one","CODE":"one"},
{"NAME":" Boundary WeiDang ","name":" Boundary WeiDang ","code":"two","CODE":"two"},
{"NAME":" Garbage covered ","name":" Garbage covered ","code":"three","CODE":"three"},
{"NAME":" Bare land cover ","name":" Bare land cover ","code":"four","CODE":"four"},
{"NAME":" Water spraying dust ","name":" Water spraying dust ","code":"five","CODE":"five"},
{"NAME":" Car wash ","name":" Car wash ","code":"six","CODE":"six"},
{"NAME":" Construction waste residue ","name":" Construction waste residue ","code":"seven","CODE":"seven"},
{"NAME":" Vehicles at loading ","name":" Vehicles at loading ","code":"eight","CODE":"eight"},
{"NAME":" Dust cover ","name":" Dust cover ","code":"nine","CODE":"nine"},
{"NAME":" Vehicles and leakage ","name":" Vehicles and leakage ","code":"ten","CODE":"ten"},
{"NAME":" Black smoke vehicle ","name":" Black smoke vehicle ","code":"eleven","CODE":"eleven"},
{"NAME":" Road dust ","name":" Road dust ","code":"twelve","CODE":"twelve"}
]

The result is returned with key in uppercase and lowercase

What if column names in sql statements are capitalized? Test 1 yourself! If you have any questions please leave a message to me, this site will respond to you in a timely manner. Thank you very much for your support!


Related articles: