spring boot mybatis one-to-many query

background

In development projects, we often encounter one-to-many data situations. When querying company information list information, we need to display the tags of multiple companies in the list. At this time, we can use mybatis to construct the data information in the above requirements.

need

One-to-many queries usually involve two entity classes, one is a party entity class and the other is a multi-party entity class. For example, a company can have multiple tags, then the company is a one-party entity class and the tag is a multi-party entity class.
When querying company list information, in addition to the company's basic information, multiple tag information of the company must also be returned.

The first implementation

Use the association in SQL to query all the data, and then use the collection tag to format the return value. Part of the code is as follows:

 	<!-- 映射结果集 SerCompanyVO -->
    <resultMap id="resultMap" type="SerCompanyVO">
        <id column="company_id" property="companyId" jdbcType="INTEGER" />
        <result column="company_name" property="companyName" jdbcType="VARCHAR" />
        <result column="company_code" property="companyCode" jdbcType="VARCHAR" />
        <result column="company_msg" property="companyMsg" jdbcType="VARCHAR" />
        <result column="company_crdt" property="companyCrdt" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 一对多查询 join 结果集 -->
    <resultMap id="tagResultMap" type="SerCompanyVO" extends="resultMap">
        <collection property="list" ofType="SerCompanyTagVO">
            <id column="tag_id" property="tagId" jdbcType="INTEGER" />
            <result column="tag_name" property="tagName" jdbcType="VARCHAR" />
            <result column="tag_msg" property="tagMsg" jdbcType="VARCHAR" />
        </collection>
    </resultMap>
     
     <!-- 一对多查询 join 语句-->
    <select id="selectSerCompanyListTagJoin" resultMap="tagResultMap">
        SELECT c.company_id,c.company_name, company_code, company_msg, company_crdt,
        t.tag_id,t.tag_msg,t.tag_name FROM ser_company c
        LEFT JOIN ser_company_tag t ON c.company_id = t.company_id
    </select>

Query company information and associated company tag information through selectSerCompanyListTagJoin. Then use tagResultMap to map the result set into the SerCompanyVO entity class. Where list is a collection of label classes in the SerCompanyVO entity class. The following data is returned after the interface is called:
Insert image description here

The second implementation

Use an interface to query the main table data, then perform a secondary subquery through the association tag, and then return the associated data. The specific code is as follows:

<!-- 映射结果集 SerCompanyVO -->
    <resultMap id="resultMap" type="SerCompanyVO">
        <id column="company_id" property="companyId" jdbcType="INTEGER" />
        <result column="company_name" property="companyName" jdbcType="VARCHAR" />
        <result column="company_code" property="companyCode" jdbcType="VARCHAR" />
        <result column="company_msg" property="companyMsg" jdbcType="VARCHAR" />
        <result column="company_crdt" property="companyCrdt" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 映射结果集 SerCompanyTagVO -->
    <resultMap id="serCompanyTagResultMap" type="SerCompanyTagVO">
        <id column="tag_id" property="tagId" jdbcType="INTEGER" />
        <result column="tag_name" property="tagName" jdbcType="VARCHAR" />
        <result column="tag_msg" property="tagMsg" jdbcType="VARCHAR" />
        <result column="company_id" property="companyId" jdbcType="INTEGER" />
    </resultMap>

    <!-- 一对多查询 asso 结果集 -->
    <resultMap id="assoResultMap" type="com.study.model.SerCompanyVO" extends="resultMap">
        <association property="list"  column="company_id" select="getSerCompanyTag"/>
    </resultMap>

    <!-- 一对多查询  asso方式 子表查询 -->
    <select id="selectSerCompanyListTagAsso" resultMap="assoResultMap">
        select company_id,company_name, company_code, company_msg, company_crdt from ser_company
    </select>

    <!-- 一对多查询  asso方式 主表查询 -->
    <select id="getSerCompanyTag" resultMap="serCompanyTagResultMap" parameterType="int">
          select t.company_id,t.tag_id,t.tag_msg,t.tag_name from ser_company_tag t where t.company_id=#{companyId}
    </select>

