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不做限制,可以为任意 ...
随机推荐
- Flutter之GetX之GetBuilder
Flutter之GetX之GetBuilder GetX是Flutter的一个非常强力的三方库,包含了非常多的功能,比如状态管理.路由管理.国际化.路由中间件.主题.数据库等等 今天简单介绍一下状态管 ...
- 在 Ubuntu GUI 中以 root 身份登录
参考:https://zhuanlan.zhihu.com/p/610049537?utm_id=0 有一些桌面用户想以 root 身份登录.这不是什么明智之举,但肯定是可以做到的. 默认情况下,Ub ...
- Linux 添加开机自启动
rc.local 方式 一.& 在 Linux 命令后加上 & 可以在后台运行 二.nohup 对 SIGHUP 信号免疫,对 SIGINT 信号不免疫,可用 shopt | gre ...
- mac 10.15 国内如何安装brew
下载文件 brew_install.sh,然后执行 sh brew_install.sh 通常会卡在 tapping homebrew/core ,没关系, 执行如下命令即可 解决方法,手动执行下面 ...
- Qt编写地图综合应用58-兼容多浏览器内核
一.前言 miniblink是一个追求极致小巧的浏览器内核项目,全世界第三大流行的浏览器内核控件.其基于chromium最新版内核,去除了chromium所有多余的部件,只保留最基本的排版引擎blin ...
- SHA256 64 位加密
/// <summary> /// SHA256 64位加密 /// </summary> /// <param name="input">&l ...
- [转]vue项目启动时,npm run serve 和 npm run dev 的区别
npm run serve 和 npm run dev 的区别 在我们运行一些 vue 项目的时候,输入npm run serve或者 npm run dev的其中一个时,系统会报错: PS C:\U ...
- 隐藏zsh前面用户名或者主机名
1. zshrc 修改 vim ~/.zshrc文件,在文件底部增加: prompt_context() {} 只保留用户名,隐藏主机名 prompt_context() { if [[ " ...
- vue实现自定义音乐播放器
项目中遇到了,让实现一个音乐播放器的功能.修改其样式要求自定义,切需要有,进度条,时间,开关,应用于H5需要兼容ios与android.简单看一下如图播放器 完成代码 audioCom.vue < ...
- SQL统计数据之总结
一.查询SQL SELECT t1.规则编号 AS 编码, t1.规则描述 AS 名称, SUM( CASE WHEN t3.DATA_SOURCES = '00' THEN 1 ELSE 0 END ...