mybatis xml Learn the difference between association&collection from the parent category and subcategory of the region, a must-see for getting started

I. Introduction

After many years of java development, I suddenly wanted to write a series of association & collection about mybatis xml

The picture below is the focus of this article
Insert picture description here

Two, the difference between association & collection

Both are used in the ResultMap tag

One of the thoughts when MyBatis was created was that the database cannot always be what you want or need. We hope that every database has a good third normal form or BCNF paradigm, but they are not all that way. It would be great if there could be a database mapping mode that perfectly fits all applications, but unfortunately there is no such thing. And ResultMap is MyBatis's answer to this question.

  • association – a complex type of association; many results will be packed into this type.
    Nested result mapping – an association can be a resultMap element or a reference to another result map

  • collection-a collection of complex types
    Nested result mapping-a collection can be a resultMap element, or a reference to another result map

In development:

  1. For the association attribute is a single object (non-collection type) type, use the association element, usually directly use the multi-table query operation.
  2. For the associated attribute is a collection object type, use the collection element, usually use additional SQL processing, and configure lazy loading.

2.1 Core code

<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.cancan.mp.entity.Region">
    <id column="id" property="id" />
    <result column="region_name" property="regionName" />
    <result column="parent_id" property="parentId" />
    <result column="rp_region_name" property="parentName"/>

    <!--列出地区的父类-->
    <association property="parent" javaType="com.cancan.mp.entity.Region">
        <id column="rp_id" property="id" />
        <result column="rp_region_name" property="regionName" />
        <result column="rp_parent_id" property="parentId" />
    </association>
    
    <!--根据父类id,查出子类id-->
    <collection property="children" select="com.cancan.mp.mapper.RegionMapper.selectByCode" column="id"/>

</resultMap>

2.2 Database data

Insert picture description here

2.3 id find out the collection of parent_id and children corresponding to the id to get the request result

GET http://localhost:8081/region/get?id=440100

{
    
    
  "id": 440100,
  "regionName": "广州市",
  "parentId": 440000,
  "parentName": "广东省",
  "parent": {
    
    
    "id": 440000,
    "regionName": "广东省",
    "parentId": 0,
    "parentName": null,
    "parent": null,
    "children": null
  },
  "children": [
    {
    
    
      "id": 440101,
      "regionName": "市辖区",
      "parentId": 440100,
      "parentName": null,
      "parent": null,
      "children": null
    },
    {
    
    
      "id": 440183,
      "regionName": "增城市",
      "parentId": 440100,
      "parentName": null,
      "parent": null,
      "children": null
    },
    {
    
    
      "id": 440184,
      "regionName": "从化市",
      "parentId": 440100,
      "parentName": null,
      "parent": null,
      "children": null
    }
  ]
}

Three, specific cases

3.1 Database tables

CREATE TABLE `region` (
  `id` int(11) NOT NULL COMMENT '主键',
  `region_name` varchar(40) DEFAULT NULL COMMENT '地区名字',
  `parent_id` int(11) DEFAULT NULL COMMENT '父类id',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='地区表';

3.2 Entity class

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("region")
public class Region implements Serializable {
    
    

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.INPUT)
    private Integer id;

    /**
     * 地区名字
     */
    @TableField("region_name")
    private String regionName;

    /**
     * 父类id
     */
    @TableField("parent_id")
    private Integer parentId;

    /**
     * 父类的名字
     */
    @TableField(exist = false)
    private String parentName;

    /**
     * 父类
     */
    @TableField(exist = false)
    private Region parent;

    /**
     * 子类
     */
    @TableField(exist = false)
    private List<Region> children;

}

3.3 folders 和 mapper.xml

public interface RegionMapper extends BaseMapper<Region> {
    
    

    Region getRegion(@Param("code") Integer id);
}
<?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.cancan.mp.mapper.RegionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.cancan.mp.entity.Region">
        <id column="id" property="id" />
        <result column="region_name" property="regionName" />
        <result column="parent_id" property="parentId" />
        <result column="rp_region_name" property="parentName"/>

        <!--列出地区的父类-->
        <association property="parent" javaType="com.cancan.mp.entity.Region">
            <id column="rp_id" property="id" />
            <result column="rp_region_name" property="regionName" />
            <result column="rp_parent_id" property="parentId" />
        </association>

        <!--根据父类id,查出子类id-->
        <collection property="children" select="com.cancan.mp.mapper.RegionMapper.selectByCode" column="id"/>

    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, region_name, parent_id
    </sql>
    <select id="getRegion" resultMap="BaseResultMap">
        select
         r.id,
         r.region_name,
         r.parent_id,
         rp.id rp_id,
         rp.region_name rp_region_name,
         rp.parent_id rp_parent_id
        from region r
        left join region rp
        on r.parent_id = rp.id
        where r.id = #{code}

    </select>

    <select id="selectByCode" resultType="com.cancan.mp.entity.Region">
        select
        <include refid="Base_Column_List"/>
        from region
        where parent_id = #{id}
    </select>

</mapper>

Guess you like

Origin blog.csdn.net/qq_34168515/article/details/111113408