• 关于Mybatis中传入一个List,字符串数组,或者Map集合作为查询条件的参数


    一.入参为List的写法

    <select id="queryParamList" resultType="map" parameterType="java.util.List">
              select id from static
              where id in 
              <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
                #{item}  
            </foreach>
      </select>

    其中<foreach>这个标签是用来循环传入的集合的,collection="list"这个参数中有list,map两种,还有就是自定义的参数,item="item"这个参数可以自定义,

    用来循环集合里面的值,这个参数的取名要和下面#()这个里面的取名一致。

    parameterType="java.util.List"这个传入的参数类型不能简写成List(其中只有基本数据类型可以简写)。

    当然,如果用in来查询的,可以用一个string来写,如上图列子:将id手动拼接成一个string传入。参照sql语句的规则。

    二.入参为Map的写法

    <selectid="findTeacherByPage"resultMap="supervisorResultMap" parameterType="java.util.Map">
                  select * from teacher  where name= #{name} limit #{start},#{limit}  
    </select>

    注:map中的key值就是name,start,limit。

    三.入参为String数组的写法

    <sql id="condition_sql">     
            <if test=" paymentTypes != null and paymentTypes.size() > 0">
                AND payment_type in
                <foreach collection="paymentTypes" index="index" item="item" open="(" separator="," close=")">
                    #{item}       
                </foreach> 
            </if>
    </sql>
    mapper的接口:  List<Dept> getDeptsByCompanyIds(@Param("companyIds") String[] companyIds);
    
    <select id="getDeptsByCompanyIds" resultMap="Dept">
          select * from t_dept where COMPANY_ID in
          <foreach collection="companyIds" item="id" open="(" close=")" separator=",">
                  #{id}
          </foreach>
    </select>
  • 相关阅读:
    (转载) 天梯赛 L2-018. 多项式A除以B
    天梯赛 L2-007. (并查集) 家庭房产
    天梯赛 L2-013. (并查集) 红色警报
    天梯赛 L2-002. (模拟) 链表去重
    28. Implement strStr() (C++)
    27. Remove Element(C++)
    26. Remove Duplicates from Sorted Array(C++)
    19. Remove Nth Node From End of List(C++)
    21. Merge Two Sorted Lists(C++)
    20. Valid Parentheses(C++)
  • 原文地址:https://www.cnblogs.com/ZJOE80/p/10531789.html
Copyright © 2020-2023  润新知