Mybatis resultMap用法之查询返对象中的属性包含 List

版权声明:本文为博主原创文章,如需转载请注明原文出处: https://blog.csdn.net/u010979642/article/details/91045831

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. 运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u010979642/article/details/91045831
今日推荐