mybatis 中 foreach collection的三種用法


foreach元素的屬性主要有 item,index,collection,open,separator,close。

  1. item表示集合中每一個元素進行迭代時的別名,
  2. index指 定一個名字,用於表示在迭代過程中,每次迭代到的位置,
  3. open表示該語句以什么開始,
  4. separator表示在每次進行迭代之間以什么符號作為分隔 符,
  5. close表示以什么結束。

collection是這里面比較難得下面我們詳細介紹一下實際中的運用:

  1. 如果傳入的是單參數且參數類型是一個List的時候,collection屬性值為list 
  2. 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array 
  3. 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可
  4. 如果傳入的參數是多個的時候,我們也可以放在實體類中(這種實際用到也是非常多的)

下面詳細介紹這幾種方法: 

1.

  1.  
    <select id="queryUser" resultType="com.example.demo.entity.UserEntity">
  2.  
    select * from tab_user where user_id in
  3.  
    <foreach collection="list" item="item" open="(" separator="," close=")" index="index">
  4.  
    #{item}
  5.  
    </foreach>
  6.  
    </select>
  1.  
    /**
  2.  
    * 根據傳入的id獲取對應的user
  3.  
    * @param ids
  4.  
    * @return
  5.  
    */
  6.  
    List<UserEntity> queryUser(List<String> ids);

2.

  1.  
    <delete id="deleteUserById" >
  2.  
    delete from tab_user where user_id in
  3.  
    <foreach collection="array" item="item" open="(" separator="," close=")" index="index">
  4.  
    #{item}
  5.  
    </foreach>
  6.  
    </delete>
  1.  
    /**
  2.  
    * 根據傳入的ID刪除對應的用戶
  3.  
    * @param ids
  4.  
    * @return
  5.  
    */
  6.  
    int deleteUserById (String[] ids);

3.

  1.  
    <select id="queryUserByUser" parameterType="java.util.Map" resultType="com.example.demo.entity.UserEntity">
  2.  
    select * from tab_user where user_name = #{name} and user_id in
  3.  
    <foreach collection="ids" item="item" open="(" separator="," close=")" index="index">
  4.  
    #{item}
  5.  
    </foreach>
  6.  
    </select>
  1.  
    /**
  2.  
    * 根據Map中的信息獲取對應的用戶
  3.  
    * @param user
  4.  
    * @return
  5.  
    */
  6.  
    List<UserEntity> queryUserByUser(Map user);

 

4.

  1.  
    <insert id="addUsers" parameterType="com.example.demo.entity.UserEntity" >
  2.  
    insert into tab_user (user_id,user_name) values
  3.  
    <foreach collection="userId.split(',')" item="item" separator=",">
  4.  
    (#{item},#{userName})
  5.  
    </foreach>
  6.  
    </insert>
  1.  
    /**
  2.  
    * 根據傳入的user添加用戶 多個userId用逗號隔開
  3.  
    * @param user
  4.  
    * @return
  5.  
    */
  6.  
    int addUsers(UserEntity user);

 

下面有附上完整的代碼:

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3.  
     
  4.  
    <mapper namespace="com.example.demo.mapper.UserMapper">
  5.  
     
  6.  
    <select id="queryUser" resultType="com.example.demo.entity.UserEntity">
  7.  
    select * from tab_user where user_id in
  8.  
    <foreach collection="list" item="item" open="(" separator="," close=")" index="index">
  9.  
    #{item}
  10.  
    </foreach>
  11.  
     
  12.  
    </select>
  13.  
     
  14.  
    <delete id="deleteUserById" >
  15.  
    delete from tab_user where user_id in
  16.  
    <foreach collection="array" item="item" open="(" separator="," close=")" index="index">
  17.  
    #{item}
  18.  
    </foreach>
  19.  
    </delete>
  20.  
     
  21.  
    <select id="queryUserByUser" parameterType="java.util.Map" resultType="com.example.demo.entity.UserEntity">
  22.  
    select * from tab_user where user_name = #{name} and user_id in
  23.  
    <foreach collection="ids" item="item" open="(" separator="," close=")" index="index">
  24.  
    #{item}
  25.  
    </foreach>
  26.  
    </select>
  27.  
     
  28.  
    <insert id="addUsers" parameterType="com.example.demo.entity.UserEntity" >
  29.  
    insert into tab_user (user_id,user_name) values
  30.  
    <foreach collection="userId.split(',')" item="item" separator=",">
  31.  
    (#{item},#{userName})
  32.  
    </foreach>
  33.  
    </insert>
  34.  
    </mapper>

 

  1.  
    package com.example.demo.mapper;
  2.  
     
  3.  
     
  4.  
    import com.example.demo.entity.UserEntity;
  5.  
     
  6.  
    import java.util.List;
  7.  
    import java.util.Map;
  8.  
     
  9.  
    /**
  10.  
    * @author pidaowei
  11.  
    */
  12.  
    public interface UserMapper {
  13.  
     
  14.  
    /**
  15.  
    * 根據傳入的id獲取對應的user
  16.  
    * @param ids
  17.  
    * @return
  18.  
    */
  19.  
    List<UserEntity> queryUser(List<String> ids);
  20.  
     
  21.  
    /**
  22.  
    * 根據傳入的ID刪除對應的用戶
  23.  
    * @param ids
  24.  
    * @return
  25.  
    */
  26.  
    int deleteUserById (String[] ids);
  27.  
     
  28.  
    /**
  29.  
    * 根據Map中的信息獲取對應的用戶
  30.  
    * @param user
  31.  
    * @return
  32.  
    */
  33.  
    List<UserEntity> queryUserByUser(Map user);
  34.  
     
  35.  
    /**
  36.  
    * 根據傳入的user添加用戶 多個userId用逗號隔開
  37.  
    * @param user
  38.  
    * @return
  39.  
    */
  40.  
    int addUsers(UserEntity user);
  41.  
     
  42.  
    }
  1.  
    package com.example.demo.entity;
  2.  
    import lombok.Data;
  3.  
    /**
  4.  
    * @author pidaowei
  5.  
    */
  6.  
    @Data
  7.  
    public class UserEntity {
  8.  
     
  9.  
    private String userId;
  10.  
    private String userName;
  11.  
    }
  12.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM