码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis批量和传参

时间:2014-07-30 20:35:24      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   使用   os   

MyBatis中批量插入  

方法一:

<insert id="insertbatch" parameterType="java.util.List">

  <selectKey keyProperty="fetchTime" order="BEFORE"

  resultType="java.lang.String">

  SELECT CURRENT_TIMESTAMP()

  </selectKey>

  insert into kangaiduoyaodian ( depart1, depart2, product_name,

  generic_name, img, product_specification, unit,

  approval_certificate, manufacturer, marketPrice, vipPrice,

  website, fetch_time, productdesc ) values

  <foreach collection="list" item="item" index="index"

  separator=",">

  ( #{item.depart1}, #{item.depart2}, #{item.productName},

  #{item.genericName}, #{item.img},

  #{item.productSpecification}, #{item.unit},

  #{item.approvalCertificate}, #{item.manufacturer},

  #{item.marketprice}, #{item.vipprice}, #{item.website},

  #{fetchTime}, #{item.productdesc} )

  </foreach>

  </insert>

方法二:

<insert id="batchInsertB2B" parameterType="ArrayList">
insert into xxxxtable(hkgs,hkgsjsda,office,asdf,ddd,ffff,supfullName,classtype,agent_type,remark)
<foreach collection="list" item="item" index="index" separator="union all">
select #{item.hkgs,jdbcType=VARCHAR},
#{item.hkgsjsda,jdbcType=VARCHAR},
#{item.office,jdbcType=VARCHAR},
#{item.asdf,jdbcType=VARCHAR},
#{item.ddd,jdbcType=VARCHAR},
#{item.ffff,jdbcType=VARCHAR},
#{item.supfullName,jdbcType=VARCHAR},0,0,
#{item.remark,jdbcType=VARCHAR} from dual
</foreach>
</insert>

 

可以考虑用union all来实现批量插入。
例如:
insert into XX_TABLE(XX,XX,XX)select ‘xx‘,‘xx‘,‘xx‘ union all select ‘xx‘,‘xx‘,‘xx‘ union all select ‘xx‘,‘xx‘,‘xx‘ ...
先拼装好语句再动态传入insert into XX_TABLE(XX,XX,XX)后面部分

 

MyBatis中批量删除

 

 

<!-- 通过主键集合批量删除记录 -->

 

<delete id="batchRemoveUserByPks" parameterType="java.util.List">

 

DELETE FROM LD_USER WHERE ID in 

 

<foreach item="item" index="index" collection="list" open="(" separator="," close=")">

 

#{item}

 

</foreach>

 

</delete>

 

 

 

 

 

MyBatis中in子句

mybatis in 参数 使用方法

 

1.只有一个参数

 

参数的类型要声明为List或Array

 

Sql配置如下:

 

<select id="selectProduct" resultMap="Map">

 

SELECT *

 

FROM PRODUCT

 

WHERE PRODUCTNO IN

 

     <foreach item="productNo" index="index" collection="参数的类型List或array">

 

            #{productNo}

 

    </foreach>

 

</select>

 

2.多个参数

 

首先要将多个参数写入同一个map,将map作为一个参数传入mapper

 

Sql配置如下:

 

<select id="selectProduct" resultMap="Map">

 

SELECT *

 

FROM PRODUCT

 

WHERE PRODUCTNO IN

 

     <foreach item="productNo" index="index" collection="map中集合参数的名称">

 

            #{productNo}

 

    </foreach>

 

</select>

 

 MyBatis批量修改

 

 

 

 <update id="updateOrders" parameterType="java.util.List">
 update orders set state = ‘0‘ where no in
 <foreach collection="list" item="nos" open="(" separator="," close=")">
   #{nos}
 </foreach>
 </update>

Mybatis批量和传参,布布扣,bubuko.com

Mybatis批量和传参

标签:des   style   blog   http   color   java   使用   os   

原文地址:http://www.cnblogs.com/loveless/p/3878797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!