First, call selectSerCompanyListTagAsso to query the main table information. When returning the result set, call the set select subquery through association to perform a secondary query. Here is the getSerCompanyTag interface. Then reorganize the result set. The result is as follows:

Insert image description here

Summarize

The second method is not very useful and is suitable for list query interfaces. The subquery will be queried as many times as the main table returns data. It is a waste of database resources, so it is recommended to use the first method for querying.

In the first method, please use inner join association when necessary. This is only an example of one-to-many return.

Entity class source code

@Alias("SerCompanyTagVO")
public class SerCompanyTagVO {
    
    

    private int tagId;
    private String tagName;
    private String tagMsg;
    private int companyId;

    public int getTagId() {
    
    
        return tagId;
    }

    public void setTagId(int tagId) {
    
    
        this.tagId = tagId;
    }

    public String getTagName() {
    
    
        return tagName;
    }

    public void setTagName(String tagName) {
    
    
        this.tagName = tagName;
    }

    public String getTagMsg() {
    
    
        return tagMsg;
    }

    public void setTagMsg(String tagMsg) {
    
    
        this.tagMsg = tagMsg;
    }

    public int getCompanyId() {
    
    
        return companyId;
    }

    public void setCompanyId(int companyId) {
    
    
        this.companyId = companyId;
    }
}
@Alias("SerCompanyVO")
public class SerCompanyVO {
    
    

    private int companyId;
    private String companyName;
    private String companyCode;
    private String companyMsg;
    private String companyCrdt;
    private List<SerCompanyTagVO> list;

    private SerCompanyTagVO serCompanyTagVO;


    public int getCompanyId() {
    
    
        return companyId;
    }

    public void setCompanyId(int companyId) {
    
    
        this.companyId = companyId;
    }

    public String getCompanyName() {
    
    
        return companyName;
    }

    public void setCompanyName(String companyName) {
    
    
        this.companyName = companyName;
    }

    public String getCompanyCode() {
    
    
        return companyCode;
    }

    public void setCompanyCode(String companyCode) {
    
    
        this.companyCode = companyCode;
    }

    public String getCompanyMsg() {
    
    
        return companyMsg;
    }

    public void setCompanyMsg(String companyMsg) {
    
    
        this.companyMsg = companyMsg;
    }

    public String getCompanyCrdt() {
    
    
        return companyCrdt;
    }

    public void setCompanyCrdt(String companyCrdt) {
    
    
        this.companyCrdt = companyCrdt;
    }

    public List<SerCompanyTagVO> getList() {
    
    
        return list;
    }

    public void setList(List<SerCompanyTagVO> list) {
    
    
        this.list = list;
    }

    public SerCompanyTagVO getSerCompanyTagVO() {
    
    
        return serCompanyTagVO;
    }

    public void setSerCompanyTagVO(SerCompanyTagVO serCompanyTagVO) {
    
    
        this.serCompanyTagVO = serCompanyTagVO;
    }
}

dao layer source code

@Mapper
public interface SerCompanyMapper {

    /**
     * 一对多查询公司信息 包含公司标签 join 方式
     * @return
     */
    List<SerCompanyVO> selectSerCompanyListTagJoin();

    /**
     * 一对多查询公司信息 包含公司标签 asso 方式
     * @return
     */
    List<SerCompanyVO> selectSerCompanyListTagAsso();

    /**
     * 一对多查询公司信息 包含公司标签 asso 方式
     * @param companyId
     * @return
     */
    SerCompanyTagVO getSerCompanyTag(int companyId);
}

write at the end

Open source is a virtue, join the open source community as soon as possible and build a beautiful ecosystem together!

Guess you like

Origin blog.csdn.net/qq_36378416/article/details/127769485