ResultMap tag of MyBatis

ResultMap tag of MyBatis

The basic function of the ResultMap tag: establish the mapping relationship information between SQL query result fields and entity attributes

  Before diving into the ResultMap tag, we need to understand the process from a SQL query result set to a JavaBean or POJO entity.

  1. Get ResultSet object through JDBC query

  2. Traverse the ResultSet object and temporarily store each row of data in the HashMap instance, using the field name or field alias of the result set as the key, and the field value as the value

  3. Instantiate the domain model through reflection based on the type attribute of the ResultMap tag

  4. Fill the key-value pairs in the HashMap into the domain model instance according to the type attribute of the ResultMap tag and the tag information such as id and result and return it

例:
<resultMap id="getStudentRM" type="EStudnet"> <id property="id" column="ID"/> <result property="studentName" column="Name"/> <result property="studentAge" column="Age"/> </resultMap>
<select id="getStudent" resultMap="getStudentRM">
  SELECT ID, Name, Age
    FROM TStudent
</select> 
tag description: Main tag
id: the mark of the resultMap
type: the class name of the returned value, in this case, the EStudnet class
sub-tag is returned:

id: used to set the mapping relationship between the primary key field and the domain model attribute, where the primary key is ID, which corresponds to id.
result: used to set the mapping relationship between common fields and domain model properties

<resultMap type="Category" id="categoryBean">
<id column="cid" property="id" />
<result column="cname" property="name" /><!-- 一对多的关系 --><!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 --><collection property="products" ofType="Product"><id column="pid" property="id" /><result column="pname" property="name" /><result column="price" property="price" /></collection></resultMap>









Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326640687&siteId=291194637