IT源码网

Mybatis批量插入数据讲解

lxf 2021年02月13日 编程语言 134 0

1.xml文件

<insert id="addMrqDataBatch" > 
        insert into c_b_mrq_info 
        ( 
        proj_id,templ_rela_id,actual_value,design_value,is_qualified, 
        rsr_id,index_num,templ_rela_det_id,create_time, 
        create_user,img_name,index_group,proj_section_id, 
        part_id,flag,household_no,wdd_id 
        ) 
        values 
        <foreach collection ="bMrqInfo2s" item="bMrqInfo2" index= "index" separator =","> 
            ( 
            #{bMrqInfo2.projectID}, 
            #{bMrqInfo2.relationID}, 
            #{bMrqInfo2.measureValues}, 
            #{bMrqInfo2.designValues}, 
            #{bMrqInfo2.measureResult}, 
            #{bMrqInfo2.measurePlace}, 
            #{bMrqInfo2.measureIndex}, 
            #{bMrqInfo2.standardRelationID}, 
            now(), 
            #{bMrqInfo2.takenBy}, 
            #{bMrqInfo2.measurePhoto}, 
            #{bMrqInfo2.indexGroup}, 
            #{bMrqInfo2.projSectionId}, 
            #{bMrqInfo2.partId}, 
            #{bMrqInfo2.flag}, 
            #{bMrqInfo2.householdNo}, 
            #{bMrqInfo2.dotId} 
            ) 
        </foreach > 
 
    </insert>

2.dao层

Integer addMrqDataBatch(@Param("bMrqInfo2s") List<BMrqInfo2> bMrqInfo2s);

3.实体类

public class BMrqInfo2 { 
 
    private String projectID; 
 
    private String relationID; 
 
    private String measureValues; 
 
    private String designValues; 
 
    private String measureResult; 
 
    private String measurePlace; 
 
    private Integer measureIndex; 
 
    private Integer standardRelationID; 
 
    //private String createTime; 
 
    private String takenBy; 
 
    private String measurePhoto; 
 
    private int indexGroup; 
 
    private Integer projSectionId; 
 
    private Integer partId; 
 
    private    String flag; 
 
    private String householdNo; 
 
    private String dotId; 
 
    public String getProjectID() { 
        return projectID; 
    } 
 
    public void setProjectID(String projectID) { 
        this.projectID = projectID; 
    } 
 
    public String getRelationID() { 
        return relationID; 
    } 
 
    public void setRelationID(String relationID) { 
        this.relationID = relationID; 
    } 
 
    public String getMeasureValues() { 
        return measureValues; 
    } 
 
    public void setMeasureValues(String measureValues) { 
        this.measureValues = measureValues; 
    } 
 
    public String getDesignValues() { 
        return designValues; 
    } 
 
    public void setDesignValues(String designValues) { 
        this.designValues = designValues; 
    } 
 
    public String getMeasureResult() { 
        return measureResult; 
    } 
 
    public void setMeasureResult(String measureResult) { 
        this.measureResult = measureResult; 
    } 
 
    public String getMeasurePlace() { 
        return measurePlace; 
    } 
 
    public void setMeasurePlace(String measurePlace) { 
        this.measurePlace = measurePlace; 
    } 
 
    public Integer getMeasureIndex() { 
        return measureIndex; 
    } 
 
    public void setMeasureIndex(Integer measureIndex) { 
        this.measureIndex = measureIndex; 
    } 
 
    public Integer getStandardRelationID() { 
        return standardRelationID; 
    } 
 
    public void setStandardRelationID(Integer standardRelationID) { 
        this.standardRelationID = standardRelationID; 
    } 
 
    public String getTakenBy() { 
        return takenBy; 
    } 
 
    public void setTakenBy(String takenBy) { 
        this.takenBy = takenBy; 
    } 
 
    public String getMeasurePhoto() { 
        return measurePhoto; 
    } 
 
    public void setMeasurePhoto(String measurePhoto) { 
        this.measurePhoto = measurePhoto; 
    } 
 
    public int getIndexGroup() { 
        return indexGroup; 
    } 
 
    public void setIndexGroup(int indexGroup) { 
        this.indexGroup = indexGroup; 
    } 
 
    public Integer getProjSectionId() { 
        return projSectionId; 
    } 
 
