zoukankan      html  css  js  c++  java
  • MyBatis 的小细节问题

    mybatis Result Maps collection already contains value 

    这是个小功能,我当时没有怎么在意,后来发到了测试环境的时候测试提出了bug,我才慌忙查看原因,


    <select id="getRoleInfo" parameterType="java.lang.String" resultMap="
    com.hupu.smart.game.platform.dto.common.RoleInfo" >
            SELECT r.id,r.`name` FROM gamedb.game_platform_role r,gamedb.game_platform_user_role u
    WHERE u.role_id = r.id AND u.user_id = #{userId}
    </select>


     这是我最开始写的mapper  , 查询是两个表的连接查询,返回的结果我是用一个dto对象来封装的,后来我发现,如果这 个对象的字段和数据库的字段 一样的话,就可以用resultMap="RoleInfo"  ,当实体类的字段存在和数据库的字段不一样的情况的时候,就不能用resultMap 来封装查询出的数据了。我在调试的时候看到List是有三条数据,但是数据的内容是看不到的,我当时以为有数据及可以了。可是。。

       后来,这个问题以bug的形式提交给我的时候,我才发现原来是数据有问题,我debugger 的时候查看 返回的list 提示 “”所有基础都是空的“” 

    后来 我把mapper  添加了一个resultMap   

    <resultMap type="com.hupu.smart.game.platform.dto.common.RoleInfo" id="roleMap">
    <id column="id" property="roleId"/>
    <result column="name" property="roleName"/>
    </resultMap>

    <select id="getRoleInfo" parameterType="java.lang.String" resultMap="roleMap" >
    SELECT r.id,r.`name` FROM gamedb.game_platform_role r,gamedb.game_platform_user_role u
    WHERE u.role_id = r.id AND u.user_id = #{userId}
    </select>

    同时 resultType也要改成resultMap 这样才能完全正确的显示数据
  • 相关阅读:
    制作Windows Server 2008安装启动U盘
    wireshark教程(一)
    TCPdump抓包命令详解
    ATM交换机 和普通交换机区别
    胖ap和瘦ap区别
    酒店网络非常常见故障一例
    JQuery EasyUI DataGrid动态合并(标题)单元) 一
    字典表左右选择
    treegrid-dnd.js
    MySQL开发规范
  • 原文地址:https://www.cnblogs.com/murong/p/5826278.html
Copyright © 2011-2022 走看看