Deep parsing resultMap tag

Generally speaking, a table corresponds to a mapper mapping file, but the storage of some attributes in the database does not directly store its contents, but stores its unique identification, such as id. the political outlook, Department, job level and position of employees in the following table are maintained through the id of each sub table:


Therefore, in the XML file of Mybatis, we can use the resultMap tag to correspond all fields in the database to each field of the Java entity class one by one:

<resultMap id="BaseResultMap" type="org.javaboy.vhr.model.Employee">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="gender" property="gender" jdbcType="CHAR"/>
        <result column="birthday" property="birthday" jdbcType="DATE"/>
        <result column="idCard" property="idCard" jdbcType="CHAR"/>
        <result column="wedlock" property="wedlock" jdbcType="CHAR"/>
        <result column="nationId" property="nationId" jdbcType="INTEGER"/>
        <result column="nativePlace" property="nativePlace" jdbcType="VARCHAR"/>
        <result column="politicId" property="politicId" jdbcType="INTEGER"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
        <result column="departmentId" property="departmentId" jdbcType="INTEGER"/>
        <result column="jobLevelId" property="jobLevelId" jdbcType="INTEGER"/>
        <result column="posId" property="posId" jdbcType="INTEGER"/>
        <result column="engageForm" property="engageForm" jdbcType="VARCHAR"/>
        <result column="tiptopDegree" property="tiptopDegree" jdbcType="CHAR"/>
        <result column="specialty" property="specialty" jdbcType="VARCHAR"/>
        <result column="school" property="school" jdbcType="VARCHAR"/>
        <result column="beginDate" property="beginDate" jdbcType="DATE"/>
        <result column="workState" property="workState" jdbcType="CHAR"/>
        <result column="workID" property="workID" jdbcType="CHAR"/>
        <result column="contractTerm" property="contractTerm" jdbcType="DOUBLE"/>
        <result column="conversionTime" property="conversionTime" jdbcType="DATE"/>
        <result column="notWorkDate" property="notWorkDate" jdbcType="DATE"/>
        <result column="beginContract" property="beginContract" jdbcType="DATE"/>
        <result column="endContract" property="endContract" jdbcType="DATE"/>
        <result column="workAge" property="workAge" jdbcType="INTEGER"/>
    </resultMap>

The database maintains the relationship between the main table and sub table through id. in practical application, we will not apply the content of one table alone, but often conduct multi table associated query, which makes the relationship between tables. If we want the program to reflect these relationships, we don't just create a main class (such as employee class) and a classification (such as department class), We will inject classification into the main category. For example, the employee category will include department category, job level category, nationality category and so on:

public class Employee {
    // Corresponding to the fields stored in the main table in the database
    private Integer nationId;
    private Integer politicId;
    private Integer departmentId;
    private Integer jobLevelId;
    private Integer posId;
    // The primary table in the corresponding database depends on each sub table corresponding to the sub table id
    private Nation nation;
    private Politicsstatus politicsstatus;
    private Department department;
    private JobLevel jobLevel;
    private Position position;
    // ...
}

At this time, the problem arises. In the BaseResultMap above, we only map the fields appearing in the main table, that is, only the IDs of the corresponding sub table and the attributes of the Java main class have been mapped (the upper part of the Employee class), but the object class fields - such as the ethnic object, political appearance object, Department object, etc. in the lower part - have not been mapped with the sub table of the corresponding database!
How do I implement this mapping—— In another resultMap tab, complete the mapping between the main table object and the associated sub table object. If the sub table object and the main table are in a one-to-one relationship (for example, an employee and a department, and an employee corresponds to a department), the association tab is used internally; If the sub table object has a one to many relationship with the main table (for example, teachers and courses, and one teacher corresponds to multiple courses), the collection tag is used internally.

<resultMap id="AllEmployeeInfo" type="org.javaboy.vhr.model.Employee" extends="BaseResultMap">
        <association property="nation" javaType="org.javaboy.vhr.model.Nation">
            <id column="nid" property="id"/>
            <result column="nname" property="name"/>
        </association>
        <association property="politicsstatus" javaType="org.javaboy.vhr.model.Politicsstatus">
            <id column="pid" property="id"/>
            <result column="pname" property="name"/>
        </association>
        <association property="department" javaType="org.javaboy.vhr.model.Department">
            <id column="did" property="id"/>
            <result column="dname" property="name"/>
        </association>
        <association property="jobLevel" javaType="org.javaboy.vhr.model.JobLevel">
            <id column="jid" property="id"/>
            <result column="jname" property="name"/>
        </association>
        <association property="position" javaType="org.javaboy.vhr.model.Position">
            <id column="posid" property="id"/>
            <result column="posname" property="name"/>
        </association>
    </resultMap>

matters needing attention:

  1. The property attribute in the association and collection tags corresponds to the property name of the object in the main class. The path of this class is represented by javaType attribute for the former and ofType attribute for the latter;
  2. The column attribute of the id/result tag inside the association and collection tags may not be consistent with the database table fields! Instead, it is consistent with the result field queried by sql statement (for example, the field name queried can be newly defined by using AS keyword), and the column is consistent with the query result field. Remember.

Tags: Java Database programming language

Posted by langemarkdesign on Tue, 17 May 2022 21:43:35 +0300