mybatis insert多级List级联类 之(批量插入返回主键集合)使用《示例》下

来源:互联网 发布:短信端口号查询 编辑:程序博客网 时间:2024/06/06 00:49

@Transactionalpublic int notifyStationStats(String arg0, TStationStatsInfo arg1) throws TException {Date now = new Date();Date startTime = null,endTime = null;StationStats sStats=new StationStats();sStats.setStationId(arg1.getStationID());sStats.setStationElectricity(arg1.getStationElectricity());sStats.setUpdateTime(now);sStats.setCreateUser("admin");sStats.setUpdateUser("admin");sStats.setCreateTime(now);try  {     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     startTime = sdf.parse(arg1.getStartTime());   endTime = sdf.parse(arg1.getEndTime());   sStats.setStartTime(startTime);     sStats.setEndTime(endTime);}  catch (ParseException e)  {  log.error(e.getMessage());  }  System.err.println("插入前主键="+sStats.getStationStatsId());stationStatsMapper.insert(sStats);System.err.println("插入后返回主键="+sStats.getStationStatsId());/*** 对推送的设备统计信息执行批量插入*/List<EquipmentStats> esList=new ArrayList<EquipmentStats>();for (int i = 0; i < arg1.getEquipmentStatsInfos().size(); i++) {EquipmentStats es=new EquipmentStats();es.setEquipmentId(arg1.getEquipmentStatsInfos().get(i).getEquipmentID());es.setEquipmentElectricity(arg1.getEquipmentStatsInfos().get(i).getEquipmentElectricity()); es.setTconnectorStatsTinfo(arg1.getEquipmentStatsInfos().get(i).getConnectorStatsInfos());esList.add(es);   }Map<String, Object> emap=new HashMap<String, Object>();emap.put("stationStatsId", sStats.getStationStatsId());emap.put("list",esList);emap.put("startTime", startTime);emap.put("endTime", endTime);emap.put("updateTime", now);emap.put("updateUser", "admin");emap.put("createTime", now);emap.put("createUser", "admin");System.err.println(JSON.toJSONString(esList));System.err.println("设备批量--之前主键:"+esList.get(0).getEquipmentStatsId()+","+esList.get(1).getEquipmentStatsId());equipmentStatsMapper.notifyEquipmentBatch(emap);for (int i = 0; i < esList.size(); i++) {System.err.println("设备批量之后主键:"+esList.get(i).getEquipmentStatsId()+"\n");}System.err.println(JSON.toJSONString(esList));/*** 对推送的接口信息执行批量插入*/List<ConnectorStats> csList =new ArrayList<ConnectorStats>();for (int i = 0; i < esList.size(); i++) {for (int j = 0; j <esList.get(i).getTconnectorStatsTinfo().size(); j++) {ConnectorStats cs =new ConnectorStats();cs.setEquipmentStatsId(esList.get(i).getEquipmentStatsId().intValue());cs.setConnectorId(esList.get(i).getTconnectorStatsTinfo().get(j).getConnectorID());cs.setConnectorElectricity(esList.get(i).getTconnectorStatsTinfo().get(j).getConnectorElectricity());csList.add(cs);}}Map<String, Object> cmap=new HashMap<String, Object>();cmap.put("list",csList);cmap.put("startTime", startTime);cmap.put("endTime", endTime);cmap.put("updateTime", now);cmap.put("updateUser", "admin");cmap.put("createTime", now);cmap.put("createUser", "admin");System.err.println(JSON.toJSONString(csList));connectorStatsMapper.notifyConnectorBatch(cmap);return 1;} <!-- 对推送的设备统计信息执行批量插入 -->  <insert id="notifyEquipmentBatch" parameterType="java.util.Map" keyProperty="equipmentStatsId" useGeneratedKeys="true">insert into equipment_stats ( Equipment_Stats_Id, Org_Id, Station_Stats_Id, Start_Time,       End_Time, Create_User, Create_Time, Update_User, Update_Time, Equipment_Id,Equipment_Electricity)<foreach collection="list" item="item" index="index"separator="UNION ALL">        SELECT #{equipmentStatsId,jdbcType=BIGINT},#{orgId,jdbcType=VARCHAR},      #{stationStatsId,jdbcType=INTEGER},       #{startTime,jdbcType=TIMESTAMP},     #{endTime,jdbcType=TIMESTAMP},      #{createUser,jdbcType=VARCHAR},      #{createTime,jdbcType=TIMESTAMP},       #{updateUser,jdbcType=VARCHAR},      #{updateTime,jdbcType=TIMESTAMP},#{item.equipmentId,jdbcType=VARCHAR},#{item.equipmentElectricity,jdbcType=DOUBLE}FROM DUAL </foreach></insert>

ps:DB三表:station-stats  equipment-stats connector-stats

三class:StationStatsInfo.java   ==》List<TEquipmentStatsInfo> EquipmentStatsInfos; // required

EquipmentStats.java  ==》List<TConnectorStatsInfo> ConnectorStatsInfos; // required

ConnectorStats.java


对应mapper的sql语句


======================================

以下是代码:


ps:

Mybatis批量插入返回自增主键参考地址:http://blog.csdn.net/u014336799/article/details/52023887

1:升级Mybatis版本到3.3.1。

2、在Dao中不能使用@param注解。

2:这个地方接收参数


阅读全文
0 0