服务器之家

服务器之家 > 正文

Mybatis 传输List的实现代码

时间:2020-12-29 14:59     来源/作者:starkpan

1. 当查询的参数只有一个时

?
1
findByIds(List<Long> ids)

 1.1 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list

Xml代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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>
<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.2 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array

Xml代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tabs where ID in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
 #{item}
</foreach>
  </select>
<select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tabs where ID in
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
 #{item}
</foreach>
  </select>

2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)

 这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称

         下面是一个示例     

?
1
2
3
4
Map<String, Object> params = new HashMap<String, Object>(2);
     params.put("name", name);
     params.put("ids", ids);
    mapper.findByIdsMap(params);

Xml代码

?
1
2
3
4
5
6
7
8
<select id="findByIdsMap" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from tabs where ID in
 <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
 #{item}
 </foreach>
</select>

总结

以上所述是小编给大家介绍的Mybtis 传输List的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/starkpan/article/details/77870448

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部