Backend Development 5 min read

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.

Architecture Digest
Architecture Digest
Architecture Digest
Structuring Session‑Based Scene Data with MyBatis in Java

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
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);
}
SELECT session_id, scene_id, scene_name
        FROM your_table
        WHERE session_id = #{sessionId,jdbcType=VARCHAR}

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);
    }
}
backendJavaMyBatisControllerServiceMapper
Architecture Digest
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.