题记部分

用于处理数据库中的字段名和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. Grid 网格布局备忘录

    概述 网格布局(Grid)是最强大的 CSS 布局方案. 它将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局. Grid 布局与 Flex 布局有一定的相似性,都可以指定容器内部多个 ...

  2. c++的OJ练习一

    1.1599.米老鼠偷糖果 2.1323.扩建花圃问题 3.P1425.小鱼的游泳时间 4.1598.文具店的折扣 5.P5706.再分肥宅水 6.1345.玫瑰花圃 7.P3954.NOIP2017 ...

  3. 藏不住了,这届数字打工人(RPA)想在各行各业“当骨干”!

    数字化时代,最红的"打工人"是谁? 无疑,是RPA(数字化劳动力). 这种由机器人流程自动化+AI驱动,模拟人点击.移动鼠标等在电脑上的操作,在各种规则明确.高重复度业务场景中代替 ...

  4. TypeScript 源码详细解读(2)词法1-字符处理

    本节文章研究的代码位于 tsc/src/compiler/scanner.ts 字符 任何源码都是由很多字符组成的,这些字符可以是字母.数字.空格.符号.汉字等-- 每一个字符都有一个编码值,比如字符 ...

  5. 渗透测试-前端加密分析之RSA+AES

    本文是高级前端加解密与验签实战的第8篇文章,本系列文章实验靶场为Yakit里自带的Vulinbox靶场,本文讲述的是绕过RSA与AES加密来爆破登录. 由于RSA加解密有长度限制,以及解密速度等问题, ...

  6. 【C#】【答卷】萌狼蓝天大二上学期期末C#考试复习卷(样题)

  7. Springboot+Vue进行Web开发时特别需要注意的小事项

    Springboot+Vue进行Web开发时特别需要注意的小事项: 1.在测试页面效果时,如果没有特别设置安全Http访问,在输入url请求测试网页时,只能使用http://......,而不能使用h ...

  8. 墨卡托及Web墨卡托投影解析

    Google Maps.Virtual Earth等网络地理所使用的地图投影,常被称作Web Mercator(Web墨卡托投影)或Spherical Mercator(球面墨卡托投影),它与常规墨卡 ...

  9. 揭秘企业微信是如何支持超大规模IM组织架构的——技术解读四维关系链

    本文由序员先生分享,原题"技术解读企业微信之四维关系链",本文有修订和改动. 1.引言 3年疫情后的中国社会,最大的永久性变化之一,就是大多数的企业.教育机构或者政务机构,都用上了 ...

  10. 基于Netty,从零开发IM(二):编码实践篇(im单聊功能)

    本文由作者"大白菜"分享,个人博客 cmsblogs.cn,有较多修订和改动.注意:本系列是给IM初学者的文章,IM老油条们还望海涵,勿喷! 1.引言 接上篇<IM系统设计篇 ...