题记部分

用于处理数据库中的字段名和Java实体类中的属性名不一致的问题

数据库中的字段为id,name,pwd。Java实体类属性为id,name,password。

在映射文件中select标签使用的resultType就不适用了。需要使用resultMap

step1:配置resultMap

<resultMap id="UserMap" type="com.harley.pojo.User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>

column是数据库的字段名称,property是Java实体类属性名称

step2:使用

<select id="getUserById" resultMap="UserMap" parameterType="integer">
select * from mybatis.user where id = #{id}
</select>

注意:resultMap的id和select标签中resultMap的值保持一致。

复杂的语句

<!-- 非常复杂的语句 -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
A.favourite_section as author_favourite_section,
P.id as post_id,
P.blog_id as post_blog_id,
P.author_id as post_author_id,
P.created_on as post_created_on,
P.section as post_section,
P.subject as post_subject,
P.draft as draft,
P.body as post_body,
C.id as comment_id,
C.post_id as comment_post_id,
C.name as comment_name,
C.comment as comment_text,
T.id as tag_id,
T.name as tag_name
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>
<!-- 非常复杂的结果映射 -->
<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int"/>
</constructor>
<result property="title" column="blog_title"/>
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<result property="favouriteSection" column="author_favourite_section"/>
</association>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" javaType="Author"/>
<collection property="comments" ofType="Comment">
<id property="id" column="comment_id"/>
</collection>
<collection property="tags" ofType="Tag" >
<id property="id" column="tag_id"/>
</collection>
<discriminator javaType="int" column="draft">
<case value="1" resultType="DraftPost"/>
</discriminator>
</collection>
</resultMap>

mybatis - [11] ResultMap结果集映射的更多相关文章

  1. Mybatis学习笔记-ResultMap结果集映射

    解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...

  2. Mybatis 强大的结果集映射器resultMap

    1. 前言 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC ...

  3. Mybatis的ResultMap的使用(转)

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  4. 使用mybatis的resultMap进行复杂查询

        记录下mybatis的集合查询中碰到的问题 https://jaychang.iteye.com/blog/2357143   MyBatis ofType和javaType区别 https: ...

  5. resultMap结果集映射解决属性名和字段不一致问题

    解决属性名和字段名不一致的问题 1.出现的问题 数据库中的字段 ​ 新建一个项目,拷贝之前的,测试实体类与数据库字段不一致的情况 public class User { private int id; ...

  6. Mybatis的ResultMap的使用

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  7. 使用MyBatis的resultMap高级查询时常用的方式总结

    以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...

  8. 【MyBatis】ResultMap

    [MyBatis]ResultMap 转载:https://www.cnblogs.com/yangchongxing/p/10486854.html 支持的 JDBC 类型为了未来的参考,MyBat ...

  9. mybatis 使用resultMap实现表间关联

    AutoMapping auto mapping,直译过来就是自动映射,工作原理大概如下: 假设我们有一张表,表名为person,包含id,name,age,addr这4个字段 mysql> d ...

  10. Mybatis:resultMap的使用总结

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

随机推荐

  1. cve-2021-3156-sudo堆溢出简单分析

    调试方式 首先从github下载代码 https://github.com/sudo-project/sudo/archive/SUDO_1_9_5p1.tar.gz 编译 tar xf sudo-S ...

  2. 鸿蒙UI开发快速入门 —— part10: PersistentStorage与Environment

    1.前言 我们在鸿蒙UI开发快速入门 -- part09: 应用级状态管理LocalStorage & AppStorage中已经学习了LocalStorage与AppStorage,但他们都 ...

  3. GraphQL Part V: 字段,参数和变量

    字段 我们对字段已经有了好的起点,我们在 HelloWorldQuery 中有两个字段:hello 和 world.他们都是单值字段. 现在我们可以扩展应用来支持复杂类型.例如,我们想象一下,我们在创 ...

  4. 用 erlang 描述 tcc

    Transaction 视为一个 Actor. start_transaction(Order) -> p1 = spawn(start_order(self, Order.subOrder1) ...

  5. 2024年1月Java项目开发指南8:统一数据返回格式

    有时候返回一个字符串,有时候返回一串数字代码,有时候返回一个对象-- 不过怎么说,我们返回的内容往往具有三个 1.消息代码 code 2.消息内容 msg 3.数据内容 data 接下来,我们要编写一 ...

  6. Qt/C++音视频开发54-视频监控控件的极致设计

    一.前言 跌跌撞撞摸爬滚打一步步迭代完善到今天,这个视频监控控件的设计,在现阶段水平上个人认为是做的最棒的(稍微自恋一下),理论上来说应该可以用5年不用推翻重写,推翻重写当然也是程序员爱干的事情,这个 ...

  7. [转]创建Visual Studio 2019离线安装包

    可以在不同的网络环境和不同的计算机上在线安装微软Visual Studio 2019.微软提供的在线安装工具(Visual Studio web installer)可以让用户在线下载最新版本Visu ...

  8. Failed to run MSBuild command 错误问题解决

    场景:提示:这里简述项目相关背景: CMake 报错 CMake ERROR Failed to run MSBuild command: MSBuild.exe.如下图所示: 问题描述提示:这里描述 ...

  9. Eclipse中如何将web应用部署到tomcat

    第1种方法: 将workspace下的复制到tomcat的webapp下(我看度还是算了,看着都觉得麻烦). 第2种方法:总体思路就是:(1)先为web应用创建Tomcat Server:(2)再将w ...

  10. 快速定位Linux 内核驱动中GPIO冲突

    #全开开kernel log echo "8" > /proc/sys/kernel/printk #打开gpiolib 动态调试 echo 'file gpiolib.c ...