mybatis 中 foreach collection的三种用法
foreach元素的属性主要有 item,index,collection,open,separator,close。
- item表示集合中每一个元素进行迭代时的别名,
- index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
- open表示该语句以什么开始,
- separator表示在每次进行迭代之间以什么符号作为分隔 符,
- close表示以什么结束。
collection是这里面比较难得下面我们详细介绍一下实际中的运用:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
- 如果传入的参数是多个的时候,我们也可以放在实体类中(这种实际用到也是非常多的)
下面详细介绍这几种方法:
1.
-
<select id="queryUser" resultType="com.example.demo.entity.UserEntity">
-
select * from tab_user where user_id in
-
<foreach collection="list" item="item" open="(" separator="," close=")" index="index">
-
#{item}
-
</foreach>
-
</select>
-
/**
-
* 根据传入的id获取对应的user
-
* @param ids
-
* @return
-
*/
-
List<UserEntity> queryUser(List<String> ids);
2.
-
<delete id="deleteUserById" >
-
delete from tab_user where user_id in
-
<foreach collection="array" item="item" open="(" separator="," close=")" index="index">
-
#{item}
-
</foreach>
-
</delete>
-
/**
-
* 根据传入的ID删除对应的用户
-
* @param ids
-
* @return
-
*/
-
int deleteUserById (String[] ids);
3.
-
<select id="queryUserByUser" parameterType="java.util.Map" resultType="com.example.demo.entity.UserEntity">
-
select * from tab_user where user_name = #{name} and user_id in
-
<foreach collection="ids" item="item" open="(" separator="," close=")" index="index">
-
#{item}
-
</foreach>
-
</select>
-
/**
-
* 根据Map中的信息获取对应的用户
-
* @param user
-
* @return
-
*/
-
List<UserEntity> queryUserByUser(Map user);
4.
-
<insert id="addUsers" parameterType="com.example.demo.entity.UserEntity" >
-
insert into tab_user (user_id,user_name) values
-
<foreach collection="userId.split(',')" item="item" separator=",">
-
(#{item},#{userName})
-
</foreach>
-
</insert>
-
/**
-
* 根据传入的user添加用户 多个userId用逗号隔开
-
* @param user
-
* @return
-
*/
-
int addUsers(UserEntity user);
下面有附上完整的代码:
-
-
-
-
<mapper namespace="com.example.demo.mapper.UserMapper">
-
-
<select id="queryUser" resultType="com.example.demo.entity.UserEntity">
-
select * from tab_user where user_id in
-
<foreach collection="list" item="item" open="(" separator="," close=")" index="index">
-
#{item}
-
</foreach>
-
-
</select>
-
-
<delete id="deleteUserById" >
-
delete from tab_user where user_id in
-
<foreach collection="array" item="item" open="(" separator="," close=")" index="index">
-
#{item}
-
</foreach>
-
</delete>
-
-
<select id="queryUserByUser" parameterType="java.util.Map" resultType="com.example.demo.entity.UserEntity">
-
select * from tab_user where user_name = #{name} and user_id in
-
<foreach collection="ids" item="item" open="(" separator="," close=")" index="index">
-
#{item}
-
</foreach>
-
</select>
-
-
<insert id="addUsers" parameterType="com.example.demo.entity.UserEntity" >
-
insert into tab_user (user_id,user_name) values
-
<foreach collection="userId.split(',')" item="item" separator=",">
-
(#{item},#{userName})
-
</foreach>
-
</insert>
-
</mapper>
-
package com.example.demo.mapper;
-
-
-
import com.example.demo.entity.UserEntity;
-
-
import java.util.List;
-
import java.util.Map;
-
-
/**
-
* @author pidaowei
-
*/
-
public interface UserMapper {
-
-
/**
-
* 根据传入的id获取对应的user
-
* @param ids
-
* @return
-
*/
-
List<UserEntity> queryUser(List<String> ids);
-
-
/**
-
* 根据传入的ID删除对应的用户
-
* @param ids
-
* @return
-
*/
-
int deleteUserById (String[] ids);
-
-
/**
-
* 根据Map中的信息获取对应的用户
-
* @param user
-
* @return
-
*/
-
List<UserEntity> queryUserByUser(Map user);
-
-
/**
-
* 根据传入的user添加用户 多个userId用逗号隔开
-
* @param user
-
* @return
-
*/
-
int addUsers(UserEntity user);
-
-
}
-
package com.example.demo.entity;
-
import lombok.Data;
-
/**
-
* @author pidaowei
-
*/
-
-
public class UserEntity {
-
-
private String userId;
-
private String userName;
-
}
-