Oracle + Mybatis implements batch insert update and delete sample code

  • 2020-12-21 18:01:38
  • OfStack

preface

Mybatis is a very common framework for data persistence in web engineering development. Through this framework, it is very easy for us to add, delete, modify and check the database. When a database connection commits a transaction, it needs to consume more resources. If more updated data need to be inserted, and only one data is committed per transaction, it will cause a very large waste of database resources, leading to a significant decline in database performance and system performance.

As for the bulk inserts of mybatis, most of the examples on the web are mostly about the MySQL database, and few are about the Oracle database. This article will introduce you to Oracle+Mybatis batch insert, update and delete related content, the following is not enough, let's look at the detailed introduction of 1.

1, insert,

(1) The first method: utilization < foreach > Tag to generate virtual data through UNION ALL to achieve batch insertion (verified)


<insert id="insertBatchLaTContactRecord" parameterType="java.util.Map">
 <selectKey resultType="java.lang.Long" keyProperty="dto.id" order="BEFORE">
    select seq_LA_T_CONTACT_RECORD.nextval as id from dual
 </selectKey>
   insert into la_t_contact_record
   (   
     id        ,
     contract_id     ,
     contacter_add_name    ,
     contacter_add_type    ,
     contact_add_phone    ,
     contact_add_home_address  ,
     contact_add_work    ,
     contact_add_work_address  ,
     create_by      ,
     create_time     ,
     modify_by      ,
     modify_time     ,
     validate_state     ,
     sys_source      ,
     isquery        
   )
  select seq_LA_T_CONTACT_RECORD.NEXTVAL,A.* from( 
 <foreach collection="list" item="dto" index="index" separator="UNION ALL">
  select 
     #{dto.contractId,jdbcType=VARCHAR}
     ,#{dto.contacterAddName,jdbcType=VARCHAR}
     ,#{dto.contacterAddType,jdbcType=VARCHAR}
     ,#{dto.contactAddPhone,jdbcType=VARCHAR}
     ,#{dto.contactAddHomeAddress,jdbcType=VARCHAR}
     ,#{dto.contactAddWork,jdbcType=VARCHAR}
     ,#{dto.contactAddWorkAddress,jdbcType=VARCHAR}
     ,#{dto.createBy,jdbcType=DECIMAL}
     ,systimestamp
     ,#{dto.modifyBy,jdbcType=DECIMAL}
     ,#{dto.modifyTime,jdbcType=TIMESTAMP}
     ,'1'
     ,#{dto.sysSource,jdbcType=VARCHAR}
     ,#{dto.isquery,jdbcType=VARCHAR}
  from dual
 </foreach>) A
</insert>

Note: the input must be the list set. values is not in the sql statement.

(2) The second way: Using stored procedures to achieve batch insert (verified)


<insert id="insertPlanRepaymentOtherfeeBatch" parameterType="java.util.List">
  begin
  <foreach collection="list" item="item" index="index">
   insert into lb_t_plan_repayment_otherfee
   (
   id        ,
   key       ,
   value       ,
   term       ,
   contract_id,
   PAY_ORDER,
   FEE_NAME,
   INTO_ID
   )
   values(SEQ_LB_T_PLAN_REPAY_OTHERFEE.nextval
   ,#{item.key,jdbcType=VARCHAR}
   ,#{item.value,jdbcType=VARCHAR}
   ,#{item.term,jdbcType=DECIMAL}
   ,#{item.contractId,jdbcType=VARCHAR}
   ,#{item.payOrder,jdbcType=DECIMAL}
   ,#{item.feeName,jdbcType=VARCHAR}
   ,#{item.intoId,jdbcType=VARCHAR}
   );
  </foreach>
  end;
 </insert>

Note: The input parameter is still the list set, and values is in sql. In essence, the stored procedure is used to implement batch insert.

(3) The third way: use special sql statement (found on the Internet, to be verified)

Refer to the blog: http: / / blog csdn. net/w_y_t_ / article/details / 51416201

The following sql statement can achieve 1 statement batch insert!


INSERT ALL 
 INTO USERINFO(userid,username) VALUES('1001','Tom') 
 INTO USERINFO(userid,username) VALUES('1002','Black') 
 INTO USERINFO(userid,username) VALUES('1003','Jetty') 
 INTO USERINFO(userid,username) VALUES('1004','Cat') 
SELECT 1 FROM DUAL;

<insert id="batchInsertUser" parameterType="java.util.ArrayList"> 
INSERT ALL 
  <foreach collection="list" item="userList" index="index"> 
   INTO USERINFO(userid,username) VALUES(#{userList.userid},#{userList.username}) 
  </foreach> 
  SELECT 1 FROM DUAL 
</insert>

Note: When size is greater than 500, it will fail.

2, update,

(1) The first way: also using stored procedures (online search, the same blog above)


<update id="batchUpdateUser" parameterType="java.util.ArrayList"> 
 <foreach collection="list" item="userlist" index="index" open="begin" close=";end;" separator=";"> 
  UPDATE USERINFO T 
   <set> 
   T.USERID = #{userlist.userid,jdbcType=VARCHAR}, 
   T.USERNAME = #{userlist.username,jdbcType=VARCHAR}, 
   </set> 
   WHERE 
   T.USERID = #{userlist.userid,jdbcType=VARCHAR} 
 </foreach> 
</update>

(2) The second way: Using conditions to achieve (verified)


<update id="updateBatchByListStat" parameterType="java.util.Map">
 update la_t_advfinished t1
    set t1.list_stat='07',
    t1.modify_time     =systimestamp 
   where t1.id in(<foreach collection="ids" separator="," item="id">'${id}'</foreach>)
</update>

Note: You can also use the conditional implementation of or, similar to sql removed below;

3, delete,

This is similar to updating the second option


<delete id="deleteAttractions" parameterType="java.util.List">
 delete from ATTRACTIONS
 <where>
 <foreach collection="list" index="index" item="item" open="(" separator="or" close=")">  
 id=#{item.id}
 </foreach>
 </where>
 </delete>

conclusion


Related articles: