service层调用service层的方法,并切换数据库调用不同数据库获取数据

我这边是的业务需求是:从A库查询到部分数据,返回一个结果集,通过这个结果集的某些字段来查询第二个数据库的数据,然后将两个返回的数据封装到一个list集合里面返回出来!(我还做了动态查询我也一块贴出来给大家看!)


第一个接口的数据全部要,我就直接select * 了,你们看自己需求写sql语句,我这边有按团队名称搜索,按状态搜索,按投诉次数搜索,按投诉渠道搜索,按时间段搜索,这个你们根据自己的业务需求来确定!

对应的mapper和service我就不贴了,实体类我也不贴了,实体类就是放你要的所有数据就好!这个不用太多解释!

接下来是service实现类!

 @Autowired
    private ComplaintMapper complaintMapper;
//注入service
 @Autowired
    private PersonnelServiceImpl personnelService;
 //分页搜索总记录数
 //下面这个是注解切换数据源的方式,如果你发现切换没有效果那你把你的事务给拿掉吧
    @DataSource(value = "druid")
    @Override
    public RestResult selectComplaint(Map params) throws Exception {
    //查询的条件,我有点冗余,你们可以不要按照我这样写
        Integer pageNum = Integer.valueOf(String.valueOf(params.get("pageNum")));
        Integer pagesize = Integer.valueOf(String.valueOf(params.get("pagesize")));
        String keyword = String.valueOf(params.get("keyword"));
        String startTime = String.valueOf(params.get("startTime"));
        String endTime = String.valueOf(params.get("endTime"));
        Integer status = Integer.valueOf(String.valueOf(params.get("status")));
        Integer channels = Integer.valueOf(String.valueOf(params.get("channel")));
        String frequency = String.valueOf(params.get("frequency"));
        String TScustomer = String.valueOf(params.get("TScustomer"));
        Integer TechnologyRecruitmentID =Integer.valueOf(String.valueOf(params.get("TechnologyRecruitmentID")));
        Integer PersonnelID = Integer.valueOf(String.valueOf(params.get("PersonnelID")));
        Integer TeamID = Integer.valueOf(String.valueOf(params.get("TeamID")));
        String TeamName = String.valueOf(params.get("TeamName"));
        if (pagesize>=10){
            pagesize = 10;
        }if (pageNum == 1){
            pageNum = 0;
        }else {
            pageNum=(pageNum-1)*pagesize;
        }
        params.put("pageNum",pageNum);
        params.put("pageSize",pagesize);
        params.put("keyword",keyword);
        params.put("status",status);
        if (Strings.isEmpty(startTime)){
            params.put("startTime",startTime);
        }
        if (Strings.isEmpty(endTime)) {
            params.put("endTime", endTime);
        }
        if (channels !=-1){
        params.put("channels",channels);
        }
        if (frequency.isEmpty()) {
            params.put("frequencys", frequency);
        }
        if (Strings.isEmpty(TScustomer)){
            params.put("TScustomer",TScustomer);
        }
        if (Strings.isEmpty(TeamName)){
            params.put("TeamName",TeamName);
        }
        if (TeamID !=-1){
            params.put("TeamID",TeamID);
        }
        if (TechnologyRecruitmentID != -1){
            params.put("TechnologyRecruitmentID",TechnologyRecruitmentID);
        }
        if (PersonnelID != -1 ){
            params.put("PersonnelID",PersonnelID);
        }
        //调用dao层
        List list = complaintMapper.selectComplaintList(params);
        List result = new ArrayList<>();
        for (ComplaintVo vo:list){
            String wangwangnum = vo.getWangwangnum();
            params.put("wangwangnum",wangwangnum);
            ComplaintVo complaintVos = personnelService.selectComplaintListOver(params);
            ComplaintVo complaintVo = new ComplaintVo();
            complaintVo.setScenerestoration(vo.getScenerestoration());
            complaintVo.setStatus(vo.getStatus());
            complaintVo.setCreateTime(vo.getCreateTime());
            complaintVo.setId(vo.getId());
            complaintVo.setChannel(vo.getChannel());
            complaintVo.setComplaintdate(vo.getComplaintdate());
            complaintVo.setDepartment(vo.getDepartment());
            complaintVo.setWorktype(vo.getWorktype());
            complaintVo.setRemarks(vo.getRemarks());
            complaintVo.setPkId(vo.getPkId());
            complaintVo.setIsStop(vo.getIsStop());
            complaintVo.setIsDelete(vo.getIsDelete());
            complaintVo.setFrequency(vo.getFrequency());
            complaintVo.setContent(vo.getContent());
            complaintVo.setWangwangnum(vo.getWangwangnum());
            //判断第二个接口是否有数据,有数据则set进去没有就不执行
            if (complaintVos !=null) {
            complaintVo.setTechnologyrecruitmentid(complaintVos.getTechnologyrecruitmentid());
            complaintVo.setShopptype(complaintVos.getShopptype());
            complaintVo.setPersonnelid(complaintVos.getPersonnelid());
            complaintVo.setTscustomer(complaintVos.getTscustomer());
            complaintVo.setTeamid(complaintVos.getTeamid());
            complaintVo.setTeamname(complaintVos.getTeamname());
            complaintVo.setUsername(complaintVos.getUsername());
            complaintVo.setPname(complaintVos.getPname());
            complaintVo.setTename(complaintVos.getTename());
            result.add(complaintVo);
            }
        }
        //总数量
        Integer total = complaintMapper.selectComplaintCount(params);
        Map map = new HashMap();
        map.put("list",result);
        map.put("total",total);
        return new RestResultBuilder().setCode(0).setMsg("请求成功").setData(map).build();
    }    

上面要调用的service的xml文件请看下面

这个结果集我就不贴了,和上面那个是一样的!自己封装!

我这是sqlserver的查询!sqlserver好像不支持动态查询,我这是mybatis,是支持的!为了避免不采坑,我建议动态查询的条件你们全部写成字段名!

 

接下来是service的实现类:

//切换数据源,这个接口的数据全是从sqlserver出来的,至于怎么切换数据源,看我另外的博客吧!
 @DataSource(value = "slave1")
    @Override
    public ComplaintVo selectComplaintListOver(Map map) {
        return personnelMapper.selectComplaintListOver(map);
    }

代码就这样,不明白的联系博主simon

你可能感兴趣的:(service层调用service层的方法,并切换数据库调用不同数据库获取数据)