赞
踩
foreach 用于迭代传入过来的参数。
它的属性介绍分别是
介绍完属性之后,下面就进入实践。首先先来看一个简单到爆炸的表(表名:t_test_foreach)
- (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>
-
-
- <!--list<String>-->
- <select id="getSocketNumList" parameterType="com.ex.application.model.dto.ScoketNumDTO" resultType="com.ex.application.model.dto.ScoketNumDTO">
- select sn,station_sn,socket_number,member_sn,state from socket where 1=1
- <if test="station_sn != null and station_sn != ''">
- station_sn = #{station_sn}
- </if>
- <if test="station_sns != null and station_sns.size() > 0" >
- and station_sn in
- <foreach collection="station_sns" open="(" separator="," close=")" item="item" index="index">
- #{item,jdbcType=VARCHAR}
- </foreach>
- </if>
- </select>
-
- (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>

- <delete id="deleteByList">
- delete from goods where id in
- <foreach collection="list" open="(" separator="," close=")" item="haha">
- #{haha}
- </foreach>
-
- </delete>
-
-
- //批量操作 (返回影响了几条数据的一个int 数字)
- public Integer deleteByList(List<GoodsInfo> list);
测试类
- // ids = {1,2,3}
- public List<User> testFindByArray(int[] ids) throws Exception {
- SqlSession sqlSession = getSession().openSession();
- userList = sqlSession.selectList(NameSpace + ".findByArray", ids);
- System.out.println(userList.toString());
- sqlSession.close();
- return userList;
- }
mapper.xml
- <!--这里的 item 值可以和传递过来的参数名不一样,在介绍属性的时候已经说过这是一个别名了。比如可以修改成如下代码:
- <foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
- #{id} <!--这里要和 item 值保持一致-->
- </foreach>
- -->
- <select id="findByArray" resultType="com.test.foreach.User">
- SELECT id,`name` FROM t_test_foreach WHERE id IN
- <foreach collection="array" item="ids" index="index" open="(" close=")" separator=",">
- #{ids}
- </foreach>
- </select>
输出结果
- DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? , ? )
- DEBUG - ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
- DEBUG - <== Total: 3
- [User{name='n1', id='1'}, User{name='n2', id='2'}, User{name='n3', id='3'}]
测试类
- // List 元素有 1,3,5
- public List<User> testFindByList(List<Integer> ids) throws Exception {
- SqlSession sqlSession = getSession().openSession();
- userList = sqlSession.selectList(NameSpace + ".findByList", ids);
- System.out.println(userList.toString());
- sqlSession.close();
- return userList;
- }
mapper.xml
- <select id="findByList" resultType="com.test.foreach.User">
- SELECT id,`name` FROM t_test_foreach WHERE id IN
- <foreach collection="list" item="ids" index="index" open="(" close=")" separator=",">
- #{ids}
- </foreach>
- </select>
输出结果
- DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? , ? )
- DEBUG - ==> Parameters: 1(Integer), 3(Integer), 5(Integer)
- DEBUG - <== Total: 3
- [User{name='n1', id='1'}, User{name='n3', id='3'}, User{name='n5', id='5'}]
测试类
- // Map<String, Object> 中的元素有 int[] ids = {2, 4};map.put("ids", ids);
- public List<User> testFindByMap(Map map) throws Exception {
- SqlSession sqlSession = getSession().openSession();
- System.out.println(map.toString());
- List<Object> objects = sqlSession.selectList(NameSpace + ".findByMap", map);
- System.out.println(objects.toString());
- sqlSession.close();
- return userList;
- }
mapper.xml
- <!--注意 collection 值是 ids,即要进行迭代的对象。觉得有点懵的伙伴可以回到最开始介绍 collection 属性那里看看,不要急-->
- <select id="findByMap" resultType="com.test.foreach.User">
- SELECT id,`name` FROM t_test_foreach WHERE id IN
- <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
- #{id}
- </foreach>
- </select>
输出结果
- DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? )
- DEBUG - ==> Parameters: 2(Integer), 4(Integer)
- DEBUG - <== Total: 2
- [User{name='n2', id='2'}, User{name='n4', id='4'}]
这种情况在传参数时,一定要改用 Map 方式
测试类
- public void testUpdateByParams(int[] ids,String name) throws Exception {
- SqlSession sqlSession = getSession().openSession();
- Map<String,Object> map = new HashMap<String, Object>();
- map.put("ids",ids); // ids = {1,2,4}
- map.put("name",name);// name = "updated"
- sqlSession.selectList(NameSpace + ".findByParams", map);
- sqlSession.close();
- }
-
mapper.xml
- <select id="findByParams">
- UPDATE t_test_foreach SET `name` = '#{name}' WHERE id IN
- <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
- #{item}
- </foreach>
- </select>
输出结果
- DEBUG - ==> Preparing: UPDATE t_test_foreach SET `name` = ? WHERE id IN ( ? , ? , ? )
- DEBUG - ==> Parameters: updated(String), 1(Integer), 2(Integer), 4(Integer)
尊重他人劳动成果,转载请注明出处:
mybatis foreach 属性及其三种使用情况_mybatis foreach object-CSDN博客
参考文章:
Mybatis 示例之 foreach (上)_mybatis的foreach-CSDN博客
MyBatis的foreach语句详解-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。