介绍
vo(value object):值对象,又称表现层对象,对应展示界面的数据对象。比如一个前台展示的数据列表,这个列表中的姓名和用户名分别是存在于两个表中的数据,此时数据显示时查询语句用到了多表查询sql,查询出来的数据包含了两个表的字段,此时使用一个JavaBean实体类无法存储返回结果中两个表的数据字段,因此将这两个表中使用到的字段或属性重新封装为一个新的JavaBean,这个JavaBean就叫做vo。通俗来说,vo就是一个自定义的、多个表的属性或字段的集合。
下面需求是一个多级列表的查询
先看字段的设计 通过自定义vo 层 返回数据到前端
下面是用法
@Data
public class ProblemDescriptionVo {
private Integer id;
private String name;
private String text;
//子节点 分类使用的无线递归的方式实现,通过pid 实现
private List<ProblemDescriptionVo> children;
}
controller层
@Autowired
public JyHelpService jyHelpService;
@GetMapping("/helpCenter")
public Object helpCenter(){
//查询所有一级问题信息
//1.查询所有一级 问题信息
List<ProblemDescriptionVo> parentInfo = jyHelpService.findParentInfo();
//2.查询所有二级 问题信息
for (ProblemDescriptionVo parent : parentInfo) {
//根据id 查询二级分类 并将数据存入到 private List<ProblemDescriptionVo> children;
List<ProblemDescriptionVo> children = jyHelpService.findQuestionInfoByParentId(parent.getId());
parent.setChildren(children);
}
return ResponseUtil.ok(parentInfo);
}
<!-- 查询所有 一级分类 -->
<select id="findParentInfo" resultType="com.qqwl.gold.vo.ProblemDescriptionVo">
select `id`,`name`,`text` from jy_help where parent_id = 0 and deleted = 0
</select>
<!-- 查询所有 二级分类 -->
<select id="findChilrent" resultType="com.qqwl.gold.vo.ProblemDescriptionVo">
select `id`,`name`,`text` from jy_help where parent_id = #{id} and deleted = 0
</select>
下面是返回的数据
{
"msg": "成功",
"code": 0,
"data": [
{
"id": 3,
"name": "店铺认证",
"text": null,
"children": [
{
"id": 4,
"name": "店铺问题",
"text": "忘记密码,如何操作\r\n\t1:xxxxx\r\n\t2:xxxxx",
"children": null
},
{
"id": 5,
"name": "店铺问题2",
"text": "忘记密码,如何操作\r\n\t1:xxxxx\r\n\t2:xxxxx",
"children": null
}
]
},
{
"id": 6,
"name": "其他问题",
"text": null,
"children": [
{
"id": 10,
"name": "申述失败",
"text": "申述失败,如何操作\r\n\t1:xxxxx\r\n\t2:xxxxx",
"children": null
},
{
"id": 11,
"name": "退款失败",
"text": "退款失败,如何操作\r\n\t1:xxxxx\r\n\t2:xxxxx",
"children": null
}
]
}
]
}
高质量男性,点歌赞可否