關於java+mybatis+orcle批量插入數據的學習

最近在操作orcle時候爲了提升效率,需要進行批量插入的問題 ,
學習了一下關於這方面內容。 我需要插入的數據格式如下:

**內容:**  String str= [{"appid":"10000","deploy":"Lotus"},{"appid":"10001","deploy":"Lotus+PAFA5+Apollo"}]
**java代碼:**
	
	  JSONObject jsonArray=JSONObject.parseArray(str);
		 List <PageData> list= new ArrayList<PageData>;
		  for(int i=0;i<jsonArray.size();i++){
		  JSONObject  jsonObject==jsonArray.getJSONObject(i);
			Itearator it=jsonObject.entrySet.itearator();
			PageData pd=new PageData();//自定義對象,實際實現hashmap
			while(it.hashNext()){
			Entry<String,Object> entry=  (Entry<String,Object>)it.next();
			pd.put(entry.getKey(),entry.getValue());
			 list.add(pd);
			//開始批量插入:一次插入500條
			 if(i+1)%500==0){ 
	           dao.batchSave("****Mapper.方法名",list);
	           list.clear(); //一定要清空哦,要不然數據會重複
		     }
		 }
		  dao.batchSave("****Mapper.方法名",list)
}

**mybatis代碼:**
<insert id="" parameterType="java.util.List" useGeneratedKeys="false">
	 insert into Stuent 
	   ( 
	     id,name
	  )
  <foreach collection="list" item="item" index="index" open="(" close=")" separator=" union all">
	  select 
	     #{item.id} "id",
	      #{item.name} "name"
	   from dual   
</foreach>

</insert>
代碼到此結束了,實現了批量插入,手動敲代碼 單詞可能錯誤

--------解釋—
insert into 表名 (字段1)
select ‘1’ from dual
union
select ‘2’ from dual
那麼這一次就插入了兩條數據

                                                 																2020.5.28  
                                                 																       doc by :    kang.du
                                                 											代碼每天學習一點,薪資也會高一點
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章