<>MyBatis loop Map

I met a special business today , The incoming Map The data is traversed in the mapping file , In previous studies , We also know MyBatis There are default operations on the collection list and array
, But there is no default map , All cannot be written directly collection=“map” , If so , It will be taken as a basis map.get(“map”)
Get pass value only , In most cases, it's a map There will be no “map” this key of , So it's a mistake . If you want to use map
ID to get parameters map, You need to ensure the incoming Map Parameters have @Param(“map”) annotation .

<>1.Mybatis Input parameter Map Use of

<>1.1 transmit Map data

First kind : Self encapsulation and delivery Map Medium KEY value , Analog data
//service: public List<UserDepte> getByMap(Map<String,List<Long>> map){
Map<String,Object> paramsMap= new HashMap<String, Object>();
params.put("paramsMap", map); return this.getByMap(paramsMap); } //dao: int
getByMap(Map<String,Object> map)
Second : Personal recommendation @Param annotation
//service Direct call dao int getByMap(@param("paramsMap") Map<String,Object> map)
<>1.2 Mapping file XML

(1) loop KEY Value writing method :
<foreach collection="paramsMap.keys" item="k" separator="and"> ${k} = #{k}
(2) loop VAVLUE Value writing method
<foreach collection="paramsMap.values" item="v" separator="and"> ${v} = #{v}
In fact, the essence is to use Map.Entiy Properties in object , Review for yourself Java Middle pair Map There are several ways to traverse

<>1.3 KEY obtain VALUE
<foreach collection="paramsMap.keys" item="k" separator="and"> <if test="null
!= paramsMap[k]"> ${k} = ${paramsMap[k]} </if> </foreach>
be careful : Used here $ To get the data , In most cases, we personally do not recommend it $ Symbol

Personal recommendation method : Obtained by one cycle key and value value , Use here # and $ Everything is OK
<foreach collection="paramsMap" index="key" item="value"> #{key} = #{value}
review : # and $ Differences between , I should remember !

<>1.4 Expression writing error

Usually when we set the value , Will take #{} The way , instead of ${} , as follows : Wrong writing
<foreach collection="paramsMap.keys" item="k" separator=","> <if test="null !=
paramsMap[k]"> ${k} = #{paramsMap[k]} </if> </foreach>
Write like this , It's not worth it , ${condition[k]} Available values , but #{condition[k]} The value is not real null, The correct way of writing should be :
<foreach collection="paramsMap.keys" item="k" separator=","> <if test="null !=
paramsMap[k]"> ${k} = #{paramsMap[${k}]} <!-- Please pay attention to this way of writing , Personally, I still like to get it at one time key and value Writing method of
--> </if> </foreach>
<>2.Map Advanced Usage

MyBatis Nested loop map Advanced usage of : Suppose the parameter type is such a type structure : Map map = new HashMap<String,List>
, I want to put it together sql as follows :
select * from Table name where ( (id = 1 and name in (' name of a fictitious monkey with supernatural powers ',' Tang Monk ',' Bajie ') or (id = 2 and
name in (' Liu Bei ',' Fei Zhang ',' Guan Yu ') or (id = 3 and name in (' the other woman ',' Wang Wu ',' bastard ') or …… )
id Corresponding to Map in KEY, and in The data in is Map Medium value
Map<String,List<String>> map = new HashMap<>(); List<String> nameList = new
ArrayList<>(); nameList.add(" name of a fictitious monkey with supernatural powers "); nameList.add(" Tang Monk "); nameList.add(" Bajie ");
map.put("1",nameList); nameList = new ArrayList<>(); nameList.add(" Liu Bei ");
nameList.add(" Fei Zhang "); nameList.add(" Guan Yu "); map.put("2",nameList);
(1) The first way to write : Is through KEY obtain value Form of
SELECT * FROM Table name WHERE <foreach collection="paramsMap.keys" item="key"
open="(" separator="OR" close=")"> (id = #{key} AND name in <foreach collection
= "paramsMap[key]" item="name" separator="," open="(" close=")"> #{name}
</foreach> ) </foreach>
(1) The second way to write : Personal favorite way
SELECT * FROM Table name WHERE <foreach collection="paramsMap" index="key"
item="value" open="(" separator="OR" close=")"> (id = #{key} AND name in
<foreach collection = "value" item="name" separator="," open="(" close=")">
#{name} </foreach> ) </foreach>

©2019-2020 Toolsou All rights reserved,
Android Using wechat in H5 Payment result refresh during payment shock !!C++ Can make a sound ! Basic operation of single linked list C Language explanation Java Implement an epidemic number management system 2021 year 11 World programming language ranking linux upper mysql Invalid command _linux lower mysql The command is useless Java project : Campus dormitory management system (java+jsp+javaweb+mysql+ajax) Wechat applet development project directory linux ubuntu Which version ,Ubuntu Which version is the best ?python Code painting cherry blossoms - How to use Python Draw a beautiful cherry blossom