<resultMap id="ResultMapManage" type="com.javapandeng.po.Manage"><result property="id" column="id" jdbcType="INTEGER"/><result property="userName" column="userName" jdbcType="VARCHAR"/><result property="passWord" column="passWord" jdbcType="VARCHAR"/><result property="realName" column="realName" jdbcType="VARCHAR"/></resultMap>
<select id="getByMap" resultMap="ResultMapManage" parameterType="map">select <include refid="Manage_field"/>from manage where where 1=1<include refid="Manage_where"/></select>
好的,我们来彻底讲清楚 resultMap="ResultMapManage"
中的 ResultMapManage
是什么意思。
这是一个 MyBatis 框架中的核心概念,非常重要。
一、核心答案:它是什么?
ResultMapManage
是一个 id
,是一个标识符(名字)。
它是在 MyBatis 的 Mapper XML 文件中,由一个 <resultMap>
标签定义的结果映射集的唯一名称。
你可以把它理解为:
二、为什么需要它?它解决了什么问题?
数据库的列(Column)名和 Java 类的属性(Property)名并不总是完全一致。即使一致,有时也需要进行一些特殊处理。<resultMap>
就是为了解决这个映射问题而存在的。
没有 resultMap
时(使用 resultType
):
有 resultMap
时:
三、ResultMapManage
是如何定义的?
在你的某个 Mapper XML 文件中,肯定有这样一段定义(名字可能不叫 ResultMapManage
,但结构一样):
<!-- 1. 使用 <resultMap> 标签定义一个映射集,并给它一个唯一的 id -->
<resultMap id="ResultMapManage" type="com.javapandeng.po.Manage"><!-- 2. 定义主键映射(可选,但好习惯) --><id property="id" column="id" /><!-- 3. 定义普通属性的映射 --><result property="userName" column="user_name" /><result property="password" column="password" /><result property="realName" column="real_name" /><result property="createTime" column="create_time" jdbcType="TIMESTAMP"/><result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/><!-- 4. 可以处理更复杂的关联映射(一对一,一对多) --><!-- <association property="department" resultMap="AnotherResultMap"/> --><!-- <collection property="roles" ofType="Role" resultMap="RoleResultMap"/> -->
</resultMap>
这段代码解释:
-
<resultMap id="ResultMapManage" ...>
: 这里就是定义了这个映射集的名字(ID)叫 ResultMapManage
。
-
type="com.javapandeng.po.Manage"
: 这个映射规则最终是要把一个数据库记录转换成 Manage
这个类型的 Java 对象。
-
<id>
和 <result>
: 这是映射规则的核心。
-
column
: 数据库表的列名(user_name
)。
-
property
: Java 对象的属性名(userName
)。
-
jdbcType
: (可选)指定数据库字段的类型,在某些特定场景下(如传入参数为null时)有助于 MyBatis 生成更精确的 SQL。
四、在什么地方使用它?(resultMap="ResultMapManage"
)
定义好之后,你就可以在任何需要返回 Manage
对象的 SQL 查询语句中引用它。
<!-- 在 SELECT 语句中,使用 resultMap 属性(而不是 resultType)来引用我们定义好的映射集 -->
<select id="getManageById" resultMap="ResultMapManage">SELECT id, user_name, password, real_name, create_time, update_timeFROM manageWHERE id = #{id}
</select><select id="getAllManages" resultMap="ResultMapManage">SELECT * FROM manage <!-- 即使使用 SELECT *,也能靠 resultMap 正确映射 -->
</select>
当 MyBatis 执行 getManageById
这个查询时:
-
执行 SQL:SELECT ... FROM manage WHERE id = 1
,得到一行结果。
-
MyBatis 看到 resultMap="ResultMapManage"
,就知道要去寻找 id
为 ResultMapManage
的 <resultMap>
。
-
找到后,按照里面的规则,开始创建 Manage
对象并填充属性:
-
最终,一个完整的、属性都已赋值的 Manage
对象就被返回给了调用方(如 Service 层)。
五、resultMap
vs resultType
总结
所以,resultMap="ResultMapManage"
中的 ResultMapManage
:
-
它是一个标识符,是你在 XML 中自定义的一套映射规则的名称。
-
这套规则明确规定了数据库表的列 和 Java 对象的属性 之间的对应关系。
-
你在 SQL 查询标签(<select>
)中通过 resultMap="ResultMapManage"
来引用这套规则,告诉 MyBatis:“请按照 ResultMapManage
这个说明书,把我查到的数据组装成 Java 对象。”
这是一种显式配置,优于隐式约定的做法,是现代 MyBatis 开发中的最佳实践,使得代码更加清晰、健壮和易于维护。