当返回前端的数据中存在List对象集合,如何优雅操作?

文章目录

1.业务背景

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

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

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

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

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

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

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

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

2.实体类

方法如下:

首先创建两个实体类

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

3.自定义Mapper和xml文件

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

<!-- 一个session_id对应多条记录,返回的是SceneVO对象,SceneVO对象有一个List装着SubSceneVO -->
    <select id="selectBySessionId" parameterType="string" resultMap="BaseResultMap">
        select session_id, scene_id, scene_name
        from 表名
        where session_id = #{sessionId,jdbcType=VARCHAR}
    </select>
</mapper>

collection标签:用于定义关联的List集合类型的封装规则
property属性:对应父类中List集合的变量名,这里SceneVO类里的List变量名为sceneList
ofType属性:集合存放的类型,List集合要装的类的类名,这里是SubSceneVO

4.Service层

public interface SceneService { 
	/** * 获取场景信息 */
	SceneVO getScenesInfo(String sessionId);
}
@Service
public class SceneServiceImpl { 

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

5.Controller层

......
@Resource
private SceneService sceneService;

@GetMapping("/getScenesInfo")
public ResModel getScenesInfo(String sessionId) { 
	SceneVO sceneVO = sceneService.getScenesInfo(sessionId);
	return ResModel.ok(sceneVO);
}

欢迎一键三连~

有问题请留言,大家一起探讨学习

———————-Talk is cheap, show me the code———————–

    原文作者:砖业洋__
    原文地址: https://blog.csdn.net/qq_34115899/article/details/118159196
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