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

@Transactional
public 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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值