zoukankan      html  css  js  c++  java
  • 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.  
      }
  • 相关阅读:
    一条SQL的执行流程
    LinkedList源码解析
    MinorGC前检查
    AbstractList源码分析
    JVM常用命令
    CountDownLatch源码解析
    ReentrantLock源码解析
    HTTPS简单介绍
    工厂方法模式
    观察者模式
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/14151464.html
Copyright © 2011-2022 走看看