关于mybatis:Mybatis中ResultMap和ResultType的区别

根本映射 :(resultType)应用resultType进行输入映射,只有查问进去的列名和pojo中的属性名统一,该列才能够映射胜利。(数据库,实体,查问字段,,这些全副都得一一对应)

<select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">  
        select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'  
    </select> 

高级映射 :(resultMap) 如果查问进去的列名和pojo的属性名不统一,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。(高级映射,字段名称能够不统一,通过映射来实现)

<!--
        自定义映射规定:resultMap标签来实现映射规定的定义
        
        id属性:标签给这个映射负责调配一个惟一的id'值,对应就是resultMap=“id属性的值”
    -->
    <resultMap id="UserEntityMap" type="com.xiaoan.store.pojo.User">
        <!-- 将表的资源和类的属性不统一的字段进行匹配指定,名称统一的字段能够省略不写 -->
        <result column="is_delete" property="isDelete"></result>
        <result column="created_User" property="createdUser"></result>
        <result column="created_time" property="createdTime"></result>
        <result column="modified_user" property="modifiedUser"></result>
        <result column="modified_time" property="modifiedTime"></result>
    </resultMap>


<!--
        User findByUsername(String username)
        resultType:示意查问后果集类型,只须要指定对应映射类的类型
        resultMap:标签当表的资源和类的对象属性字段不统一时,来自定义查问后果集的映射规定
    -->
    <select id="findByUsername" resultMap="UserEntityMap">
        select username from t_user where username = #{username}
    </select>

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据