一:使用环境

在初期的Spring boot+Mybatis项目的开发中,当需要用一张表中的多个字段进行子查询的时候,按照往常通过多次的嵌套,会变得很冗杂,此时我们需要使用Mybatis提供的嵌套查询,此次主要来讲讲collection简单用法。

二:使用方法:

这次的简易情景是我们需要通过查询所有的讨论话题,并建立子查询,看到他人的回复和自己的回复情况。

private int id;
    private String chat_name;
    private int chat_hot;
    private List<chat_son>  son;
    private List<chat_myson>  myson;

实体类只需要在原本的字段上加上自己的回复的一个List和他人回复的一个List

自己回复的实体类和别人回复的实体类分别是:

private int id;
    private String chat_name;
    private String chat_myReply;
private int id;
    private String chat_name;
    private String chat_reply;
    private String chat_person;

基本上可以看到此次建立子查询的公共字段为chat_name,即话题名称。

<resultMap id="BaseResultMap" type="cn.zcbigdata.mybits_demo.entity.Chat">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="chat_name" jdbcType="VARCHAR" property="chat_name" />
        <result column="chat_hot" jdbcType="INTEGER" property="chat_hot" />


        <collection property="son"  column="chat_name" ofType="cn.zcbigdata.mybits_demo.entity.chat_son"  select="select_son">
            <id column="id" jdbcType="INTEGER" property="id" />
            <result column="chat_name" jdbcType="VARCHAR" property="chat_name" />
            <result column="chat_reply" jdbcType="VARCHAR" property="chat_reply" />
            <result column="chat_preson" jdbcType="VARCHAR" property="chat_preson" />
        </collection>

        //prpeerty属性里是在java的实体类中的变量名称,即上文List所对应的变量名称
        //column属性是子查询的字段
        //ofType属性,因为此次是要查询返回的是一个集合,所以我们使用ofType
        //select属性主要是选择要执行的sql语句,找到对应的id即可
        <collection property="myson"  column="chat_name" ofType="cn.zcbigdata.mybits_demo.entity.chat_myson"  select="select_myson">
            <id column="id" jdbcType="INTEGER" property="id" />
            <result column="chat_name" jdbcType="VARCHAR" property="chat_name" />
            <result column="chat_myReply" jdbcType="VARCHAR" property="chat_myReply" />
        </collection>
    </resultMap>

<!--    通过话题名称显示,有模糊查询-->
    <select id="select_chatName" resultMap="BaseResultMap" parameterType="String">
        select id,chat_name,chat_hot
        from chat where chat_name like CONCAT('%',#{chat_name},'%')
<!--        limit #{offset},#{limit}-->
    </select>

<!--    他人回复 子查询-->
    <select id = "select_son"  resultType="cn.zcbigdata.mybits_demo.entity.chat_son" parameterType="String">
    select id,chat_reply,chat_person from chat_son where chat_name = #{chat_name}
    </select>

<!--    自己回复 子查询-->
    <select id="select_myson" resultType="cn.zcbigdata.mybits_demo.entity.chat_myson" parameterType="String">
        select id,chat_myReply from chat_myson where chat_name = #{chat_name}
    </select>

通过公共字段作为子查询的元素传入子查询语句中,每次只要触发主查询的时候,我们都会进行子查询的自启动。

三:结果与总结:

最后就是两个子查询的结果了。

[
    {
        "id": 1,
        "chat_name": "物价问题",
        "chat_hot": 50,
        "son": [
            {
                "id": 1,
                "chat_reply": "\"太贵了\"",
                "chat_person": "小明"
            },
            {
                "id": 2,
                "chat_reply": "\"希望能降低\"",
                "chat_person": "小红"
            }
        ],
        "myson": [
            {
                "id": 1,
                "chat_myReply": "\"肉降价了\""
            }
        ]
    },
    {
        "id": 2,
        "chat_name": "房价问题",
        "chat_hot": 30,
        "son": [
            {
                "id": 3,
                "chat_reply": "\"买不起房子\"",
                "chat_person": "小军"
            },
            {
                "id": 4,
                "chat_reply": "\"我不在乎\"",
                "chat_person": "小刚"
            }
        ],
        "myson": [
            {
                "id": 2,
                "chat_myReply": "\"我有房子\""
            }
        ]
    },
    {
        "id": 3,
        "chat_name": "食堂问题",
        "chat_hot": 50,
        "son": [
            {
                "id": 5,
                "chat_reply": "\"什么时候装修呀?\"",
                "chat_person": "花花"
            },
            {
                "id": 6,
                "chat_reply": "\"不好吃,这个菜\"",
                "chat_person": "白案"
            }
        ],
        "myson": [
            {
                "id": 3,
                "chat_myReply": "\"快换新椅子\""
            },
            {
                "id": 4,
                "chat_myReply": "\"价格有点贵\""
            }
        ]
    },
    {
        "id": 4,
        "chat_name": "奖学金问题",
        "chat_hot": 10,
        "son": [
            {
                "id": 7,
                "chat_reply": "\"什么时候发放\"",
                "chat_person": "小刘"
            }
        ],
        "myson": []
    }
]

最后总结Mybatis提供的嵌套查询机制在进行一对多的对象时可以使用,当需要的传递多个参数的时候,只需要在column属性传递一个集合即可实现多参数。