foreach, in of List Array Map in Mybatis

Original source: http://blog.csdn.net/qh_java/article/details/50754271

 

In the configuration file of mybatis, we often use collection arrays and batch queries of maps, so we will often use foreach. First, let's take a look at the properties of foreach:

This picture is very well written, it's good, that's it, you know...

Knowing these properties, let's take a look at the small demo:

1. List<Integer> IntList and List<String> strList collections store basic types

 

[java]  view plain copy  
 
  1. <select id="dynamicForeachTest" parameterType="java.util.List"  resultMap="Users">  
  2.     select id,name from t_blog where id in  
  3.     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
  4.         #{item}  
  5.     </foreach>  
  6. </select>  
The parameter passed in is the List collection, so the value of collection is directly replaced by list and index is the index value of this collection traversal, item is the value of the current index position of the list. And the separator is the separator.

 

2. List<Obect> objList , List<Users> userList reference type data

 

[java]  view plain copy  
 
  1. package soufun.com;  
  2. /** 
  3.  *@author WHD 
  4.  *data February 27, 2016 
  5.  */  
  6. publicclass Users {   
  7. privateint id;   
  8. private String name;  
  9. publicint getId() {   
  10.     return id;  
  11. }  
  12. publicvoid setId(int id) {   
  13.     this.id = id;  
  14. }  
  15. public String getName() {  
  16.     return name;  
  17. }  
  18. publicvoid setName(String name) {   
  19.     this.name = name;  
  20. }  
  21.   
  22. }  
Take this class as an example:

 

 

[java]  view plain copy  
 
  1. <select id="dynamicForeachTest" parameterType="java.util.List" resultMap="Users">  
  2.     select id,name from t_blog where id in  
  3.     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
  4.         #{item.id}  
  5.     </foreach>  
  6. </select>  

 

循环插入

 

[java]  view plain  copy
 
  1. insert into t_blog(id,name) values  
  2. <foreach collection="list" item="item" index="index" separator=",">    
  3.     (#{item.id},#{item.name})  
  4. </foreach>  

 

 

到这里都结束了,但还有个小事说一下,那就是in 的那个遍历,一般我们都是使用foreach 来组装 的但是有看到这样写的:

String  name ="'w1','w2','w3','w4'";

 

[java]  view plain copy  
 
  1. <select id="getByMap" resultMap="Users">  
  2.  SELECT *  FROM t_blog  where  name in (${name})     
  3.  </select>  
This is the string that has been assembled in by itself, and it is the same without using the foreach of mybatis to assemble it.

3. The use of Map, if Map is used as a parameter, the processing method is the same as passing a basic variable and direct reference is ok.

 

[java]  view plain copy  
 
  1. <select id="selectjdnotconfirmorder" parameterType="java.util.Map" resultMap="result">  
  2.         select bzOrderId,jdOrderId,name,mobile,success from jdorderinfo where jdIsOrder = ${notSubmit} and success =${success}  
  3.     </select>  
The parameter type in this sql is Map, and the map parameters are as follows:

 

 

[java]  view plain copy  
 
  1. Map<String,Integer> hashMap= new HashMap<String,Integer>();  
  2.         hashMap.put("notSubmit", notSubmit);  
  3.         hashMap.put("success", state);  

4. The difference between # and $ in mybatis, the variable of # is with single quotation marks and $ is without single quotation marks, for example, the two variables of notSubmit and success above are int type, so instead of # but $, because are numbers instead of characters.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529197&siteId=291194637