您的位置:首页 > 其它

mybatis使用foreach语句实现IN查询

2017-05-31 20:28 369 查看

mybatis使用foreach语句实现IN查询

先看一个实例

mapper.java:

List<User> selectByIdSet(@Param("idList")List<Integer> idList);


mapper.xml

<select id="selectByIdSet" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from t_user
WHERE id IN
<foreach collection="idList" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</select>


log打出的执行时的sql语句:

insert into t_user (id, name, age, sex, create_time) values (?, ?, ?, ?)


foreach语句中, collection属性的参数类型可以使:List、数组、map集合

​ collection: 必须跟mapper.java中@Param标签指定的元素名一样

​ item : 表示在迭代过程中每一个元素的别名,可以随便起名,但是必须跟元素中的#{}里面的名称一样。

    index :表示在迭代过程中每次迭代到的位置(下标)

    open :前缀, sql语句中集合都必须用小括号()括起来

​ close :后缀

    separator :分隔符,表示迭代时每个元素之间以什么分隔
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis