Designing a Session‑Based Scene API with MyBatis: Entities, Mapper, Service, and Controller
This article explains how to model a session that contains multiple scenes in a Java backend, defines corresponding entity classes, creates a MyBatis custom mapper with XML result mapping, and implements service and Spring MVC controller layers to expose the data via a clean API.
1. Business Background
In the business scenario a single session_id can correspond to multiple scene_id and scene_name values.
To avoid repeating the same sessionId in every object, the author proposes moving the session identifier to the outer level.
2. Entity Classes
Two Java POJOs are defined: SceneVO containing a sessionId and a List<SubSceneVO> sceneList , and SubSceneVO holding sceneId and sceneName .
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
}3. Custom Mapper and XML
The MyBatis mapper interface declares a method to fetch a SceneVO by sessionId :
public interface BusinessScenesCustomMapper {
SceneVO selectBySessionId(String sessionId);
}The accompanying XML maps the session_id column to sessionId and uses a <collection> element to populate sceneList from the scene_id and scene_name columns:
<?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" property="sessionId" jdbcType="VARCHAR"/>
<collection property="sceneList" ofType="your.package.vo.SubSceneVO">
<result column="scene_id" property="sceneId" jdbcType="VARCHAR"/>
<result column="scene_name" property="sceneName" jdbcType="VARCHAR"/>
</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>4. Service Layer
An interface defines the business method and its implementation injects the mapper:
public interface SceneService {
/** Get scene information */
SceneVO getScenesInfo(String sessionId);
}
@Service
public class SceneServiceImpl implements SceneService {
@Resource
private BusinessScenesCustomMapper businessScenesCustomMapper;
@Override
public SceneVO getScenesInfo(String sessionId) {
return businessScenesCustomMapper.selectBySessionId(sessionId);
}
}5. Controller Layer
A Spring MVC controller exposes the service through a GET endpoint and wraps the result in a generic response model:
@RestController
public class SceneController {
@Resource
private SceneService sceneService;
@GetMapping("/getScenesInfo")
public ResModel getScenesInfo(String sessionId) {
SceneVO sceneVO = sceneService.getScenesInfo(sessionId);
return ResModel.ok(sceneVO);
}
}Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.