table of Contents
Method one: alias in the SQL statement
Problem Description
What if the fields in the database and the attributes in the entity class are inconsistent?
Method one: alias in the SQL statement
The alias name looks perfect, but if there are many aliased fields, the SQL statement will be very bloated
select id,name,pwd from mybatis.user where id = #{id}
Method 2: resultMap
In the specific Mapper.xml file, add the resultMap tag. This is actually very flexible. For example, I only have pwd! = Password, so there is no need to map other fields.
This approach transfers non-essential operations in SQL to the <resultMap> tag. However, if you check 5 tables across 5 entities, <resultMap> will be very bloated, and the optimization plan will be shared in a blog
<!--结果集映射-->
<resultMap id="UserMap" type="User">
<!--column数据库中的字段,property实体类中的属性-->
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserById" resultMap="UserMap">
select * from user where id = #{id}
</select>