MyBatis属性名和列名不一致

属性名和列名不一致
  • entity类
  • mysql表单

  • 问题: 名称不同的属性无法查询

    • 原因: MyBatis会根据查询的列名,为entity对象设置(调用列名的set方法)
    • 解决方法:

      • 为列名指定别名,别名和entity类的属性名一致

        <select id="selectUser" resultType="User">
            select id, name, pwd password from user where id = #{id}
        </select>
      • 设置结果映射类型

        <select id="selectUser" resultMap="Usermp">
            select id, name, pwd from user where id = #{id}
        </select>
        
        <resultMap type="User" id="Usermp">
            <!-- id为主键 -->
            <id column="id" property="id"/>
            <!-- column为数据库中表格列名,property为entity属性名 -->
            <result column="name" property="name"/>
            <result column="pwd" property="password"/>
        </resultMap>

猜你喜欢

转载自blog.csdn.net/weixin_40683252/article/details/81085557