MyBatis传入参数为List对象的实现方法

发布时间:2021-03-06 10:28:23 作者:小新
来源:亿速云 阅读:279

这篇文章主要介绍MyBatis传入参数为List对象的实现方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

SSM框架是JavaWeb必学的框架,虽说基本的增删改查很简单,但是当面临一些特殊情况时,有时还是会显得手足无措,此篇用来记录一些特殊场景下Mybatis框架的应用.

传入参数为List对象

1. 场景复现

首先有如下一张表:

MySQL [test]> select * from t_entry_resource;
+----+-------------+------+----------+--------+--------+---------------------+
| id | resource_id | type | title  | banner | icon | add_date      |
+----+-------------+------+----------+--------+--------+---------------------+
| 11 |     6 | 14  | 分类   | 1.jpg | 2.jpg | 2017-11-17 11:22:30 |
| 12 |     3 | 1  | 测试12  | 3.jpg | 4.jpg | 2017-11-17 11:22:30 |
| 13 |    653 | 1  | 测试34  | 5.jpg | 6.jpg | 2017-11-20 02:32:26 |
| 14 |     1 | 1  | 测试5  | 7.jpg | 8.jpg | 2017-11-20 02:32:51 |
| 15 |    3942 | 3  | 测试6  | 9.jpg | 10.jpg | 2017-11-20 02:34:27 |
+----+-------------+------+----------+--------+--------+---------------------+
5 rows in set (0.01 sec)

如果要根据resource_id和type来批量查询记录,该如何编写Mybatis语句?

2. 解决方案

直接贴出来解决方案如下所示:

Dao层接口:

List<EntryResource> findByRidAndType(List<EntryResource> entryResources);

XML语句:

<select id="findByRidAndType" resultMap="entryResource" parameterType="list">
    SELECT
    *
    FROM
    t_entry_resource a
    WHERE
<foreach collection="list" index="index" item="entryResources" open="(" close=")" separator="or">
      ( `type`=#{entryResources.type} and resource_id=#{entryResources.resourceId} )
</foreach>

</select>

该语句利用了mybatis的foreach动态拼接SQL。

3. foreach属性

属性描述
item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。具体说明:在list和数组中是其中的对象,在map中是value。该参数为必选。
collection要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids"如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"上面只是举例,具体collection等于什么,就看你想对那个元素做循环。该参数为必选。
separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。
closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

4. foreach的几种用法

(1) select count(*) from users id in (x1,x2,x3,...)

<select id="countByUserList" resultType="int" parameterType="list">  
select count(*) from users  
 <where>  
  id in  
  <foreach item="item" collection="list" separator="," open="(" close=")" index="">  
   #{item.id, jdbcType=NUMERIC}  
  </foreach>  
 </where>  
</select>

(2) select count(*) from key_cols where col_a = ? AND col_b = ?

<select id="sel_key_cols" resultType="int">  
    select count(*) from key_cols where  
<foreach item="item" index="key" collection="map" open="" separator="AND" close="">
    ${key} = #{item}
</foreach>  
</select>

(3) select * from t_news n where n.tags like ? or n.tags like ?

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
 select * from t_news n where 
 <foreach collection="listTag" index="index" item="tag" open="" separator="or" close="">
      n.tags like '%'||#{tag}||'%'
 </foreach>
<select>

以上是“MyBatis传入参数为List对象的实现方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Mybatis如何传入多个参数
  2. Mybatis如何传入多个参数的实现代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis list对象

上一篇:PHP如何随机生成18位不重复的订单号

下一篇:Thinkphp如何自定义美化success和error提示跳转页面

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》