• mybatis ResultMap结果集映射


    下面代码:

    根据ID查用户 ,那么:

    如果我们把它User中的 birthday改为 birth,那么就会出现下面问题【两种情况】:

    1. birth 为空 例如: 

    所以最简单的结局方法就是 语句加别名:

     所以证实了 数据库字段 一定要和返回类型User中的变量名一样! 其次查询出来是这样的: 【下面时间被我改过....】

     


    最笨的办法是起别名AS  其次就是用 ResultMap:

    <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    
        <mapper namespace="com.bihu.Dao.UserMapper">
        <!--创建一个resultMap-->
    <resultMap id="testResultMap" type="com.bihu.Bean.User">
        <!--下面的column是数据库字段名 property是Bean的字段-->
        <!--相当于手动把数据库数据封装到JavaBean字段中-->
        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="birthday" property="birth"/>  <!--主要是这个 因为数据库和Bean成员变量名对不上-->
    </resultMap>
        <select id="findId" resultMap="testResultMap">
            select *
            from USER where id = #{id};
        </select>
        </mapper>
    UserMapper.xml

    这就是这个作用【之前在 一对一查询用过这个  也是手动封装类】

    但是你可以把 数据库字段 和 JavaBean成员变量名不一样的才Result 一下 ,例如:

    运行发现 结果也是一样的。

    这就是 结果集  resultMap 最最最简单的使用 【解决字段名不一样】

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15220958.html

  • 相关阅读:
    ApiKernel
    ApiUser
    BringWindowToTop完美激活窗口与置顶
    poj2486 Apple Tree【区间dp】
    HYSBZ1588 营业额统计【Splay】
    hdu5115 Dire Wolf【区间dp】
    poj1144 Network【tarjan求割点】
    poj1236 Network of Schools【强连通分量(tarjan)缩点】
    poj2342 Anniversary party【树形dp】
    poj2449 Remmarguts' Date【A*算法】
  • 原文地址:https://www.cnblogs.com/bi-hu/p/15220958.html
Copyright © 2020-2023  润新知