Skip to content

6.resultMap结果集处理

FuriousPws002 edited this page Apr 15, 2024 · 2 revisions

代码分支:06-result-set-handler-with-result-map

使用ResultMapping封装resultMap映射信息,其中nestedResultMapId字段表示依赖的嵌套resultMap的id,若一个resultMap包含嵌套resultMap配置,添加mapper配置时,会先添加嵌套的resultMap到Configuration的resultMaps中,再用nestedResultMapId去关联。添加和解析嵌套resultMap都用了递归的方法,调用链如下所示

添加嵌套配置

XMLMapperBuilder.resultMapElement(XNode)  (org.apache.ibatis.builder.xml)
	XMLMapperBuilder.resultMapElement(XNode, List<ResultMapping>, Class<?>)  (org.apache.ibatis.builder.xml)
	    XMLMapperBuilder.buildResultMappingFromContext(XNode, Class<?>)  (org.apache.ibatis.builder.xml)
	        XMLMapperBuilder.processNestedResultMappings(XNode, List<ResultMapping>, Class<?>)  (org.apache.ibatis.builder.xml)
	            XMLMapperBuilder.resultMapElement(XNode, List<ResultMapping>, Class<?>)  (org.apache.ibatis.builder.xml)

解析嵌套配置

DefaultResultSetHandler.handleRowValuesForNestedResultMap(ResultSetWrapper, ResultMap,List<Object>)  (org.apache.ibatis.executor.resultset)
	DefaultResultSetHandler.getRowValue(ResultSetWrapper, ResultMap, String, Object, Map<String, Object>)  (org.apache.ibatis.executor.resultset)
	    DefaultResultSetHandler.applyNestedResultMappings(ResultSetWrapper, ResultMap, Object, Map<String, Object>, String)(2 usages)  (
	        DefaultResultSetHandler.getRowValue(ResultSetWrapper, ResultMap, String, Object, Map<String, Object>)  (org.apache.ibatis.executor.resultset)

新增表如下

CREATE TABLE `car` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT NULL,
  `color` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `user_car_relation` (
  `user_id` bigint NOT NULL,
  `car_id` bigint NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

单元测试:

初始化数据

INSERT INTO user (id, name, age, birth) VALUES (1, 'Jack', 18, '2024-03-29 22:09:07');
INSERT INTO user (id, name, age, birth) VALUES (2, 'Tom', 20, '2024-03-29 22:09:07');
INSERT INTO user (id, name, age, birth) VALUES (3, 'Alice', 23, '2024-03-29 22:09:07');
INSERT INTO user (id, name, age, birth) VALUES (4, 'Sam', 20, '2024-03-29 22:09:07');

INSERT INTO car (id, name, color) VALUES (1, 'Porsche', 'white');
INSERT INTO car (id, name, color) VALUES (2, 'Bentley', 'black');
INSERT INTO car (id, name, color) VALUES (3, 'Lamborghini', 'gree');

INSERT INTO user_car_relation (user_id, car_id) VALUES (1, 1);
INSERT INTO user_car_relation (user_id, car_id) VALUES (1, 2);
INSERT INTO user_car_relation (user_id, car_id) VALUES (2, 1);
INSERT INTO user_car_relation (user_id, car_id) VALUES (3, 2);
INSERT INTO user_car_relation (user_id, car_id) VALUES (3, 3);

测试类

public class SqlSessionTest {
    /**
     * 查询简单resultMap映射
     */
    @Test
    public void queryResultMap() {
        Configuration configuration = new Configuration();
        configuration.setDataSource(DataSourceBuilderTest.build());
        configuration.addMapper(UserMapper.class);
        SqlSession sqlSession = new DefaultSqlSession(configuration);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<UserDO> list = userMapper.selectResultMap();
        Assert.assertNotNull(list);
        UserDO userDO = list.get(0);
        Assert.assertNotNull(userDO.getNamex());
    }

    /**
     * 查询嵌套resultMap映射
     */
    @Test
    public void queryNestedResultMap() {
        Configuration configuration = new Configuration();
        configuration.setDataSource(DataSourceBuilderTest.build());
        configuration.addMapper(UserMapper.class);
        SqlSession sqlSession = new DefaultSqlSession(configuration);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<UserDTO> list = userMapper.selectNestedResultMap();
        Assert.assertNotNull(list);
        UserDTO user = list.get(0);
        Assert.assertNotNull(user.getName());
        Assert.assertNotNull(user.getCarList());
    }

}
Clone this wiki locally