06. Dynamic SQL and foreach

Dynamic sql:

Mapped file code:

1   <-! Dynamic sql, query based on name and age, where the label will process the first and, at other locations and does not automatically process -> 
2    < the SELECT the above mentioned id = "queryStudentByNameAndAge" parameterType = "Student" resultMap = " student1 " > 
. 3      SELECT * from Student 
 . 4      <-! 1. use where 1 = 1, where after avoid directly with and -> 
. 5       - <! used where keyword 2, where after avoid directly with and - > 
. 6      < WHERE > 
. 7          < IF Test = "sname! = null and sname! = ''" >
 8             and sname = #{sname}
 9         </if>
10         <if test="age != null and age != '' ">
11             and age = #{age}
12         </if>
13     </where>
14   </select>

foreach loop set of attributes:

Mapping file:

  <-! The foreach loop attribute set -> 
  < SELECT ID = "queryStudentsByHomeAddress" the parameterType = "Home" The resultMap = "student1" > 
    SELECT * from Student 
    < WHERE > 
    < IF Test = "null and the homeAddress the homeAddress = = '!! ' " > 
    <-! colleaction is a collection of attributes, item out each element is traversed, open cycle is the result of something in front, close behind the results of the cycle is to add something, separator result of what is delimited cycle - -> 
        < the foreach Collection = "the homeAddress" Item = "homeAr" Open = "and in homeAddress (" close=" )" separator=",">
            #{homeAr}            
        </foreach>
    </if>    
    </where>
  </select>

Test categories:

1  Home home = new Home();
2  List<String> homeAddress = new ArrayList<String>();
3  homeAddress.add("北京");
4  homeAddress.add("南京");
5  home.setHomeAddress(homeAddress);
6  List<Student> stu = studentMapper.queryStudentsByHomeAddress(home);

foreach loop collections:

Mapping file :( Note that cycle to cycle list)

 1  <!-- foreach循环集合-->
 2   <select id="queryStudentsWithList" parameterType="list" resultMap="student1">
 3     select * from student 
 4     <where>
 5     <!-- 必须写list -->
 6     <if test="list != null and list != '' ">
 7         <foreach collection="list" item="homeAr" open=" and homeaddress in (" close=" )" separator=",">
 8             #{homeAr}            
 9         </foreach>
10     </if>    
11     </where>
12   </select>

Test categories:

1 List<String> list = new ArrayList<String>();
2 list.add("北京");
3 list.add("南京");
4 List<Student> stu = studentMapper.queryStudentsWithList(list);

foreach loop through the array:

Mapping file :( note circulation must write array, write the input parameters is not a simple Object Type [])

 1  <!-- foreach循环数组-->
 2   <select id="queryStudentsWithArray" parameterType="Object[]" resultMap="student1">
 3     select * from student 
 4     <where>
 5     <!-- 必须写array -->
 6     <if test="array != null and array != '' ">
 7         <foreach collection="array" item="homeAr" open=" and homeaddress in (" close=" )" separator=",">
 8             #{homeAr}            
 9         </foreach>
10     </if>    
11     </where>
12   </select>

Test categories:

1 String[] str = {"北京","南京"};
2 List<Student> stu = studentMapper.queryStudentsWithArray(str);

foreach loop array of objects:

:( mapping file should be noted that at this time the item objects to write out the cycle, Xia Xie can not name, and then get the value of an object by way point)

. 1  <-! The foreach loop array of objects -> 
2    < SELECT ID = "queryStudentsWithObjectArray" the parameterType = "Object []" The resultMap = "student1" > 
. 3      SELECT * from Student 
 . 4      < WHERE > 
. 5      <-! Must write array -> 
. 6      < IF Test = "! = null and Array Array = ''!" > 
. 7       <-! Item is written inside the object out of the cycle -> 
. 8          < the foreach Collection = "Array" Item = "address" open=" and homeaddress in (" close=" )" separator=",">
 9             <!-- 对象点属性获取值 -->
10             #{address.homeAddress}            
11         </foreach>
12     </if>    
13     </where>
14   </select>

Test categories:

1 Address addr1 = new Address();
2 addr1.setHomeAddress("北京");
3 Address addr2 = new Address();
4 addr2.setHomeAddress("南京");
5 Address[] address = {addr1,addr2};
6 List<Student> s = studentMapper.queryStudentsWithObjectArray(address);

 

Guess you like

Origin www.cnblogs.com/man-tou/p/11343800.html