1、写sql语句时写别名
<select id="getUserInfoById" resultType="com.system.User">
select id,
username as userName,
password
from user
where id = #{id}
</select>
2、在map映射文件中使用resultMap类来自定义规则
<select id="getUserInfoById" resultMap="ResultMap">
select id,
user_name as userName,
password
from user
where id = #{id}
</select>
<resultMap id="ResultMap" type="com.system.user">
<id column="id" property="id" />
<result column="user_name " property="userName" />
<result column="password" property="password" />
</resultMap>



本文介绍了如何在SQL查询中使用别名(如`username as userName`)简化结果,并在MyBatis的 resultMap中自定义字段映射,以提高代码可读性。通过实例演示了如何配置`<resultMap>`和`<id>...</property>`标签来映射数据库表结构到Java对象属性。

1万+

被折叠的 条评论
为什么被折叠?



