java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > foreach list和array用法

Mybatis foreach用法解析--对于list和array

作者:二十六画生的博客

这篇文章主要介绍了Mybatis foreach用法解析--对于list和array,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

foreach用法--对于list和array

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。

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

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3.如果使用Map封装了,collection的属性值为对应的Key

4.parameterType为list,java.util.List,java.util.ArrayList均可以,切记不可以为Array.

建表语句和表的数据,以及范例如下

第1种

DAO:

public List<User> findByIdList(List<String> idList);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="list">  <!-- parameterType="java.util.List" 也正确 -->
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="list" item="id" open="(" separator="," close=")">
        #{id}
    </foreach>
</select>

2018-02-04 14:44:18,015 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (66277ms)] - ==>  
Preparing: SELECT id, username, password, sex FROM t_user WHERE id in ( ? , ? , ? ) 
2018-02-04 14:44:18,015 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (66277ms)] - ==> 
Parameters: 1(String), 2(String), 3(String)
2018-02-04 14:44:18,062 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (66324ms)] - <==      
Total: 3

第2种

DAO:

public List<User> findByIdList(List<String> idList);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="list">  <!-- parameterType="java.util.List" 也正确 -->
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="list" item="item123" open="(" separator="," close=")">
        #{item123}
    </foreach>
</select>

2018-02-04 14:48:25,654 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (16876ms)] - ==>  
Preparing: SELECT id, username, password, sex FROM t_user WHERE id in ( ? , ? , ? ) 
2018-02-04 14:48:25,654 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (16876ms)] - ==> 
Parameters: 1(String), 2(String), 3(String)
2018-02-04 14:48:25,654 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (16876ms)] - <==      
Total: 3

第3种

DAO:

public List<User> findByIdList(List<String> idList);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="java.util.List">
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="List" item="item123" open="(" separator="," close=")">   <!-- 这里collection="List"报错 -->
        #{item123}
    </foreach>
</select>

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: 
Parameter 'List' not found. Available parameters are [list]

第4种

SERVICE:

public List<User> findByIdList(List<String> idList) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("List123", idList);
    return userDao.findByIdList(map);
}

DAO:

public List<User> findByIdList(Map<String, Object> map);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="java.util.List">
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="List123" item="item123" open="(" separator="," close=")">   <!-- 使用Map封装,key是List123 -->
        #{item123}
    </foreach>
</select>

2018-02-04 15:10:46,794 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (14878ms)] - ==>  
Preparing: SELECT id, username, password, sex FROM t_user WHERE id in ( ? , ? , ? ) 
2018-02-04 15:10:46,794 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (14878ms)] - ==> 
Parameters: 1(String), 2(String), 3(String)
2018-02-04 15:10:46,809 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (14893ms)] - <==      
Total: 3

第5种

SERVICE:

public List<User> findByIdList(String[] idArray) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("Array123", idArray);
    return userDao.findByIdList(map);
}

DAO:

public List<User> findByIdList(Map<String, Object> map);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="array">  <!-- 这里使用array会报错 -->
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="Array123" item="item123" open="(" separator="," close=")">
        #{item123}
    </foreach>
</select>

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. 
Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. 
Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'array'.  
Cause: java.lang.ClassNotFoundException: Cannot find class: array

第6种

SERVICE:

public List<User> findByIdList(String[] idArray) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("Array123", idArray);
    return userDao.findByIdList(map);
}

DAO:

public List<User> findByIdList(Map<String, Object> map);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="list">   <!-- parameterType="java.util.List" 或者 parameterType="java.util.ArrayList" 均正确 --> 
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="Array123" item="item123" open="(" separator="," close=")">
        #{item123}
    </foreach>
</select>

2018-02-04 15:23:07,928 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (43730ms)] - ==>  
Preparing: SELECT id, username, password, sex FROM t_user WHERE id in ( ? , ? , ? ) 
2018-02-04 15:23:07,928 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (43730ms)] - ==> 
Parameters: 1(String), 2(String), 3(String)
2018-02-04 15:23:07,928 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (43730ms)] - <==      
Total: 3

第7种

DAO:

public List<User> findByIdList(String[] idArray);

SQL:

<select id="findByIdList" resultMap="resultMap" parameterType="java.util.ArrayList">
    SELECT
    <include refid="columnSql"></include>
    FROM t_user
    WHERE id in
    <foreach collection="array" item="item123" open="(" separator="," close=")">
        #{item123}
    </foreach>
</select>

2018-02-04 15:32:44,682 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (21575ms)] - ==>  
Preparing: SELECT id, username, password, sex FROM t_user WHERE id in ( ? , ? , ? ) 
2018-02-04 15:32:44,682 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (21575ms)] - ==> 
Parameters: 1(String), 2(String), 3(String)
2018-02-04 15:32:44,682 -[DEBUG]  method:[com.springmvc.dao.UserDao.findByIdList (21575ms)] - <==      
Total: 3

    <sql id="columnSql">
        <trim suffixOverrides=","> <!-- suffixOverrides此时的作用是去除最后一个逗号 -->
            id,
            username,
            password,
            sex,
        </trim>
    </sql>

以上基于JDK1.8。日志为亲测。仅为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文