MyBatis+Oracle带自增主键的批量添加、修改、模糊查询

一、MyBatis+Oracle带自增主键的批量添加

方法一:
<insert id="addBatch" parameterType="java.util.List">  
BEGIN  
<foreach collection="list" item="item" index="index" separator="">  
 INSERT INTO JOB_QUEUE(  ID, JOB_ID, START_TIME, END_TIME, MODIFIED_USER, CREATED_TIME, UPDATED_TIME   )
 VALUES 
(JOB_QUEUE_SEQ.NEXTVAL,#{item.jobId}, #{item.startTime}, #{item.endTime}, 
 #{item.modifiedUser},  systimestamp,   systimestamp );  
 </foreach>  
  COMMIT;  
  END;  
  </insert> 
方法二:
<insert id="insertSalconfigAfter" parameterType="java.util.List">  
    <![CDATA[  
       INSERT INTO TB_DEPARTMENT (id,s1,s2,s3,s4,s5,s6)  
    ]]>  
    select TB_MY_SEQUENCE.NEXTVAL,m.* from (  
<foreach collection="list" item="item" index="index" separator="union all">  
         select  
        #{item.s1,jdbcType=VARCHAR},  
        #{item.s2,jdbcType=VARCHAR},  
        #{item.s3,jdbcType=VARCHAR},  
        #{item.s4,jdbcType=VARCHAR},  
        #{item.s5,jdbcType=VARCHAR},  
        #{item.s6,jdbcType=VARCHAR}  
              from dual  
</foreach>  
    ) m  
</insert>

方法一存在批量添加大量数据时SQL插入语句执行不完,就没有提交事务,再次执行该方法,就会使第一次插入的数据全部回滚,导致第一次插入的数据存入不到数据库。

方法二没有加以验证,希望大家给予指正。


二、MyBatis+Oracle带自增主键的批量修改


最开始的时候,想着写一系列并列的更新语句就可以了
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";"
  open="" close="">
  update REGION_CODE set
    CODE=#{item.Code,jdbcType=VARCHAR},
    NAME=#{item.Name,jdbcType=VARCHAR}
    where ID = #{item.id,jdbcType=DECIMAL}
</foreach>
</update>
这样直接报错,因为Mybatis映射文件中的sql语句不允许 ; 符号。按照可行的case when处理方式,Mybatis映射文件书写方式如下:
<update id="updateBatch" parameterType="java.util.List">
  update REGION_CODE set
    CODE=
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
      when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}
  </foreach>
  ,NAME=
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
      when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}
  </foreach>
  where ID in
  <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
      #{item.id,jdbcType=DECIMAL}
  </foreach>
</update>
      至此,批量更新功能完成。
另外,注意一下oracle的SQL带in只能修改1000条一下,所以这个地方需要把大于1000条的批量修改进行分段截取拼装成in () or in ()的字符串最好。
  1. /** 
  2.      * 根据传入的List和参数,拼接in条件,防止in超过999条 
  3.      * 
  4.      * @param list 
  5.      * @param parameter 
  6.      * @return list.size()=n  'list1','list2',...,'list900') or parameter in ('list901','list902',...,'list1800') or parameter in ('list1801','list1802',...,'listn' 
  7.      * list.size()=0  '' 
  8.      */  
  9.     public static String getInParameter(List list, String parameter) {  
  10.         if (!list.isEmpty()) {  
  11.             List<String> setList = new ArrayList<String>(0);  
  12.             Set set = new HashSet();  
  13.             StringBuffer stringBuffer = new StringBuffer();  
  14.             for (int i = 1; i <= list.size(); i++) {  
  15.                 set.add("'" + list.get(i - 1) + "'");  
  16.                 if (i % 900 == 0) {//900为阈值  
  17.                     setList.add(StringUtils.join(set.iterator(), ","));  
  18.                     set.clear();  
  19.                 }  
  20.             }  
  21.             if (!set.isEmpty()) {  
  22.                 setList.add(StringUtils.join(set.iterator(), ","));  
  23.             }  
  24.             stringBuffer.append(setList.get(0));  
  25.             for (int j = 1; j < setList.size(); j++) {  
  26.                 stringBuffer.append(") or " + parameter + " in (");  
  27.                 stringBuffer.append(setList.get(j));  
  28.             }  
  29.             return stringBuffer.toString();  
  30.         } else {  
  31.             return "''";  
  32.         }  
  33.   
  34.     } 


三、MyBatis+Oracle的模糊查询

1.  参数中直接加入%%
  param.setUsername("%CD%");
      param.setPassword("%11%");
<select id="selectPersons" resultType="person" parameterType="person">select id,sex,age,username,password from person where true <if test="username!=null"> AND username LIKE #{username}</if><if test="password!=null">AND password LIKE #{password}</if></select>
2.  bind标签
<select id="selectPersons" resultType="person" parameterType="person"> <bind name="pattern" value="'%' + _parameter.username + '%'" /> select id,sex,age,username,password from person where username LIKE #{pattern}</select>
 
 
3. CONCAT
where username LIKE concat(concat('%',#{username}),'%')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值