码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis结果集一对多映射

时间:2019-12-22 16:23:08      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:insert   doctype   drop   设置   test   ibatis   charset   image   obj   

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"
    }
]

MyBatis结果集一对多映射

标签:insert   doctype   drop   设置   test   ibatis   charset   image   obj   

原文地址:https://www.cnblogs.com/mozq/p/12079972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!