关于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
                                                 											代码每天学习一点,薪资也会高一点
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章