接口优化:在for循环里反复执行sql的场景

1. 当接口返回的字段数量较多的情况下,没办法通过一个主sql获取所有的字段,需要通过主sql获取部分字段的值,再通过主sql获取的结果集作为查询条件,将其他的字段也查询出来。

2.这时候,如果项目里已经有了包含我们接下来要查询的字段的实体类以及Dao.xml,Dao.xml里写sql,mapper接口里写对应的方法声明,service类里写对应的实现;

1)Dao.xml

<select id="getdSCUserCode" parameterType="java.util.List" resultMap="BaseResultMap">
        select
            mainno, dbname, usercode, id
        from prplbpmmain
        where (dbname,mainno) in
        <foreach collection="registNoAndDBNames" item="item" index="item" open="(" separator="," close=")">
            (#{item.DBName},#{item.registNo})
        </foreach>
        and nodeid = 500
        and valid = '1'
        and cancelstate = '0'
    </select>

 2)Dao接口

List<PrpLbpmMain> getdSCUserCode(@Param("registNoAndDBNames") List<RegistNoAndDBName> registNoAndDBNames);

3)Service

List<PrpLbpmMain> HXprpLbpmMains = prpLbpmMainDao.getdHXUserCode(registNoAndDBNames);
HXprpLbpmMains = new ArrayList<>(new HashSet<>(HXprpLbpmMains));
Map<String, PrpLbpmMain> HXprpLbpmMainsMap = (HashMap<String, PrpLbpmMain>) HXprpLbpmMains.stream().collect(
        Collectors.toMap(
                   PrpLbpmMain -> PrpLbpmMain.getMainno() + PrpLbpmMain.getDBName(),
                                Function.identity(),
                                (key1, key2) -> key1
               )
);



if(HXprpLbpmMainsMap!=null && HXprpLbpmMainsMap.size()>0) {
      PrpLbpmMain HXprpLbpmMain = HXprpLbpmMainsMap.get(bpmId + dbname);
      if (HXprpLbpmMain != null) {
          rswjExcelDataVo.setHXuserCode(HXprpLbpmMain.getUserCode());
        }
}

3. 如果我们获取的字段没有对应的实体类,那么需要可任意考虑建一个实体类,需要获取的几个字段为成员变量,写获取字段的sql,将主sql获取的结果集中的部分数据拼成String,并作为查询条件来使用:

public class DSRWCount {
    private String registNo;
    private String DBName;
    private int count;
}


List<String> registNoList = RSWJExcelDataList1.stream().map(RSWJExcelDataVo::getRegistNo)
.collect(Collectors.toList());
 
registNoList = new ArrayList<>(new HashSet<>(registNoList));

List<String> dbnameList = RSWJExcelDataList1.stream().map(RSWJExcelDataVo::getDBName).
collect(Collectors.toList());
                
dbnameList = new ArrayList<>(new HashSet<>(dbnameList));



StringBuffer stringBuffer3 = new StringBuffer();
                stringBuffer3.append("select registno, dbname, count(*) from prpldeflossmain where   ");
                if (registNoList.size() > 0) {
                    String registStr = registNoList.stream().collect(Collectors.joining("','"));
                    stringBuffer3.append(" registno in ( '").append(registStr).append("' ) ");
                }

                if (dbnameList.size() > 0) {
                    String dbnameStr = dbnameList.stream().collect(Collectors.joining("','"));
                    stringBuffer3.append(" AND dbname in ( '").append(dbnameStr).append("' )  ");
                }

                if (businessIdList.size() > 0) {
                    String businessIdStr = businessIdList.stream().collect(Collectors.joining("','"));
                    stringBuffer3.append(" AND id in ( '").append(businessIdStr).append("' ) group by id, registno, dbname ");
                }

                params5.put("sql", stringBuffer3);

                List<DSRWCount> dsrwCountList = rswjExcelDataMapper.getdsrwcount(params5);
                dsrwCountList = new ArrayList<>(new HashSet<>(dsrwCountList));

                Map<String, DSRWCount> dsrwCountMap = new HashMap<>();
                dsrwCountMap = dsrwCountList.stream().collect(
                        Collectors.toMap(
                                DSRWCount -> DSRWCount.getRegistNo() + DSRWCount.getDBName(),
                                Function.identity(),
                                (key1, key2) -> key1
                        )
                );



if(dswcCountMap!=null && dswcCountMap.size()>0 && dsrwCountMap!=null && dsrwCountMap.size()>0){
                        DSWCCount dswcCount = dswcCountMap.get(registNo + dbname);
                        DSRWCount dsrwCount = dsrwCountMap.get(registNo + dbname);
                        if(dswcCount!=null && dsrwCount!=null) {
                            if (dswcCount.getCount() == dsrwCount.getCount() && dsrwCount.getCount() > 0) {
                                rswjExcelDataVo.setEndlossflag("是");
                            } else {
                                rswjExcelDataVo.setEndlossflag("否");
                            }
                        }
                    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值