Oracle+Mybatis批量插入,更新和删除

1、插入

(1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过)

 <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}
,''
,#{dto.sysSource,jdbcType=VARCHAR}
,#{dto.isquery,jdbcType=VARCHAR}
from dual
</foreach>) A
</insert>

  注意:入参必须是list集合,sql语句中没有values;

(2)第二种方式:利用存储过程实现批量插入(验证过)

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

  注意:入参仍然是list集合,sql中有values,本质是利用存储过程实现批量插入;

(3)第三种方式:使用特殊的sql语句(网上搜到的,待验证) 

  参考博客:http://blog.csdn.net/w_y_t_/article/details/51416201

  

  下面这条sql语句可以实现一条语句批量插入!

 INSERT ALL
INTO USERINFO(userid,username) VALUES('','Tom')
INTO USERINFO(userid,username) VALUES('','Black')
INTO USERINFO(userid,username) VALUES('','Jetty')
INTO USERINFO(userid,username) VALUES('','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>

  注意:当list的size大于500时,会失败;

2、更新

(1)第一种方式:同样是利用存储过程(网上搜索的,还是上面那个博客)

 <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)第二种方式:利用条件实现(已验证)

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

  注意:同样可以使用or的条件实现,类似于下面删除的sql;

3、删除

与更新第二种方式类似

 <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>
上一篇:js实现上传图片本地预览功能以及限制图片的文件大小和尺寸大小


下一篇:Android -- MediaRecord