我是靠谱客的博主 悦耳果汁,最近开发中收集的这篇文章主要介绍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技术快餐
更多资料等你来拿!

 

最后

以上就是悦耳果汁为你收集整理的mybatis单参数以及多参数集合查询的全部内容,希望文章能够帮你解决mybatis单参数以及多参数集合查询所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部