Java實現 調用JdbcTemplate.batchUpdate方法批次更新__Java
來源:互聯網
上載者:User
1、準備參數 private List<Object[]> preparParam(IDataList ret) {
// 準備入參
List<Object[]> paramList = new ArrayList<>() ;
for ( int i = 0 ; i < ret.size() ; i++) {
IData detail = ret.getData(i) ;
String[] arr = new String[ 6] ;
arr[ 0] = detail.getString( "orderDetailId") ;
arr[ 1] = detail.getString( "clientId") ;
arr[ 2] = detail.getString( "foodId") ;
arr[ 3] = detail.getString( "foodName") ;
arr[ 4] = detail.getString( "pictureUrl") ;
arr[ 5] = detail.getString( "num") ;
paramList.add(arr) ;
}
return paramList ; } 2、調用Dao層方法 private void insertIntoTempTable(IData param , IDataList ret) throws Exception {
if(Common. isNull(param.getString( "clientId"))) {
throw new CustomException( "餐機ID不可為空白") ;
}
if(ret.size() <= 0) {
throw new CustomException( "本餐機沒有您可取的餐品") ;
}
// 根據param的餐機ID,刪除本餐機的正在出貨記錄
orderDao.deleteByClientId(param) ;
// 插入本次的正在出貨記錄
orderDao.insertShipment(preparParam(ret)) ; } 3、Dao層方法實現 public void insertShipment(List<Object[]> params) {
String sql = "INSERT INTO mp_client_shipment ( \n " +
" \t order_detail_id, \n " +
" \t client_id, \n " +
" \t food_id, \n " +
" \t food_name, \n " +
" \t url, \n " +
" \t num, \n " +
" \t created \n " +
") \n " +
"VALUES \n " +
" \t (?,?,?,?,?,?,now())" ;
jdbcTemplate.batchUpdate(sql , params) ;
}