我使用的时候collection值为mapper的参数名
如:int deleteRoleByUserIds(@Param("userIds") String[] userIds);
<delete id="deleteRoleByUserIds">
delete from uc_user_role where user_id in
<foreach item="item" index="index" collection="userIds"
open="(" separator="," close=")">
#{item}
</foreach>
</delete>
====================================================
1. 当查询的参数只有一个时
  findByIds(List<Long> ids)
 1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
 <select id="findByIdsMap" resultMap="BaseResultMap">
Select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>

 findByIds(Long[] ids)
 1.b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
  <select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="array"
open="(" separator="," close=")">
#{item}
</foreach>
</select>

2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
 这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
         下面是一个示例
         Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", name);
params.put("ids", ids);
mapper.findByIdsMap(params); <select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
 
完整的示例如下:
例如有一个查询功能,Mapper接口文件定义如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查询的sql拼装方法如下:
 <select id="findbyIds" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="array"
open="(" separator="," close=")">
#{item}
</foreach>
</select>

mybatis中使用where in查询时的注意事项的更多相关文章

  1. mybatis中参数为list集合时使用 mybatis in查询

    mybatis中参数为list集合时使用 mybatis in查询 一.问题描述mybatis sql查询时,若遇到多个条件匹配一个字段,sql 如: select * from user where ...

  2. Mybatis使用MySQL进行模糊查询时输入中文检索不到结果

    Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数   ?useUnicode=true&characterEncoding=UTF-8

  3. 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱

    警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱 以下文章来源:https://blog.csdn.net/defonds/article/details/4 ...

  4. MyBatis 中两表关联查询MYSQL (14)

    MyBatis 中两表关联查询MYSQL 1.创建数据库表语句 2.插入测试数据 3.pom文件内容 <?xml version="1.0" encoding="U ...

  5. Mybatis中 Integer 值为0时,默认为空字符串的解决办法。

    需求是查询级别为0的用户 User对象里的level字段的值为0,查询时居然没有查到为level为0的用户. <select id="selectSelective" par ...

  6. MyBatis中实现多表查询

    如果查询的数据量大,推荐使用N+1次查询.数据量少使用联合查询... 一. 1.Mybatis是实现多表查询方式 1.1  业务装配:对两个表编写单表查询语句,在业务(Service)把查询的两表结果 ...

  7. Oracle使用MyBatis中RowBounds实现分页查询

    Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便. 使用MyBatis中的RowBounds进行 ...

  8. 在Mybatis中使用连表查询的一次实际应用

    以前在工作中很少使用多表关联查询,对连表查询的具体作用和使用场景也没有很直观的认识,通过这次在项目中的实际应用,对此有了一定的认识,特记录如下. 关联表介绍: 分别是属性表attr_info.属性值表 ...

  9. mybatis中根据日期模糊查询

    首先设置起始日期startDate和结束日期endDate,数据库中日期字段为achive_time,表名为dos_dossier<select id="getDossiers&quo ...

随机推荐

  1. 批量更改数据库表架构(生成sql后直接执行!)

    批量更改数据库表架构(生成sql后直接执行!) use my_test; --当前数据库 ), ), ), @NewSql VARCHAR(max), @Index INT; SET @SchemaO ...

  2. docker 快速搭建Nexus3

    1.拉取镜像 docker pull sonatype/nexus3 2.启动容器 : -p : -p : -v /mnt/gv0/nexus-data:/nexus-data sonatype/ne ...

  3. Win8设计——现代设计,可使你的应用脱颖而出的元素

    Microsoft 设计准则 Windows 在现代设计方面遥遥领先.它采用了“真实数字”原则并从瑞士风格和交通枢纽的寻路系统中汲取灵感. 阅读详细信息 设计元素 动态磁贴 动态磁贴向你提供了一个独特 ...

  4. html5里面的延迟加载属性

    html5中给script标签引入了 async 和 defer 属性. 原理:带有async属性的script标签,会在浏览器解析时立即下载脚本同时不阻塞后续的document渲染和script加载 ...

  5. javascript prototype学习

    function foo(a, b, c) { return a*b*c; } alert(foo.length); alert(typeof foo.constructor); alert(type ...

  6. Apache Server Status详解

    Apache的日志如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗.不过在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-statu ...

  7. hdu3507 Print Article[斜率优化dp入门题]

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. parted分区脚本

    #!/bin/bash #Used to fomat 6 disks PATH=/bin:/sbin:/usr/bin:/usr/sbin export PATH disk_to_parted=&qu ...

  9. 【CF883B】Berland Army 拓扑排序

    [CF883B]Berland Army 题意:给出n个点,m条有向边,有的点的点权已知,其余的未知,点权都在1-k中.先希望你确定出所有点的点权,满足: 对于所有边a->b,a的点权>b ...

  10. RabbitMQ服务端配置详解(转自:http://www.cnblogs.com/zhen-rh/p/6884297.html)

    RabbitMQ支持三种配置方式: 1) 读取环境变量中配置, 这包括shell中环境变量和rabbitmq-env.conf/rabbitmq-env-conf.bat文件中配置的环境变量 可配置如 ...