栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-Plus一对多项目实战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-Plus一对多项目实战

要求:

查询出来的时候一个资质表中有多个计划事项

实现结果:

思路:

* 先建立好需要的数据库表以及实体类
* 建一个VO 放入List资质台账集合
* 查询资质台账集合
* 查询计划事项集合
* 遍历计划事项集合
* 将计划事项集合添加到资质台账中

具体代码逻辑如下:

@RestController
@RequestMapping("/quAccount")
@Api(value = "资质台账", tags = "资质台账")
public class QuAccountController extends BaseController {
    @Autowired
    private QuAccountService quAccountService;
    @Autowired
    private PlanIssuesService planIssuesService;

    @RequestMapping(value = "/page" , method = RequestMethod.GET)
    @ApiOperation(value = "分页查询", httpMethod = "Get")
    public TableDataInfo pageAll(@RequestBody QuAccountBO.Page quAccountBO){
        startPage(quAccountBO);
        List list = quAccountService.list();
        List quAccountVOS = list.stream().map(this::cover).collect(Collectors.toList());
        for (QuAccountVO quAccountVO : quAccountVOS) {
            LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PlanIssuesPO::getQuAccountId ,quAccountVO.getId());
            List planIssuesPOS = planIssuesService.list(wrapper);
            quAccountVO.setPlanIssuesPOS(planIssuesPOS);
        }
//        bean复制属性后,总条数固定为请求条数,设置total
        TableDataInfo dataTable = getDataTable(quAccountVOS);
        dataTable.setTotal(list.size());
        return dataTable;
    }


    
    public QuAccountVO cover(QuAccountPO quAccountPO) {
        QuAccountVO quAccountVO = new QuAccountVO();
        BeanUtils.copyProperties(quAccountPO,quAccountVO);
        return quAccountVO;
    }

}

查询出来之后,还可以使用Wrapper方法来实现模糊查询

只需要对以上代码稍微改动

BO代码如下:

public class QuAccountBO extends QuAccountPO {

    @Data
    @ApiModel(value = "资质台账id" , description = "根据id显示相应的资质台账数据")
    public static class Page  extends PageRequest{

        private static final long serialVersionUID = 1L;
        
        @ApiModelProperty(value = "id",required = true)
        private Integer id;
        
        @ApiModelProperty(value = "资质类别",required = true)
        private String category;
    }
}

VO代码如下:

@Data
public class QuAccountVO extends QuAccountPO {
    
    private List planIssuesPOS;
}


代码如下:

public TableDataInfo pageAll(@RequestBody QuAccountBO.Page quAccountBO){
        startPage(quAccountBO);
        //根据资质类别模糊查询
        List list = quAccountService.list(
                Wrappers.lambdaQuery()
                .like(StringUtils.isNotEmpty(quAccountBO.getCategory()),QuAccountPO::getCategory,quAccountBO.getCategory())
        );
        List quAccountVOS = list.stream().map(this::cover).collect(Collectors.toList());
        for (QuAccountVO quAccountVO : quAccountVOS) {
            LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PlanIssuesPO::getQuAccountId ,quAccountVO.getId());
            List planIssuesPOS = planIssuesService.list(wrapper);
            quAccountVO.setPlanIssuesPOS(planIssuesPOS);
        }
//        bean复制属性后,总条数固定为请求条数,设置total
        TableDataInfo dataTable = getDataTable(quAccountVOS);
        dataTable.setTotal(list.size());
        return dataTable;
    }

 以上只是自己的写法,具体写法还有很多。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972783.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号