mybatis - [11] ResultMap结果集映射
题记部分
用于处理数据库中的字段名和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结果集映射的更多相关文章
- Mybatis学习笔记-ResultMap结果集映射
解决属性名与字段名不一致的问题 新建项目 --> 测试实体类字段不一致的情况 数据库字段:id,name,pwd 实体类属性:id,name,password 输出结果 User{id=1, n ...
- Mybatis 强大的结果集映射器resultMap
1. 前言 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC ...
- Mybatis的ResultMap的使用(转)
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- 使用mybatis的resultMap进行复杂查询
记录下mybatis的集合查询中碰到的问题 https://jaychang.iteye.com/blog/2357143 MyBatis ofType和javaType区别 https: ...
- resultMap结果集映射解决属性名和字段不一致问题
解决属性名和字段名不一致的问题 1.出现的问题 数据库中的字段 新建一个项目,拷贝之前的,测试实体类与数据库字段不一致的情况 public class User { private int id; ...
- Mybatis的ResultMap的使用
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- 使用MyBatis的resultMap高级查询时常用的方式总结
以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...
- 【MyBatis】ResultMap
[MyBatis]ResultMap 转载:https://www.cnblogs.com/yangchongxing/p/10486854.html 支持的 JDBC 类型为了未来的参考,MyBat ...
- mybatis 使用resultMap实现表间关联
AutoMapping auto mapping,直译过来就是自动映射,工作原理大概如下: 假设我们有一张表,表名为person,包含id,name,age,addr这4个字段 mysql> d ...
- Mybatis:resultMap的使用总结
resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...
随机推荐
- ArkTs布局入门05——栅格布局(GridRow/GridCol)
1.概述 栅格布局是一种通用的辅助定位工具,对移动设备的界面设计有较好的借鉴作用.主要优势包括: 提供可循的规律:栅格布局可以为布局提供规律性的结构,解决多尺寸多设备的动态布局问题.通过将页面划分为等 ...
- Linux FTP 服务搭建
Linux FTP 服务搭建 1.安装vsftp 1.1.使用yum进行安装vsftp [root@localhost ~]# yum -y install vsftpd 1.2.配置文件目录 [ro ...
- 【Java】【Maven】002 修改maven仓库的路径与配置阿里云镜像仓库
[Java][Maven]001 下载与配置环境 - 萌狼蓝天 - 博客园 (cnblogs.com/mllt) 修改maven仓库的路径 maven指定的本地仓库的默认位置是在c盘,默认在:C:\U ...
- 【前端】解决盒子被撑大问题 box-sizing
设置 box-sizing:border-box(原本的默认值为:content-box) box-sizing: content-box;/*盒子宽度=CSS中设置的width+border+pad ...
- TypeNameExtractor could not be found
TypeNameExtractor could not be foundException 一.注意: 如果项目中使用了 knif4j或swagger (knif4j内核中就使用了swagger) 项 ...
- Base64解码、Base64编码、Base64加密解密规则
转换规则:进行Base64转换的时候,将3个byte(3*8bit = 24bit)的数据,先后放入一个24bit的缓冲区中,先来的byte占高位.数据不足3byte的话,于缓冲器中剩下的bit用0补 ...
- Linux 下挂载群晖 NAS 硬盘拓展空间(NFS)
在群晖 NAS 上配置 开启 NFS 服务 打开群晖的 设置 - 文件服务 - NFS,把 NFS 服务勾选上,协议选最新即可. 配置文件夹的 NFS 权限 在 设置 - 共享文件夹 下,选择需要开启 ...
- uniapp同城社区交友 仿小红书 APP小程序源码 含后台管理和网页端
注意(预防被骗) 本程序仅在 破晓店铺(https://shop.abyssdawn.com/).破晓一代网络科技淘宝店 出售其余地方均为骗子. 关于本程序 本程序适用于各种同城社区交友类产品,例如同 ...
- Idea中@Autowired 黄色波浪线问题以及注入类显示红色波浪线问题解决
解决办法: 点开路径:File--->Settings--->Editor--->Inspections--->Spring--->Spring core--->C ...
- 即时通讯技术文集(第38期):IM代码入门实践(Part2) [共15篇]
为了更好地分类阅读 52im.net 总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第38 期. [- 1 -] 高仿Android版手机QQ首页侧滑菜单源码 [附件下载] [链接 ...