zoukankan      html  css  js  c++  java
  • 06.动态SQL和foreach

    动态sql:

    映射文件代码:

     1  <!-- 动态sql,根据名字和年龄查询,where标签会处理第一个and,其他位置的and不会自动处理 -->
     2   <select id="queryStudentByNameAndAge" parameterType="student" resultMap="student1">
     3     select * from student 
     4     <!-- 1.使用where 1=1 ,避免where后直接跟and -->
     5      <!-- 2.使用where关键字,避免where后直接跟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循环属性集合:

    映射文件:

      <!-- foreach循环属性集合-->
      <select id="queryStudentsByHomeAddress" parameterType="Home" resultMap="student1">
        select * from student 
        <where>
        <if test="homeAddress != null and homeAddress != '' ">
        <!-- colleaction是属性中的集合,item是遍历出来的每个元素,open是循环结果前边的东西,close是循环结果后边要加的东西,separator是循环结果用什么符号分隔 -->
            <foreach collection="homeAddress" item="homeAr" open=" and homeaddress in (" close=" )" separator=",">
                #{homeAr}            
            </foreach>
        </if>    
        </where>
      </select>

    测试类:

    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循环集合:

    映射文件:(需要注意的是循环要循环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>

    测试类:

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

    foreach循环数组:

    映射文件:(注意循环必须都写array,输入参数不是简单类型时都写Object[])

     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>

    测试类:

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

    foreach循环对象数组:

    映射文件:(需要注意的是这时的item要写循环出来的对象了,不能瞎写名字了,然后通过对象点的方式获取值)

     1 <!-- foreach循环对象数组-->
     2   <select id="queryStudentsWithObjectArray" parameterType="Object[]" resultMap="student1">
     3     select * from student 
     4     <where>
     5     <!-- 必须写array -->
     6     <if test="array != null and array != '' ">
     7      <!-- item里边写的是循环出来的对象 -->
     8         <foreach collection="array" item="address" open=" and homeaddress in (" close=" )" separator=",">
     9             <!-- 对象点属性获取值 -->
    10             #{address.homeAddress}            
    11         </foreach>
    12     </if>    
    13     </where>
    14   </select>

    测试类:

    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);
  • 相关阅读:
    L3-1 二叉搜索树的结构 (30 分)
    L3-2 森森快递 (30 分)(贪心+线段树/分块)
    三分(凸函数)
    (三分入门)(凹函数)
    Print Article(斜率DP入门+单调队列)
    PTA 逆散列问题 (30 分)(贪心)
    二叉树遍历相关
    7-5 堆中的路径 (25 分)
    Grouping ZOJ
    D
  • 原文地址:https://www.cnblogs.com/man-tou/p/11343800.html
Copyright © 2011-2022 走看看