Java批量入庫Demo


java中往數據庫批量插入數據Demo

java代碼:

//入庫數據是需要批量入庫的List
int len =入庫數據.size();
//每次循環10條
int incremnet = 10;
//計算需要尋得次數
int con = len % 10 ==0 ? len /10 : len / 10 +1;

for (int i =0; i < con; i++) {
  //當前條數
  int curr = i * incremnet ;
  //空集合
  List<實體> list = new ArrayList<實體>();
  if (len - curr > incremnet ) {
    //獲取插入的集合
    list = 入庫數據.subList(curr, curr + incremnet );
    //執行插入
    xxdao.add_listxx(list);
    }else{
        //獲取插入的集合
        list = 入庫數據.subList(curr, len);
        //執行插入
        xxdao.add_listxx(list);
      }         
}

dao代碼:

//批量插入dao接口
public int add_listxx(@Param("list") List<實體對象> list);

Mybatis代碼:

<!--  批量插入 -->
<intsert id="add_listxx" useGeneratedKeys="true">
    insert into 表名(name,type,cerator,...)
    values
    <foreach collection="list" separator="," item="i">
    (#{i.name},#{i.type},#{i.cerator}...)
    </foreach>
</insert>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM