zoukankan      html  css  js  c++  java
  • 4月3日

    5、解决属性名和字段名不一致的问题

    1. 问题

    数据库中的字段

    在这里插入图片描述

    新建一个项目,拷贝之前的,测试实体类字段不一致的情况

    在这里插入图片描述

    测试出现问题 在这里插入图片描述

    // select * from user where id = #{id}
    // 类型处理器
    // select id,name,pwd from user where id = #{id}
    123

    解决方法:

    • 起别名

    <select id="getUserById" resultType="com.kuang.pojo.User">
      select id,name,pwd as password from USER where id = #{id}
    </select>
    123

    2. resultMap

    结果集映射

    id name pwd

    id name password

    <!--结果集映射-->
    <resultMap id="UserMap" type="User">
       <!--column数据库中的字段,property实体类中的属性-->
       <result column="id" property="id"></result>
       <result column="name" property="name"></result>
       <result column="pwd" property="password"></result>
    </resultMap>

    <select id="getUserList" resultMap="UserMap">
      select * from USER
    </select>
    1234567891011
      • resultMap 元素是 MyBatis 中最重要最强大的元素。

      • ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。

      • ResultMap 的优秀之处——你完全可以不用显式地配置它们。

      • 如果这个世界总是这么简单就好了。

  • 相关阅读:
    POJ 1315 Don't Get Rooked
    POJ 2051 Argus
    POJ 1942 Paths on a Grid
    POJ 2151 Check the difficulty of problems
    POJ 3349 Snowflake Snow Snowflakes
    POJ 1753 Flip Game
    POJ 2392 Space Elevator
    POJ 2385 Apple Catching
    POJ 2356 Find a multiple
    POJ 2355 Railway tickets
  • 原文地址:https://www.cnblogs.com/ldy2396/p/14909208.html
Copyright © 2011-2022 走看看