面试官:有一个 List 对象集合,如何优雅地返回给前端?我懵了。

eca50cceb7ab980f005cb96befad562c.jpeg

1.业务背景

业务场景中,一个会话中存在多个场景,即一个session_id对应多个scene_id和scene_name

如果你写成如下的聚合模型类

public  class SceneVO {
  private String sessionId;
  private String sceneId;
  private String sceneName;
  // 省略对应的getter和setter方法
}

返回的List<SceneVO>形式如下,这个数据在data属性中

{
&nbsp; "data":[
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp; "sessionId":&nbsp; "jksadhjksd",
&nbsp;&nbsp;&nbsp; "sceneId": "NDJWKSDSJKDKED",
&nbsp;&nbsp;&nbsp; "sceneName": "场景1"
&nbsp;&nbsp;},
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp; "sessionId":&nbsp; "jksadhjksd",
&nbsp;&nbsp;&nbsp; "sceneId": "KLJSDJKLSDFALK",
&nbsp;&nbsp;&nbsp; "sceneName": "场景2"
&nbsp;&nbsp;},
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp; "sessionId":&nbsp; "jksadhjksd",
&nbsp;&nbsp;&nbsp; "sceneId": "KERFJKOVDJKDSS",
&nbsp;&nbsp;&nbsp; "sceneName": "场景3"
&nbsp;&nbsp;}
&nbsp;]
}

每个对象里面都带上了重复的一个sessionId数据,我想提出来该怎么办?

我想改为如下形式,sessionId提出到外层,更能体现出一个sessionId对应多个sceneId和sceneName的含义,这样也便于前端取数据,不然每个对象都要增加一个sessionId属性,太麻烦。

{
&nbsp; "data":&nbsp;{
&nbsp;&nbsp; "sessionId":&nbsp; "jksadhjksd",
&nbsp;&nbsp; "sceneList":&nbsp;[
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp; "sceneId": "NDJWKSDSJKDKED",
&nbsp;&nbsp;&nbsp;&nbsp; "sceneName": "场景1"
&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp; "sceneId": "KLJSDJKLSDFALK",
&nbsp;&nbsp;&nbsp;&nbsp; "sceneName": "场景2"
&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp; "sceneId": "KERFJKOVDJKDSS",
&nbsp;&nbsp;&nbsp;&nbsp; "sceneName": "场景3"
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;]
&nbsp;}
}

2.实体类

方法如下,首先创建两个实体类。

public&nbsp; class&nbsp;SceneVO&nbsp;{
&nbsp; private&nbsp;String&nbsp;sessionId;
&nbsp; private&nbsp;List<SubSceneVO>&nbsp;sceneList;
&nbsp; //&nbsp;省略对应的getter和setter方法
}
public&nbsp; class&nbsp;SubSceneVO&nbsp;{
&nbsp; private&nbsp;String&nbsp;sceneId;
&nbsp; private&nbsp;String&nbsp;sceneName;
&nbsp; //&nbsp;省略对应的getter和setter方法
}

3.自定义Mapper和xml文件

public&nbsp; interface&nbsp;BusinessScenesCustomMapper&nbsp;{
&nbsp; SceneVO&nbsp;selectBySessionId(String&nbsp;sessionId);
}
<?xml&nbsp;version= "1.0"&nbsp;encoding= "UTF-8"?>
<!DOCTYPE&nbsp;mapper&nbsp;PUBLIC&nbsp; "-//mybatis.org//DTD&nbsp;Mapper&nbsp;3.0//EN"&nbsp; "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper&nbsp;namespace= "你的包名.mapper.BusinessScenesCustomMapper">
&nbsp;&nbsp;&nbsp;&nbsp;<resultMap&nbsp;id= "BaseResultMap"&nbsp;type= "你的包名.vo.SceneVO">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<result&nbsp;column= "session_id"&nbsp;jdbcType= "VARCHAR"&nbsp;property= "sessionId"/>
&nbsp;&nbsp;&nbsp;&nbsp;<!--
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; collection 标签:用于定义关联的list集合类型的封装规则
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; property:对应父类中list属性名,这里SceneVO类里的List变量名为sceneList
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofType:集合存放的类型,List集合要装的类的类名,这里是SubSceneVO
&nbsp;&nbsp;&nbsp;&nbsp;-->
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<collection&nbsp;property= "sceneList"&nbsp;ofType= "你的包名.vo.SubSceneVO">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<result&nbsp;column= "scene_id"&nbsp;jdbcType= "VARCHAR"&nbsp;property= "sceneId"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<result&nbsp;column= "scene_name"&nbsp;jdbcType= "VARCHAR"&nbsp;property= "sceneName"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</collection>
&nbsp;&nbsp;&nbsp;&nbsp;</resultMap>

<!--&nbsp;一个session_id对应多条记录,返回的是SceneVO对象,SceneVO对象有一个List装着SubSceneVO&nbsp;&nbsp;-->
&nbsp;&nbsp;&nbsp;&nbsp;<select&nbsp;id= "selectBySessionId"&nbsp;parameterType= "string"&nbsp;resultMap= "BaseResultMap">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;session_id,&nbsp;scene_id,&nbsp;scene_name
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;表名
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;session_id&nbsp;=&nbsp;#{sessionId,jdbcType=VARCHAR}
&nbsp;&nbsp;&nbsp;&nbsp;</select>
</mapper>
  • collection标签: 用于定义关联的List集合类型的封装规则
  • property属性: 对应父类中List集合的变量名,这里SceneVO类里的List变量名为sceneList
  • ofType属性: 集合存放的类型,List集合要装的类的类名,这里是SubSceneVO

4.Service层

public&nbsp; interface&nbsp;SceneService&nbsp;{
&nbsp; /**
&nbsp;*&nbsp;获取场景信息
&nbsp;*/

&nbsp; SceneVO&nbsp;getScenesInfo(String&nbsp;sessionId);
}
@Service
public&nbsp; class&nbsp;SceneServiceImpl&nbsp;{

&nbsp; @Resource
&nbsp; private&nbsp;BusinessScenesCustomMapper&nbsp;businessScenesCustomMapper;
&nbsp;......
&nbsp; public&nbsp;SceneVO&nbsp;getScenesInfo(String&nbsp;sessionId)&nbsp;{
&nbsp;&nbsp; return&nbsp;businessScenesCustomMapper.selectBySessionId(sessionId);
&nbsp;}
}

5.Controller层

......
@Resource
private&nbsp;SceneService&nbsp;sceneService;

@GetMapping( "/getScenesInfo")
public&nbsp;ResModel&nbsp;getScenesInfo(String&nbsp;sessionId)&nbsp;{
&nbsp;SceneVO&nbsp;sceneVO&nbsp;=&nbsp;sceneService.getScenesInfo(sessionId);
&nbsp; return&nbsp;ResModel.ok(sceneVO);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值