How to configure result mapping in SQL mapping file in MyBatis
foreword
MyBatis is an excellent ORM framework, which provides a variety of configuration methods to define SQL statements and result mapping rules. Among them, the SQL mapping file is one of the most commonly used configuration methods of MyBatis. It defines SQL statements and result mapping rules through XML files, which is very convenient and flexible to use. This article will introduce how the SQL mapping file in MyBatis configures the result mapping, including general types, collection types and other situations.
basic grammar
In the SQL mapping file of MyBatis, we can use <resultMap>
the tag to define the result mapping rules, for example:
<!-- 定义用户结果映射规则 -->
<resultMap id="userResultMap" type="com.example.model.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
In the above example, we have used <resultMap>
the tag to define a userResultMap
result mapping rule named , where id
the attribute is used to specify a unique identifier for the rule, type
the attribute is used to specify the Java type to which the rule applies, <id>
and <result>
the tag is used to define the object properties Mapping relationship with database fields.
Among them, <id>
the label is used to define the primary key field, property
the property is used to specify the property name of the Java object, and column
the property is used to specify the field name of the database table. <result>
Tags are used to define ordinary fields, and their attributes <id>
are the same as tags, except that no primary key fields need to be specified.
After defining the result mapping rule, we can use the label in the SQL statement <resultMap>
to refer to the rule, for example:
<!-- 查询用户列表 -->
<select id="selectUsers" resultMap="userResultMap">
SELECT * FROM users
</select>
In the above example, we use resultMap
the attribute to specify the mapping rules for the returned results, and the value of this attribute is defined before userResultMap
.
regular type
When the SQL statement returns conventional types (such as strings, integers, floating point numbers, etc.), we can use resultType
the attribute to specify the type of the return value, for example:
<!-- 查询用户数量 -->
<select id="countUsers" resultType="int">
SELECT COUNT(*) FROM users
</select>
In the above example, we use resultType
the attribute to specify the type of the return value int
, which means returning an integer.
collection type
When the SQL statement returns a collection type, we can use resultType
the attribute to specify the type of elements in the collection, for example:
<!-- 查询用户列表 -->
<select id="selectUsers" resultType="com.example.model.User">
SELECT * FROM users
</select>
In the above example, we use resultType
the attribute to specify the type of the return value com.example.model.User
, which means returning a list of User objects.
In addition to using resultType
attributes, we can also use <collection>
tags to define mapping rules for collection types, for example:
<!-- 定义用户结果映射规则 -->
<resultMap id="userResultMap" type="com.example.model.User">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
<collection property="orders" ofType="com.example.model.Order">
<id property="id" column="order_id" />
<result property="name" column="order_name" />
</collection>
</resultMap>
In the above example, we have used <collection>
the tag to define an orders
order list named , where property
the attribute is used to specify the property name of the Java object, ofType
the attribute is used to specify the type of element in the collection, <id>
and <result>
the tag is used to define the object properties and database fields mapping relationship between them.
Dynamic SQL
In actual development, sometimes we need to generate different SQL statements according to different conditions. At this time, we can use the dynamic SQL function provided by MyBatis. Dynamic SQL can dynamically generate SQL statements according to conditions, including if, choose, when, otherwise, trim, where, set, foreach and other labels. In dynamic SQL, we can also use <resultMap>
tags to define result mapping rules, for example:
<!-- 根据用户名和密码查询用户 -->
<select id="selectUserByUsernameAndPassword" resultMap="userResultMap">
SELECT * FROM users
<where>
<if test="username != null">
AND username = #{username}
</if>
<if test="password != null">
AND password = #{password}
</if>
</where>
</select>
In the above example, we use <if>
the label to determine whether the parameter is empty, and generate the corresponding SQL statement if it is not empty. The <where>
label is used to automatically splice the WHERE keyword in the SQL statement and remove redundant AND/OR keywords . After the SQL statement is executed, MyBatis will userResultMap
map the query result to a User object according to the previously defined.
in conclusion
Through the introduction of this article, we have learned how to configure the result mapping rules in the SQL mapping file in MyBatis. Whether it is a conventional type, a collection type, or dynamic SQL, MyBatis provides corresponding tags and attributes to support the definition of result mapping. In actual development, according to specific business requirements and data models, we can map query results into corresponding Java objects by defining different result mapping rules, so as to facilitate data operation and business processing.