zoukankan      html  css  js  c++  java
  • MyBatis结果集处理,中resultType和resultMap的区别

    http://blog.csdn.net/leo3070/article/details/77899574


    使用resultType

    <select id="selectUsers" parameterType="int" resultType="com.someapp.model.User">
      select id, username, hashedPassword
      from some_table
      where id = #{id}
    </select>

    这些情况下,MyBatis 会在幕后自动创建一个 ResultMap(其实MyBatis的每一个查询映射的返回类型都是ResultMap),基于属性名来映射列到 JavaBean 的属性上

    如果列名没有精确匹配,你可以在列名上使用 select 字句的别名(一个 基本的 SQL 特性)来匹配标签。比如:


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


    resultType可以直接返回给出的返回值类型,比如String、int、Map,等等,其中返回List也是将返回类型定义为Map,然后mybatis会自动将这些map放在一个List中,resultType还可以是一个对象-属性名自动映射


    使用resultMap

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

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

    外部resultMap的type属性表示该resultMap的结果是一个什么样的类型, esultMap节点的子节点id是用于标识该对象的id的,而result子节点则是用于标识一些简单属性的,其中的Column属性表示从数据库中查询的属性,Property则表示查询出来的属性对应的值赋给实体对象的哪个属性。




    注意:用resultType的时候,要保证结果集的列名与java对象的属性相同,而resultMap则不用,而且resultMap可以用typeHander转换



  • 相关阅读:
    os 模块
    time-时间模块
    hashlibloggingconfigparser
    模块-json,pickle
    转载:执行脚本出现bin/bash: bad interpreter: No such file or directory
    转载:如何查看用户当前shell和修改用户登陆时的默认shell
    转载:对#!/bin/sh的认识
    转载:mysql-Auto_increment值修改
    转载:Tomcat多数据源配置方法
    转载:struts2和spring的结合原理(精品)
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106673.html
Copyright © 2011-2022 走看看