MyBatis ResultMap用法之查询返对象中的属性包含List

warning: 这篇文章距离上次修改已过1082天,其中的内容可能已经有所变动。

文章来源:http://www.freesion.com/article/906343800/

1. 实体类

1.1 字典类

@Data
public class DictDTO {
    /**
     * 字典代码
     * */
    private String dictCode;

    /**
     * 字典名称
     * */
    private String dictName;

    /**
     * 字典项列表
     * */
    private List<DictItemDTO> dictItems;
}

1.2 字典子项类

@Data
public class DictItemDTO {

    /**
     * 字典子项代码
     * */
    private String dictItemCode;

    /**
     * 字典子项展示值
     * */
    private String dictItemValue;

    /**
     * 字典子项详细描述
     * */
    private String dictItemDesc;
}

2. DICTMAPPER.XML 文件

<?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="com.answer.ai.mapper.DictMapper" >

    <resultMap id="DictResultMap" type="com.answer.ai.entity.dto.DictDTO">
        <result column="dict_code" property="dictCode" jdbcType="VARCHAR"/>
        <result column="dict_name" property="dictName" jdbcType="VARCHAR"/>
        
        <!-- collection 标签需放在最后 -->
        <collection property="dictItems" resultMap="DictItemsMap"/>
    </resultMap>

    <resultMap id="DictItemsMap" type="com.answer.ai.entity.dto.DictItemDTO">
        <result column="dict_item_code" property="dictItemCode" jdbcType="VARCHAR"/>
        <result column="dict_item_value" property="dictItemValue" jdbcType="VARCHAR"/>
        <result column="dict_item_desc" property="dictItemDesc" jdbcType="VARCHAR"/>
    </resultMap>
    
    <select id="findRecordsByParamsPage" resultMap="DictResultMap">
        SELECT dd.dict_code, dd.dict_name, ddi.dict_item_code, ddi.dict_item_value, ddi.dict_item_desc
        FROM ai_dict dd
        LEFT JOIN ai_dict_item ddi ON dd.id = ddi.dict_id
        WHERE dd.status = 1 AND ddi.status = 1
        ORDER BY dd.id
    </select>

</mapper>

3. 数据库表

3.1 AI_DICT 表

CREATE TABLE `ai_dict` (
  `id` bigint(18) NOT NULL AUTO_INCREMENT COMMENT '字典ID',
  `dict_code` varchar(20) NOT NULL COMMENT '字典代码',
  `dict_name` varchar(35) NOT NULL COMMENT '字典名称',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态\r\n0: 停用\r\n1: 启用',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='字典表';

3.2 AI_DICT_ITEM 表

CREATE TABLE `ai_dict_item` (
  `id` bigint(18) NOT NULL AUTO_INCREMENT COMMENT '字典子项id',
  `dict_id` bigint(18) NOT NULL COMMENT '字典ID',
  `dict_item_code` varchar(35) NOT NULL COMMENT '字典子项代码',
  `dict_item_value` varchar(35) DEFAULT NULL COMMENT '字典子项展示值',
  `dict_item_desc` varchar(55) NOT NULL DEFAULT '' COMMENT '字典子项描述',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态\r\n0: 停用\r\n1: 启用',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 COMMENT='字典子项表';

4. 运行结果

注意事项:

1.所有的column属性必须和实体类中的字段对应包括大小写(这个坑会报错,也很容易看出来哪里出问题了)

2.注意返回的是resultMap而不是resultType!

添加新评论