sqlMapper的结果对象

这篇博客探讨了MyBatis中mapper映射文件与接口方法之间的关系。当输出单个POJO对象或对象列表时,mapper接口会根据返回值类型调用不同的方法。同时,介绍了如何处理列名与属性名不一致的情况,通过定义resultMap进行映射。示例展示了如何使用resultMap来映射不一致的列名到POJO属性。
摘要由CSDN通过智能技术生成

简单类型

参考这个

POJO单个对象和列表

输出单个pojo对象和pojo列表时,mapper映射文件中的resultType的类型是一样的,mapper接口的方法返回值不同。
同样的mapper映射文件,返回单个对象和对象列表时,mapper接口在生成动态代理的时候,会根据返回值的类型,决定调用selectOne方法还是selectList方法。

  <select id="findUserListByUserAndClassname" parameterType="edu.ccit.cn.domain.pojo.UserQueryVo" resultType="user">
        select * from t_user where classid=(select id from t_class where classname=#{classname}) and username like '%${user.username}%'
    </select>
    多个对象
    <select id="findUserListByMap" parameterType="hashmap" resultType="user">
        select * from t_user where username=#{username} and password=#{password}
    </select>

Map

如果查询出来的列名和属性名不一致,通过定义一个resultMap将列名和pojo属性名之间作一个映射关系。
1、 定义resultMap
2、使用resultMap作为statement的输出映射类型

   <resultMap id="userResultMap" type="user">
        <id column="id_" property="id"></id>
        <result column="username_" property="username"></result>
        <result column="calssid_" property="classid"></result>
    </resultMap>
    <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
        select id id_,username username_,classid classid_ from t_user where id=#{?}
    </select>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值