mybatis入门-05-ResultMap

Mybatis 的 ResultMap 功能用于自定义结果集与实体类之间的映射关系,解决了字段名与属性名不一致的问题。可以通过指定列的别名或者创建自定义结果映射来实现映射。此外,可以开启或关闭自动映射,甚至通过继承映射减少重复配置。ResultMap 的使用提高了代码的可读性和维护性。
摘要由CSDN通过智能技术生成

Mybatis-ResultMap

1 基本使用

解决的问题 属性名与字段名不一致

<!--
        resultMap 用来自定义结果集和实体类的映射
            属性:
                id 相当于这个resultMap的唯一标识
                type 用来指定映射到哪个实体类
        id标签  用来指定主键列的映射规则
            属性:
                property 要映射的属性名
                column  对应的列名
        result标签 用来指定普通列的映射规则
            属性:
                property 要映射的属性名
                column 对应的列名
    -->
    <resultMap id="orderMap" type="com.lzy.entities.Order" >
        <id column="id" property="id"></id>
        <result column="createtime" property="createtime"></result>
        <result column="price" property="price"></result>
        <result column="remark" property="remark"></result>
        <result column="user_id" property="userId"></result>
    </resultMap>
	<!--使用我们自定义的映射规则-->
    <select id="findAll" resultMap="orderMap">
        SELECT id,createtime,price,remark,user_id  FROM ORDERS
    </select>

发现,数据库中字段名为pwd,而实体类中变量名为password,导致查询password为空

1.1解决方法

1.1.1为列名指定别名
<select id="selectUserById" resultType="User">
	select id , name , pwd as password from user where id = #{id}
</select>
1.1.2 使用结果集映射->ResultMap 【推荐】

我们定义resultMap时默认情况下自动映射是开启状态的。也就是如果结果集的列名和我们的属性名相同是会自动映射的我们只需要写特殊情况的映射关系即可。

<resultMap id="orderMap" type="com.lzy.entities.Order" >
   <result column="user_id" property="userId"></result>
</resultMap>
<!--使用我们自定义的映射规则-->
<select id="findAll" resultMap="orderMap">
  SELECT id,createtime,price,remark,user_id  FROM ORDERS
</select>

可以选择关闭自动映射

<resultMap id="orderMap" type="com.lzy.entities.Order" autoMapping="false">
    <id column="id" property="id"></id>
    <result column="createtime" property="createtime"></result>
    <result column="price" property="price"></result>
    <result column="remark" property="remark"></result>
    <result column="user_id" property="userId"></result>
</resultMap>

继承映射关系

<!--定义个父映射,供其他resultMap继承-->
<resultMap id="baseOrderMap" type="com.lzy.entities.Order" >
   <id column="id" property="id"></id>
   <result column="createtime" property="createtime"></result>
   <result column="price" property="price"></result>
   <result column="remark" property="remark"></result>
</resultMap>
<!--继承baseOrderMap,然后只需要写自己特有的映射关系即可-->
<resultMap id="orderMap" type="com.lzy.entities.Order" autoMapping="false" extends="baseOrderMap">
   <result column="user_id" property="userId"></result>
</resultMap>

使用结果集映射

<resultMap id="UserMap" type="User">
	<!-- id为主键 -->
	<id column="id" property="id"/>
	<!-- column是数据库表的列名 , property是对应实体类的属性名 -->
	<result column="name" property="name"/>
	<result column="pwd" property="password"/>
</resultMap>
<select id="selectUserById" resultMap="UserMap">
	select id , name , pwd from user where id = #{id}
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值