mybatis里面的foreach,mybatis for each
目录
foreach用法-对于目录和排列建表语句和表的数据,以及范例如下第一种第注射毒品种第3种第四种第5种第6种第七种
foreach用法--对于list和array
foreach的主要用在构建在条件中,它可以在结构化查询语言语句中进行迭代一个集合。
为每一个元素的属性主要有项目,索引,收藏,打开,分隔符,关闭.
项目表示集合中每一个元素进行迭代时的别名,索引指定一个名字,用于表示在迭代过程中,每次迭代到的位置,打开表示该语句以什么开始,分隔符表示在每次进行迭代之间以什么符号作为分隔符,关闭表示以什么结束。在使用为每一个的时候最关键的也是最容易出错的就是募捐属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个目录的时候,收藏属性值为目录
2.如果传入的是单参数且参数类型是一个排列数组的时候,收藏的属性值为排列
3.如果使用地图封装了,收藏的属性值为对应的钥匙
4 .参数类型为list,java.util.List,java.util.ArrayList均可以,切记不可以为数组。
建表语句和表的数据,以及范例如下
第1种
道:
public ListUser findByIdList(ListString id列表);SQL:
select id= findByIdList 结果映射=结果映射参数类型=列表!- parameterType=java.util.List 也正确-SELECT include refid= column SQL /include FROM t _ user WHERE id in foreach collection= list item= id open=( separator=, close=) # { id }/foreach/SELECT 2018-02-04 143:443:18,015-[DEBUG]方法:[com .春天MVC。道。userdao。findbyidlist(66277 ms)]-==准备33333333,)2018-02-04 14:443:18,015-[调试]方法:[com。春天MVC。道。userdao。findbyidlist(66277毫秒)]-==参数:1(字符串)、2(字符串)、3(字符串)2018-02-04 14:43:18,062-[调试]方法:[com
第2种
道:
public ListUser findByIdList(ListString id列表);SQL:
select id= findByIdList 结果映射=结果映射参数类型=列表!- parameterType=java.util.List 也正确-SELECT include refid= column SQL /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。日志为亲测。仅为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。