foreach用法--对于list和array
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item
表示集合中每一个元素进行迭代时的别名,index
指定一个名字,用于表示在迭代过程中,每次迭代到的位置,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 ListfindByIdList(List idList);
SQL:
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 ListfindByIdList(List idList);
SQL:
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 ListfindByIdList(List idList);
SQL:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException:
Parameter 'List' not found. Available parameters are [list]
第4种
SERVICE:
public ListfindByIdList(List idList) { Map map = new HashMap (); map.put("List123", idList); return userDao.findByIdList(map); }
DAO:
public ListfindByIdList(Map map);
SQL:
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 ListfindByIdList(String[] idArray) { Map map = new HashMap (); map.put("Array123", idArray); return userDao.findByIdList(map); }
DAO:
public ListfindByIdList(Map map);
SQL:
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 ListfindByIdList(String[] idArray) { Map map = new HashMap (); map.put("Array123", idArray); return userDao.findByIdList(map); }
DAO:
public ListfindByIdList(Map map);
SQL:
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 ListfindByIdList(String[] idArray);
SQL:
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
id, username, password, sex,
以上基于JDK1.8。日志为亲测。仅为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。