foreach标签在批量插入数据库时非常方便,但是很容易出错,我没有注意括号的位置让我折腾了一个半小时找问题,醉醉哒,所以在这里记录一下foreach的使用。

首先,这是insert批量插入正确的代码:

<insert id="insertList" parameterType="map" useGeneratedKeys="true" keyProperty="messageId" >
insert into message_table (
message_id
to_member_id,
message_title,
message_body,
message_time,
message_update_time,
message_state,
message_type,
read_member_id,
del_member_id,
to_member_name,
create_time,
type_sn,
type )values
<foreach collection="messageLt" item="m" separator="," >
(#{m.messageId},
#{m.toMemberId},
#{m.messageTitle},
#{m.messageBody},
UNIX_TIMESTAMP(NOW()),
#{m.messageUpdateTime},
#{m.messageState},
#{m.messageType},
#{m.readMemberId},
#{m.delMemberId},
#{m.toMemberName},
UNIX_TIMESTAMP(NOW()),
#{m.typeSn},
#{m.type})
</foreach> </insert>

我犯的错误是:

  insert tableName(
)values(
<foreach collection="messageLt" item="m" separator="," open="(" close=")" >
</foreach>
)

产生的SQL结果是:insert tableName(a,b,c)values((?,?,?),(?,?,?),(?,?,?))

报错原因插入行数不对应,正常结果应该是  insert tableName(a,b,c)values(?,?,?),(?,?,?),(?,?,?)。比较可以很明显看出问题。

下面是select的foreach使用:

<select id="selectBatchMCoupon" parameterType="map" resultMap="memberCouponResultMap">
select
mc.mcoupon_id,
mc.member_id,
mc.coupon_id,
mc.mcoupon_create_time,
mc.class_show,
c.coupon_title
from
member_coupon mc
left join eland_coupon c on c.coupon_id=mc.coupon_id
where 1=1 and mc.class_show=0 and
<if test="couponLt != null and couponLt != ''">
mc.coupon_id in
<foreach collection="couponLt" item="coupon" open="(" close=")" separator=",">
#{coupon}
</foreach> </if>
</select>

我犯的错误是:

 <if test="couponLt != null and couponLt != ''">
<foreach collection="couponLt" item="coupon" open="(" close=")" separator=",">
mc.coupon_id = #{coupon}
</foreach> </if>

这个错误犯的我都尴尬。。。

mybatis foreach的使用的更多相关文章

  1. 解决mybatis foreach 错误: Parameter '__frch_item_0' not found

    解决mybatis foreach 错误: Parameter '__frch_item_0' not found 在遍历对象的属性(是ArrayList对象)时报错: org.mybatis.spr ...

  2. mybatis foreach批量插入数据:Oracle与MySQL区别

    mybatis foreach批量插入数据:Oracle与MySQL不同点: 主要不同点在于foreach标签内separator属性的设置问题: separator设置为","分 ...

  3. mybatis foreach报错It was either not specified and/or could not be found for the javaType Type handler

    或许是惯性思维,在mybatis使用foreach循环调用的时候,很多时候都是传一个对象,传一个List的情况很少,所以写代码有时候会不注意就用惯性思维方法做了. 今天向sql传参,传了一个List作 ...

  4. Mybatis foreach标签含义

    背景 考虑以下场景: InfoTable(信息表): Name Gender Age Score 张三 男 21 90 李四 女 20 87 王五 男 22 92 赵六 女 19 94 孙七 女 23 ...

  5. mybatis foreach 循环 list(map)

    直接上代码: 整体需求就是: 1.分页对象里面有map map里面又有数组对象 2.分页对象里面有list list里面有map map里面有数组对象. public class Page { pri ...

  6. mybatis <forEach>标签的使用

    MyBatis<forEach>标签的使用 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis.当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名 ...

  7. myBatis foreach详解【转】

    MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...

  8. MyBatis foreach标签遍历数组

    有时候开发中需要根据多个ID去查询,可以将ID封装为List或者数组然后使用MyBatis中的foreach标签构建in条件. 这里我将ID封装为String[]作为参数. <select id ...

  9. mybatis ForEach Collection集合等规范解析(转)

    转自:http://blog.csdn.net/wj3319/article/details/9025349 在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了fo ...

  10. mybatis foreach标签

    一.批量插入数据 示例:添加订单商品表 1.模型层的相应代码 /** * 添加订单商品表 * @param ordergoods * @return */ public boolean addOrde ...

随机推荐

  1. yii事件

    控制器: public function actionTests1(){ $c = new \app\components\cat(); $m = new \app\components\mou; $ ...

  2. iOS9 适配(杂七杂八)

    1.iOS9 以后,table cell 在旋转的时候会自动调整视图内容的布局,设置以下的属性,课禁止该行为. if (runTimeOSVersion >= 9.0f) { _listTabl ...

  3. ASP.NET Core 添加日志NLog

    1.在Nuget上搜索 NLog.Extensions.Logging 安装最新版 2.添加日志配置文件,在项目指定目录下添加配置文件nlog.config,内容添加如下: <?xml vers ...

  4. oracle 解锁

    解决方法如下: 1:查V$DB_OBJECT_CACHE SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CRM_LASTCHGINFO_DAY' AND LO ...

  5. (copy) Top Ten Reasons not to use the C shell

    http://www.grymoire.com/Unix/CshTop10.txt ========================================================== ...

  6. Sublime WiFi真机同步和WiFi真机预览使用说明

    概述WiFi真机同步: 通过在Sublime中建立调试服务,接收真机设备主动连接调试的方式,配合apploader及自定义loader,为开发者提供在局域网内通过WiFi实现应用快速真机同步和实时预览 ...

  7. HP ALM 使用经验

    使用HP ALM(Application Lifecycle Management)软件有一个多月的时间了,我是从安装,部署,建项,配置,使用,再到问题收集,这个过程过来的.发现ALM是一个功能确实强 ...

  8. 理解会话Session

  9. jQuery语法介绍

    来自:http://www.cnblogs.com/ccorz/p/5803353.html jQuery类似于Python中模块的概念,是集成了javaScript和Dom的模块.大致分为两种版本1 ...

  10. Web 服务器 low bandth DOS attack

    https://www.owasp.org/images/0/04/Roberto_Suggi_Liverani_OWASPNZDAY2010-Defending_against_applicatio ...