关于 MyBatis resultMaps 的说明
Clarification about MyBatis resultMaps
有人可以帮助阐明 MyBatis 文档中定义的重命名 properties/columns 吗?
MyBatis 文档
文档定义了一个简单的 Java class:
public class User {
private int id;
private String username;
private String password;
...
}
以及以下:
The great thing about ResultMaps is that you've already learned a lot
about them, but you haven't even seen one yet! These simple cases
don't require any more than you've seen here. Just for example sake,
let's see what this last example would look like as an external
resultMap, as that is another way to solve column name mismatches.
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="password"/>
</resultMap>
And the statement that references it uses the resultMap attribute to
do so (notice we removed the resultType attribute). For example:
<select id="selectUsers" resultMap="userResultMap">
select user_id, user_name, password
from some_table
where id = #{id}
</select>
我的问题
以上是否意味着 POJO/Bean 变量 "username" 和 "password" 被分配给名为 user_name 和 hashed_password 的数据库列,这是否以同样的方式完成他们还写了以下内容?
<select id="selectUsers" resultType="User">
select
user_id as "id",
user_name as "userName",
hashed_password as "password"
from some_table
where id = #{id}
</select>
@physicsboy 你上一条评论是对的。基本上,resultmap 将列映射到 属性。 resultmap 'sees' 你设置的姓氏列,例如,如果你进行以下查询:
select id, column_A from table
您的结果图将是
<resultMap id="ResultMapName" type="WhateverType">
<id property="id" column="id" />
<result property="columnA" column="column_A"/>
</resultMap>
但是如果您为其中一列设置别名:
select id, column_A as column_A_with_alias from table
您的 resultMap 将是
<resultMap id="ResultMapName" type="WhateverType">
<id property="id" column="id" />
<result property="columnA" column="column_A_with_alias"/>
</resultMap>
不用说你的 POJO 应该有所有属性的 getter 和 setter。
是的。
结果映射告诉 MyBatis “从查询中的 x 列中获取值并将其存储在 POJO 中的字段 y 中。
更多信息:
MyBatis 喜欢 POJO 中的字段匹配查询中的列名。
在这种情况下,
您不需要 resultMap 条目。
当 POJO 中的字段与查询中的列名不完全匹配时,您只需要 resultMap(上例 "My Question" 在您的 post 中就是这种情况)。
由于 User
POJO 中的字段与第二个示例中的列名不完全匹配 ("hashedPassword" != "password") 您仍然需要使用 resultMap 条目来查询和您的 POJO 之间的映射。
如果您按如下方式更改查询:
<select id="selectUsers" resultType="User">
select
user_id as "id",
user_name as "username",
hashed_password as "hashedPassword"
from some_table
where id = #{id}
</select>
那你就不需要resultMap了,
因为 "username" 是您的 POJO 中的实际字段名称。
有人可以帮助阐明 MyBatis 文档中定义的重命名 properties/columns 吗?
MyBatis 文档
文档定义了一个简单的 Java class:
public class User {
private int id;
private String username;
private String password;
...
}
以及以下:
The great thing about ResultMaps is that you've already learned a lot about them, but you haven't even seen one yet! These simple cases don't require any more than you've seen here. Just for example sake, let's see what this last example would look like as an external resultMap, as that is another way to solve column name mismatches.
<resultMap id="userResultMap" type="User"> <id property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="password" column="password"/> </resultMap>
And the statement that references it uses the resultMap attribute to do so (notice we removed the resultType attribute). For example:
<select id="selectUsers" resultMap="userResultMap"> select user_id, user_name, password from some_table where id = #{id} </select>
我的问题 以上是否意味着 POJO/Bean 变量 "username" 和 "password" 被分配给名为 user_name 和 hashed_password 的数据库列,这是否以同样的方式完成他们还写了以下内容?
<select id="selectUsers" resultType="User"> select user_id as "id", user_name as "userName", hashed_password as "password" from some_table where id = #{id} </select>
@physicsboy 你上一条评论是对的。基本上,resultmap 将列映射到 属性。 resultmap 'sees' 你设置的姓氏列,例如,如果你进行以下查询:
select id, column_A from table
您的结果图将是
<resultMap id="ResultMapName" type="WhateverType">
<id property="id" column="id" />
<result property="columnA" column="column_A"/>
</resultMap>
但是如果您为其中一列设置别名:
select id, column_A as column_A_with_alias from table
您的 resultMap 将是
<resultMap id="ResultMapName" type="WhateverType">
<id property="id" column="id" />
<result property="columnA" column="column_A_with_alias"/>
</resultMap>
不用说你的 POJO 应该有所有属性的 getter 和 setter。
是的。 结果映射告诉 MyBatis “从查询中的 x 列中获取值并将其存储在 POJO 中的字段 y 中。
更多信息: MyBatis 喜欢 POJO 中的字段匹配查询中的列名。 在这种情况下, 您不需要 resultMap 条目。
当 POJO 中的字段与查询中的列名不完全匹配时,您只需要 resultMap(上例 "My Question" 在您的 post 中就是这种情况)。
由于 User
POJO 中的字段与第二个示例中的列名不完全匹配 ("hashedPassword" != "password") 您仍然需要使用 resultMap 条目来查询和您的 POJO 之间的映射。
如果您按如下方式更改查询:
<select id="selectUsers" resultType="User">
select
user_id as "id",
user_name as "username",
hashed_password as "hashedPassword"
from some_table
where id = #{id}
</select>
那你就不需要resultMap了, 因为 "username" 是您的 POJO 中的实际字段名称。