resultType 和resultMap 的使用区别;

本文详细介绍了MyBatis中的resultMap和resultType的使用方法,包括如何解决POJO属性名与数据库表列名不一致的问题,以及在不同场景下如何选择使用resultMap或resultType。

resultMap,创建专门的一对一查询;

 

resultType

pojo 属性名需要和数据库中 表的 column名 一致才能映射成功。

当数据库表的列名和Pojo中属性名一致的时候(或者不一致可以使用别名解决);

以下这种情况,不使用*查询可以使用resultType ,使用resultMap需要使用*查询。

 

配置文件如下:

<select id="findUser1ByUsernameAndPassword" parameterType="user1" resultType="user1">
          select user_id userId,age,username,password from user1 where  username=#{username} and password=#{password}
    </select>

 

 


 

resultMap;

用来解决pojo  属性名和column名字 不一致,配置resultMap  ,配置resultMap标签,id=“映射的结果”  type=参数类型;

在mybatis 内部处理,仍然是保持Pojo和数据库table  column  名字保持一致;

 

   <select id="findOrdersByNameAndPrice" parameterType="orders" resultMap="ordersMap">
          select * from orders where  name=#{name} and price=#{price}
    </select>

    <resultMap id="ordersMap" type="orders">
        <id property="id" column="id"/><!--主键-->
        <result property="name" column="name"/>
        <result property="price" column="price"/>
    </resultMap>

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值