亲宝软件园·资讯

展开

SpringBoot 批量插入Oracle

沈麽鬼​​​​​​​ 人气:0

前端数据

有如下需求,前端提交一个对象cabinData,保存到数据表中,对象结构如下:

 {
     "shipId":"424",
     "shipName":"大唐2号",
     "ballastCabinData":["艏尖舱","双层底1左","双层底1右","双层底2左","双层底2右","双层底3左","双层底3右","双层底4左","双层底4右","双层底5左","双层底5右","顶边5左","顶边5右","尾尖舱"],
     "freshCabinData":["淡水舱1左","淡水舱1右"]
 }

数据表结构

数据表是oracle,表结构为:

后端Controller:

 @PostMapping("/savecabin")
 public AjaxResult savecabin(@RequestBody Map<String,Object> postCabinData){
 ​
     // 获取map中的参数
     String shipIdStr = (String) postCabinData.get("shipId");
     Integer shipId = Integer.valueOf(shipIdStr);
     String shipName = (String) postCabinData.get("shipName");
     List<String> ballastCabinData = (List<String>) postCabinData.get("ballastCabinData");
     List<String> freshCabinData = (List<String>) postCabinData.get("freshCabinData");
 ​
     // 获取shipid下的所有的船舱记录
     List<DraftCabin> list = draftCabinService.selectCabinByShipId(shipId);
     // 船舱记录大于0时,执行批量删除;船舱
     if (list.size()>0){
         List<Long> idcards= list.stream().map(DraftCabin::getCabinId).collect(Collectors.toList());
         Long[] ids = idcards.toArray(new Long[idcards.size()]);
         draftCabinService.deleteDraftCabinByIds(ids);
     }
     // 批量插入船舱
     draftCabinService.insertDraftCabins(ballastCabinData,shipId,"压舱水",shipName);
     draftCabinService.insertDraftCabins(freshCabinData,shipId,"淡水",shipName);
 ​
     return AjaxResult.success();
 }

解析:

mapper

 public int insertDraftCabins(@Param("DraftCabin")List DraftCabin, @Param("shipId")Integer shipId, @Param("cabinType")String cabinType,@Param("shipName")String shipName) ;

解析:

mapper中,多个参数使用@Param注解来接收,在mybaits的xml中,调用注解名称。

xml

 <!--批量新增-->
 <insert id="insertDraftCabins" useGeneratedKeys="false">
     INSERT ALL
     <foreach collection="DraftCabin" item="item" index="index">
         INTO draft_cabin
         (cabin_name,ship_id,cabin_type,ship_name)
         values(#{item},#{shipId},#{cabinType},#{shipName,jdbcType=VARCHAR})
     </foreach>
     SELECT 1 FROM DUAL
 </insert>

解析:

orcale的批量插入方法与mysql有很大区别,这里需要注意以下:

 insert all
     into <tableName>[(<table_column1>,<table_column2>...)] 
     values([<column_value1>,<column_value2>...]) 
     [into <tableName>[(<table_column1>,<table_column2>...)] 
     values([<column_value1>,<column_value2>...])]...
 select  <table_value1>[,<table_value2>...] from dual;

加载全部内容

相关教程
猜你喜欢
用户评论