题记部分

用于处理数据库中的字段名和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. chrome浏览器设置允许跨域

    前情 在访问测试搭建的测试环境的时候,发现接口因为跨域全部失败了,服务端又不想设置允许跨域,又急于使用,于是想到是不是可以使用跨域浏览器 放开chrome的跨域设置步骤 复制一个chrome快捷图标, ...

  2. vue3和elements创建应用

    一. 创建环境 1. 创建D:\code\vue 文件夹 2. vscode打开文件夹 3. 打开终端,输入 npm install -g @vue/cli 4. 配置环境变量 终端输入:npm co ...

  3. 中电金信:GienTech动态|一波好消息→中标!多领域“开花”

  4. blender low poly + unity 3d游戏制作

    会是一个有趣的方向,适合独立游戏制作人,独立动画电影制作人.

  5. 【前端】【样式】CSS居中的三种方式

    @charset "utf-8"; /* CSS Document */ /** *开发者:萌狼蓝天 *当前版本:v0.1[Debug] *最后更新日期:20210918 **/ ...

  6. 配置YUM源出现Errno 14 Could not open/read repomd.xml 或者 "Couldn't open file /mnt/cdrom/repodata/repomd.xml" 错误的解决办法

    报错信息: [root@tcljr-jdh-uat007 yum.repos.d]# yum makecache Loaded plugins: fastestmirror Loading mirro ...

  7. Spring Boot logback springProperty 设置默认值

    springProperty 当没有读取到source字段中设置的log.path值时,设置为defaultValue字段中的${user.dir}/logs变量值. <springProper ...

  8. 冒泡排序------python实现

    if __name__ == '__main__': ''' 算法描述 1.比较相邻的元素,更具大小交互位置 2.对每一对相邻元素作同样的工作,从开始第一队到结尾的最后一对,即可选出最大的数 3.所有 ...

  9. Qt开发经验小技巧151-155

    当Qt中编译资源文件太大时,效率很低,或者需要修改资源文件中的文件比如图片.样式表等,需要重新编译可执行文件,这样很不友好,当然Qt都给我们考虑好了策略,此时可以将资源文件转化为二进制的rcc文件,这 ...

  10. MIPI/LVDS/PCIE/HDMI 设计规范

      参考链接: 1.MIPI/LVDS/PCIE/HDMI 2.接口简介(HDMI .eDP/DP.LVDS.VGA.YPbPr.DVI.MHL.MIPI-DSI.VbyOneHS) 3.干货 | 带 ...