1. 程式人生 > >Mybatis foreach巢狀foreach

Mybatis foreach巢狀foreach

  • sql語句,加入declare 和 select count(*) into v_rowcount from dual;目的是為了防止出現,begin end; 這種情況,導致後臺報錯。

<insert id="batchDoubleForeachInsertTest">
	 declare 
  		 v_rowcount number(10);
	begin
	  	 select count(*) into v_rowcount from dual;
	     <foreach collection="items" item="aa" index="index">
	          <foreach  collection="aa.items" item="it" index="index">
	            	<if test="aa.biz_flow_uuid != null">
	             		insert into aa(a,b) values(#{it,jdbcType=VARCHAR},#{aa.biz_flow_uuid,jdbcType=VARCHAR});
	            	</if>
	          </foreach>
	     </foreach>
	end;
</insert>
  • 後臺傳參及其呼叫
//拼裝資料進行插入
Map<String,Object> attribute = new HashMap<String,Object>();
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("biz_flow_uuid", "a");
map.put("items", "1,2".split(","));
data.add(map);
//注意:少了biz_flow_uuid
Map<String,Object> map1 = new HashMap<String,Object>();
map1.put("items", "3,4".split(","));
data.add(map1);
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("biz_flow_uuid", "b");
map2.put("items", "5,6".split(","));
data.add(map2);
attribute.put("items", data);
int count = sqlSession.insert("com.kyl.oracle.batchDoubleForeachInsertTest", attribute);
System.out.println("countAdmin, count:" + count);  
  • 最終拼接成的sql
begin 
	insert into aa(a,b) values( '1' ,'a');
	insert into aa(a,b) values( '2' ,'a');
	insert into aa(a,b) values( '5' ,'b');
	insert into aa(a,b) values( '6' ,'b');
end;

特別注意

  1. 如果使用如下方法則可能會報錯
<insert id="batchDoubleForeachInsertTest">
	 insert into aa(a,b)
     <foreach close=")" collection="items" item="aa" index="index"  open="(" separator="union">
    		<foreach  collection="aa.items" item="it" index="index"  separator="union">
            		<if test="aa.biz_flow_uuid != null">
            			select #{it,jdbcType=VARCHAR},#{aa.biz_flow_uuid,jdbcType=VARCHAR} from dual
            		</if>
          	</foreach>
     </foreach>
</insert>
  1. 最終拼接成的sql,會在中間少了個union
 insert into aa(a,b)(
	  select '1','a' from dual union
	  select '2','a' from dual 
	  select '5','b' from dual union
	  select '6','b' from dual 
 )
  1. 原因分析
    這種方法最好不要使用,我在實際專案開發中就出現上面的問題,但是在自己搭的專案中有沒復現,具體原因沒有去細查。所以個人建議使用最上面的sql語句是最保險的。