mybatis批量和传参

1、批量添加元素session.insert(String string,Object o)

方法一:

<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>

  1. public void batchInsertStudent(){  
  2.     List<Student> ls = new ArrayList<Student>();  
  3.     for(int i = 5;i < 8;i++){  
  4.         Student student = new Student();  
  5.         student.setId(i);  
  6.         student.setName("maoyuanjun" + i);  
  7.         student.setSex("man" + i);  
  8.         student.setTel("tel" + i);  
  9.         student.setAddress("浙江省" + i);  
  10.         ls.add(student);  
  11.     }  
  12.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();  
  13.     session.insert("mybatisdemo.domain.Student.batchInsertStudent", ls);  
  14.     session.commit();  
  15.     session.close();  
  16. }  
  17.   
  18. <insert id="batchInsertStudent" parameterType="java.util.List">  
  19.     INSERT INTO STUDENT (id,name,sex,tel,address)  
  20.     VALUES   
  21.     <foreach collection="list" item="item" index="index" separator="," >  
  22.         (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})  
  23.     </foreach>  
  24. </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)后面部分

 

2、批量修改session. insert (String string,Object o)

 

 <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>

    1. 实例1:  
    2. public void batchUpdateStudent(){  
    3.     List<Integer> ls = new ArrayList<Integer>();  
    4.     for(int i = 2;i < 8;i++){  
    5.         ls.add(i);  
    6.     }  
    7.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();  
    8.     session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls);  
    9.     session.commit();  
    10.     session.close();  
    11. }  
    12. <update id="batchUpdateStudent" parameterType="java.util.List">  
    13.     UPDATE STUDENT SET name = "5566" WHERE id IN  
    14.     <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >  
    15.         #{item}  
    16.     </foreach>  
    17. </update>  
    18.   
    19. 实例2:  
    20. public void batchUpdateStudentWithMap(){  
    21.     List<Integer> ls = new ArrayList<Integer>();  
    22.     for(int i = 2;i < 8;i++){  
    23.         ls.add(i);  
    24.     }  
    25.     Map<String,Object> map = new HashMap<String,Object>();  
    26.     map.put("idList", ls);  
    27.     map.put("name", "mmao789");  
    28.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();  
    29.     session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map);  
    30.     session.commit();  
    31.     session.close();  
    32. }  
    33. <update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >  
    34.     UPDATE STUDENT SET name = #{name} WHERE id IN   
    35.     <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">   
    36.         #{item}   
    37.     </foreach>  
    38. </update>

3、批量删除session.delete(String string,Object o)

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

<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>

    1. public void batchDeleteStudent(){  
    2.     List<Integer> ls = new ArrayList<Integer>();  
    3.     for(int i = 4;i < 8;i++){  
    4.         ls.add(i);  
    5.     }  
    6.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();  
    7.     session.delete("mybatisdemo.domain.Student.batchDeleteStudent",ls);  
    8.     session.commit();  
    9.     session.close();  
    10. }  
    11. <delete id="batchDeleteStudent" parameterType="java.util.List">  
    12.     DELETE FROM STUDENT WHERE id IN  
    13.     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">   
    14.         #{item}   
    15.     </foreach>  
    16. </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> 

posted on 2014-08-01 17:59  来,我帮你解决  阅读(354)  评论(0)    收藏  举报

导航