    public void setProjSectionId(Integer projSectionId) { 
        this.projSectionId = projSectionId; 
    } 
 
    public Integer getPartId() { 
        return partId; 
    } 
 
    public void setPartId(Integer partId) { 
        this.partId = partId; 
    } 
 
    public String getFlag() { 
        return flag; 
    } 
 
    public void setFlag(String flag) { 
        this.flag = flag; 
    } 
 
    public String getHouseholdNo() { 
        return householdNo; 
    } 
 
    public void setHouseholdNo(String householdNo) { 
        this.householdNo = householdNo; 
    } 
 
    public String getDotId() { 
        return dotId; 
    } 
 
    public void setDotId(String dotId) { 
        this.dotId = dotId; 
    } 
}

4.service层

public Integer addMrqDataBatch(List<BMrqInfo2> bMrqInfo2s){ 
        return bMrqAssessMapper.addMrqDataBatch(bMrqInfo2s); 
    }

 

public Integer addMrqDataBatch(List<BMrqInfo2> bMrqInfo2s){ 
        return bMrqAssessMapper.addMrqDataBatch(bMrqInfo2s); 
    } 
 
    @Transactional 
    public void  addMeasuredvalue(Map<String, Map<String,BMrqInfo2 >> map,String projId,String sectionId,String flag,String partId){ 
        //先遍历所有二级分类下的实测数据,即遍历groupId级别 
        for (Map.Entry<String, Map<String,BMrqInfo2 >> entry1 : map.entrySet()) { 
            //此处的map2就是item级别的实测数据 
            Map<String, BMrqInfo2> map2 = entry1.getValue(); 
            //将item级别的实测数据只取value值,并生成一个准备插入实测表的list 
            List<BMrqInfo2> result2 = new ArrayList<>(map2.values()); 
            //取一个具体的数据,获取projId,sectionId,flag和partId的值,用于后面调用存储过程来计算合格率 
            BMrqInfo2 bMrqInfo2 = result2.get(0); 
            projId=bMrqInfo2.getProjectID(); 
            sectionId=bMrqInfo2.getProjSectionId().toString(); 
            flag=bMrqInfo2.getFlag(); 
            partId=bMrqInfo2.getPartId().toString(); 
            //将上面生成的实测数据的所有templ_rela_id生成一个字符串集合,用于作为删除下面实测数据的条件 
            List<String> acceptTypeList = result2.stream().map(BMrqInfo2 :: getRelationID).collect(Collectors.toList()); 
            //根据projId和上面的字符串集合 ,先把该项目下templ_rela_id的值在上面结果集的数测数据 删除 
            bMrqAssessMapper.deleteMrqDataByAcceptType(projId,acceptTypeList,partId); 
            //然后进行批量添加操作 
            bMrqAssessMapper.addMrqDataBatch(result2); 
        } 
 
        Map<String, Object> mapCount = new HashMap<>(); 
        mapCount.put("projId", projId); 
        mapCount.put("projSectionId", Integer.parseInt(sectionId)); 
        mapCount.put("flag", flag); 
        mapCount.put("partId", Integer.parseInt(partId)); 
        try { 
            //调用存储过程来计算合格率 
            bMrqAssessMapper.countBMrqInfo2(mapCount); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    }

 

5.controller层

    @RequestMapping(value = "/addMeasuredvalue", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") 
    @ResponseBody 
    public synchronized Info addMeasuredvalue(@RequestBody Map<String, Map<String,BMrqInfo2 >> map, HttpServletRequest request) { 
        //接口埋点 
        InvokeInterface.addInvokeInterfaceData(request,ConstantList.actualMeasurement_code); 
        logger.info("/shiceAction/addMeasuredvalue"); 
        String projId = null; 
        String sectionId = null; 
        String flag = null; 
        String partId = null; 
        Info infos = new Info(); 
        //指定数据库 
        DBContextHolder.setDbType("dataSource2"); 
        bMrqAssessService.addMeasuredvalue(map,projId,sectionId,flag,partId); 
        infos.setCode("1"); 
        infos.setMsg("数据上传成功"); 
        DBContextHolder.clearDbType(); 
        return infos; 
    }

 

发布评论

分享到:

IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

mybatis的标签,标签,标签,标签以及useGeneratedKeys用法讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。