zoukankan      html  css  js  c++  java
  • 6、ResultMap结果集映射-解决实体类属性名和数据库字段名不一致的问题

    1、根据id查询到用户

     2、如果改变实体类的字段再进行查询,将User类中的pwd改为password

      可以见到查询到的用户密码为空

     

    3、 解决方法:

      第一种、在mapper中给要查询的字段起别名

    <select id="getUserById" resultType="user" parameterType="int">
            /*根据id查询用户*/
            select id,name,pwd as password from mybatis.user where id = #{id};
        </select>

      第二种、使用结果集映射

        <!--结果集映射-->
        <resultMap id="userMap" type="user"> <!--user是给实体类起的别名,别弄混淆-->
            <!--column:数据库中的字段 property:实体类中的属性-->
            <result column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="pwd" property="password"/>
        </resultMap>
    
        <select id="getUserById" resultMap="userMap"  parameterType="int">
            /*根据id查询用户*/
            select * from mybatis.user where id = #{id};
        </select>

    • 上面resultMap的配置如果:数据库字段跟实体类属性的名称相同,可以不写:
    
    
    
    

     

  • 相关阅读:
    protobuf 中的嵌套消息的使用
    Mysql++详解
    MYSQL++之Connect类型
    c/c++中宏定义##连接符 和#符的使用
    c指针
    linux学习历程
    linux sar 命令详解
    Linux下多线程查看工具(pstree、ps、pstack)
    知识杂项
    python 使用xrld
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14201316.html
Copyright © 2011-2022 走看看