• MyBatis结果集一对多映射


    MyBatis结果集一对多映射

    需求:重画二维码配置类,根据sizeCode将查询出来的imageCode分组。

    DROP TABLE IF EXISTS `size_code`;
    CREATE TABLE `size_code` (
      `sizeCode` int(11) DEFAULT NULL,
      `sizeCodeName` varchar(255) DEFAULT NULL,
      `imageCode` int(11) DEFAULT NULL,
      `imageCodeName` varchar(255) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    -- ----------------------------
    -- 这里特意将sizeCode相同时,sizeCodeName为了测试设置了不同的值。(本来应该是一致的)
    -- ----------------------------
    INSERT INTO `size_code` VALUES ('1', '15X19', '1', '红色');
    INSERT INTO `size_code` VALUES ('1', '15X18', '2', '橙色');
    INSERT INTO `size_code` VALUES ('2', '12X12', '1', '古风');
    
    <?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.mozq.mb.mb01.mapper.SizeCodeMapper">
    
        <select id="find" resultType="com.mozq.mb.mb01.pojo.SizeCode">
            select * from size_code
        </select>
    
        <!--没有id属性时,result的所有列,(sizeCode,sizeCodeName)的值都不同会创建一个对象,值相同的则将结果封装到imageCodeList-->
        <resultMap id="SizeCodeImageCodeMap" type="com.mozq.mb.mb01.pojo.SizeCode">
            <result column="sizeCode" property="sizeCode" />
            <result column="sizeCodeName" property="sizeCodeName" />
            <collection property="imageCodeList" ofType="com.mozq.mb.mb01.pojo.ImageCode">
                <result column="imageCode" property="imageCode" />
                <result column="imageCodeName" property="imageCodeName" />
            </collection>
        </resultMap>
        <select id="findDetail" resultMap="SizeCodeImageCodeMap">
            select * from size_code
        </select>
    
        <!--有id属性时,id属性不同,则创建一个单独的对象,其余属性取的时查询出来第1条记录的属性,id相同的记录都封装到imageCodeList -->
        <resultMap id="SizeCodeImageCodeMap2" type="com.mozq.mb.mb01.pojo.SizeCode">
            <id column="sizeCode" property="sizeCode" />
            <result column="sizeCodeName" property="sizeCodeName" />
            <collection property="imageCodeList" ofType="com.mozq.mb.mb01.pojo.ImageCode">
                <result column="imageCode" property="imageCode" />
                <result column="imageCodeName" property="imageCodeName" />
            </collection>
        </resultMap>
        <select id="findDetail2" resultMap="SizeCodeImageCodeMap2">
            select * from size_code
        </select>
    
    </mapper>
    
    package com.mozq.mb.mb01.mapper;
    
    import com.alibaba.fastjson.JSONObject;
    import com.mozq.mb.mb01.pojo.SizeCode;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    public class SizeCodeMapperTest {
        @Autowired
        private SizeCodeMapper sizeCodeMapper;
        @Test
        public void findDetail(){
            List<SizeCode> sizeCodes = sizeCodeMapper.findDetail();
            System.out.println(JSONObject.toJSONString(sizeCodes));
        }
    
        @Test
        public void findDetail2(){
            List<SizeCode> sizeCodes = sizeCodeMapper.findDetail2();
            System.out.println(JSONObject.toJSONString(sizeCodes));
        }
        
        @Test
        public void find(){
            List<SizeCode> sizeCodes = sizeCodeMapper.find();
            System.out.println(sizeCodes);
        }
    }
    
    package com.mozq.mb.mb01.mapper;
    
    import com.mozq.mb.mb01.pojo.SizeCode;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface SizeCodeMapper {
        List<SizeCode> find();
    
        List<SizeCode> findDetail();
    
        List<SizeCode> findDetail2();
    }
    
    [
        {
            "imageCodeList":[
                {
                    "imageCode":1,
                    "imageCodeName":"红色"
                }
            ],
            "sizeCode":1,
            "sizeCodeName":"15X19"
        },
        {
            "imageCodeList":[
                {
                    "imageCode":2,
                    "imageCodeName":"橙色"
                }
            ],
            "sizeCode":1,
            "sizeCodeName":"15X18"
        },
        {
            "imageCodeList":[
                {
                    "imageCode":1,
                    "imageCodeName":"古风"
                }
            ],
            "sizeCode":2,
            "sizeCodeName":"12X12"
        }
    ]
    
    [
        {
            "imageCodeList":[
                {
                    "imageCode":1,
                    "imageCodeName":"红色"
                },
                {
                    "imageCode":2,
                    "imageCodeName":"橙色"
                }
            ],
            "sizeCode":1,
            "sizeCodeName":"15X19"
        },
        {
            "imageCodeList":[
                {
                    "imageCode":1,
                    "imageCodeName":"古风"
                }
            ],
            "sizeCode":2,
            "sizeCodeName":"12X12"
        }
    ]
    
  • 相关阅读:
    常见面试题
    3*0.1 == 0.3 将会返回什么?true 还是 false?
    poj_2186 强连通分支
    强连通分量、割点、桥
    最小生成树
    poj_2349 Kruskal 最小生成树
    poj_1258 prim最小生成树
    最短路径
    poj_1125 Floyd最短路
    poj_1860 SPFA
  • 原文地址:https://www.cnblogs.com/mozq/p/12079972.html
Copyright © 2020-2023  润新知