mybatis单参数以及多参数集合查询

单参数情况:

(1)array

List<CarnumberAlarm> selectByDeviceIdAndTime(String[] ids);

xml配置:
    <if test="ids != null and ids.length > 0" >
        deviceId in 
        <foreach collection="array" open="(" separator="," close=")" item="item" index="index">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </if>

(2)list

List<CarnumberAlarm> selectByDeviceIdAndTime(List<String> ids);

xml配置:
    <if test="ids != null and ids.size() > 0" >
        deviceId in 
        <foreach collection="list" open="(" separator="," close=")" item="item" index="index">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </if>

(3)map

List<CarnumberAlarm> selectByDeviceIdAndTime(Map<String, Object> params);

xml配置:
    <if test="ids != null and ids.size() > 0" >
        deviceId in 

        <!--collection属性是map.key,其它所有属性都是map.key-->
        <foreach collection="ids" open="(" separator="," close=")" item="item" index="index">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </if>

 

 

多个参数情况下,需要取别名:

(1)array

List<CarnumberAlarm> selectByDeviceIdAndTime(@Param("ids")String[] ids, @Param("startTime")Date startTime, @Param("endTime")Date endTime, @Param("pageIndex")Long pageIndex,@Param("pageSize")Long pageSize);

xml配置:

  <!-- 根据设备id列表以及起止时间分页检索 -->
  <select id="selectByDeviceIdAndTime" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_carnumber_alarm 
    where alarmTime between #{startTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
    <if test="ids != null and ids.length > 0" >
        and deviceId in 
        <foreach collection="ids" open="(" separator="," close=")" item="id" index="index">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </if>
    limit #{pageIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
  </select>

(2)list

List<CarnumberAlarm> selectByDeviceIdAndTime(@Param("ids")List<String> ids, @Param("startTime")Date startTime, @Param("endTime")Date endTime, @Param("pageIndex")Long pageIndex,@Param("pageSize")Long pageSize);

xml配置: 

<!-- 根据设备id列表以及起止时间分页检索 -->
  <select id="selectByDeviceIdAndTime" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_carnumber_alarm 
    where alarmTime between #{startTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
    <if test="ds != null and ids.size() > 0" >
        and deviceId in 
        <foreach collection="ids" open="(" separator="," close=")" item="id" index="index">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </if>
    limit #{pageIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
  </select>

(3)map

List<CarnumberAlarm> selectByDeviceIdAndTime(@Param("params")List<String> params, @Param("startTime")Date startTime, @Param("endTime")Date endTime, @Param("pageIndex")Long pageIndex,@Param("pageSize")Long pageSize);

xml配置:<!-- 根据设备id列表以及起止时间分页检索 -->
  <select id="selectByDeviceIdAndTime" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_carnumber_alarm 
    where alarmTime between #{startTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
    <if test="params!= null and params.size() > 0" >
        and deviceId in 

        <!--params.keys, params.values, params.键名-->
        <foreach collection="params.values" open="(" separator="," close=")" item="id" index="index">
            #{id,jdbcType=VARCHAR}
        </foreach>
    </if>
    limit #{pageIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
  </select>

 

快来成为我的朋友或合作伙伴,一起交流,一起进步!
QQ群:961179337
微信:lixiang6153
邮箱:lixx2048@163.com
公众号:IT技术快餐
更多资料等你来拿!

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贝壳里的沙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值