一.入参为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>

 

转载于:https://www.cnblogs.com/ZJOE80/p/10531789.html

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