Structuring Session‑Based Scene Data with MyBatis in Java
This article explains how to redesign a JSON payload that repeats a sessionId for each scene by moving the sessionId to the outer level and using MyBatis resultMap with a collection to map a Session object containing a list of Scene objects, including Java entity definitions, mapper XML, service, and controller code.
In a typical business scenario a single sessionId can correspond to multiple sceneId / sceneName pairs, but the original JSON returned a List<SceneVO> where each element duplicated the same sessionId.
To make the data structure clearer for the front‑end, the sessionId is extracted to the outer level and the scenes are placed inside a sceneList array.
{
"data": {
"sessionId": "jksadhjksd",
"sceneList": [
{"sceneId":"NDJWKSDSJKDKED","sceneName":"场景1"},
{"sceneId":"KLJSDJKLSDFALK","sceneName":"场景2"},
{"sceneId":"KERFJKOVDJKDSS","sceneName":"场景3"}
]
}
}The Java entity classes are defined as follows:
public class SceneVO {
private String sessionId;
private List<SubSceneVO> sceneList;
// getters and setters omitted
}
public class SubSceneVO {
private String sceneId;
private String sceneName;
// getters and setters omitted
}A custom MyBatis mapper interface and its XML mapping handle the one‑to‑many relationship:
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="your.package.mapper.BusinessScenesCustomMapper">
<resultMap id="BaseResultMap" type="your.package.vo.SceneVO">
<result column="session_id" jdbcType="VARCHAR" property="sessionId"/>
<!-- collection defines the List<SubSceneVO> mapping -->
<collection property="sceneList" ofType="your.package.vo.SubSceneVO">
<result column="scene_id" jdbcType="VARCHAR" property="sceneId"/>
<result column="scene_name" jdbcType="VARCHAR" property="sceneName"/>
</collection>
</resultMap>
<select id="selectBySessionId" parameterType="string" resultMap="BaseResultMap">
SELECT session_id, scene_id, scene_name
FROM your_table
WHERE session_id = #{sessionId,jdbcType=VARCHAR}
</select>
</mapper>The service layer simply delegates to the mapper:
public interface SceneService {
/** Get scene information for a given session */
SceneVO getScenesInfo(String sessionId);
}
@Service
public class SceneServiceImpl implements SceneService {
@Resource
private BusinessScenesCustomMapper businessScenesCustomMapper;
@Override
public SceneVO getScenesInfo(String sessionId) {
return businessScenesCustomMapper.selectBySessionId(sessionId);
}
}Finally, a REST controller exposes the data through an endpoint:
@RestController
public class SceneController {
@Resource
private SceneService sceneService;
@GetMapping("/getScenesInfo")
public ResModel getScenesInfo(String sessionId) {
SceneVO sceneVO = sceneService.getScenesInfo(sessionId);
return ResModel.ok(sceneVO);
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
