zoukankan      html  css  js  c++  java
  • 重学Mybatis从入门到源码之六---解决属性名和字段名不一致的问题

    如果数据库中的字段名和代码中的属性名不一致,这很容易就不一致,因为代码中一般都是驼峰,数据库里面可能会有下划线。

    1. 给字段名起别名:

    <select id="selectUsers" resultType="User">
      select
        user_id             as "id",
        user_name           as "userName",
        hashed_password     as "hashedPassword"
      from some_table
      where id = #{id}
    </select>

    2. 使用resultMap

    定义一个resultMap:

    <resultMap id="userResultMap" type="User">
      <id property="id" column="user_id" />
      <result property="username" column="username"/>
      <result property="password" column="password"/>
    </resultMap> 

    使用这个result Map:

    <select id="selectUsers" resultMap="userResultMap">
      select user_id, user_name, hashed_password
      from some_table
      where id = #{id}
    </select>

    resultMap也不是要将所有的字段名都做一个映射,只写一下不一样的字段也是可以的。

  • 相关阅读:
    函数宏实现循环左移
    函数宏判断小端
    Linux下32位与64位数据类型大小
    转:C语言嵌入式系统编程之软件架构篇
    转:详解大端小段模式
    time函数计算时间
    匈牙利命名法
    20131030
    20131029
    20131028
  • 原文地址:https://www.cnblogs.com/yunyunde/p/13826534.html
Copyright © 2011-2022 走看看