mapper映射文件中resultMap和resultType的区别

使用mybatis进行数据库连接时对于select语句返回结果的处理方式有两种:resultMap和resultType。

  • resultType
    使用resultType时,对于select语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。

具体实例

  <select id="findAdmin" resultType="test.bean.Admin" >
        select * from tb_admin where id=#{id}
    </select>
  • resultMap
    当使用resultMap做select语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。

当我们遇到在实体类中的字段名和数据库中的字段名不一致时,resultMap就可以帮助我们将数据库字段名和本类字段名相应射。

具体实例

<resultMap id="UserType" type="test.entity.User">
<!-- id 唯一标识符
     cloumn 简单说就是数据库表的字段名
     property 就是User类中和要数据库表相对应的属性名
-->
        <id property="id" column="id"></id>
        <result property="username" column="account"></result>
    </resultMap>

    <select id="findUser" resultMap="UserType" >
        select id,account,password from tb_user where account=#{user.account} and password=#{user.password}
   </select>

注意:resultType跟resultMap不能同时存在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值