题记部分

用于处理数据库中的字段名和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. LINUX通过STTY命令操作串口设备(LINUX串口操作命令)

    stty(settty,设置tty)命令用于检查和修改当前注册的终端的通信参数 1.显示某个串口参数信息:stty -F /dev/ttySTM6  -a          2.设置某个串口参数信息: ...

  2. C/C++源码扫描系列- Joern 篇

    文章首发于 https://xz.aliyun.com/t/9277 概述 和 codeql,Fortify 相比 Joern不需要编译源码即可进行扫描,适用场景和环境搭建方面更加简单. 环境搭建 首 ...

  3. LeetCode题集-7 - 整数反转

    题目:给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果.如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0. 假设环境不允许存储 ...

  4. Java 基于接口的动态代理

    UserDao接口 package com.pry.cn; public interface UserDao { public int add(int a,int b); public String ...

  5. /etc/rancher/k3s/registries.yaml

    mirrors: "192.168.50.3": endpoint: - "https://192.168.50.3"configs: "192.16 ...

  6. Navicat Premium15安装与激活

    Navicat premium是一款数据库管理工具,是一个可多重连线资料库的管理工具,它可以让你以单一程式同时连线到 MySQL.SQLite.Oracle 及 PostgreSQL 资料库,让管理不 ...

  7. PostgreSQL 的特点

    title: PostgreSQL 的特点 date: 2024/12/24 updated: 2024/12/24 author: cmdragon excerpt: PostgreSQL 是当今最 ...

  8. 即时通讯技术文集(第15期):IM跨平台和社交软件红包技术 [共19篇]

    为了更好地分类阅读 52im.net 总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第15 期. [- 1 -] IM跨平台技术学习(一):快速了解新一代跨平台桌面技术--Elec ...

  9. SpringBoot集成开源IM框架MobileIMSDK,实现即时通讯IM聊天功能

    一.前言 MobileIMSDK 是什么? MobileIMSDK  是一套专门为移动端开发的开源IM即时通讯框架,超轻量级.高度提炼,一套API优雅支持UDP .TCP .WebSocket 三种协 ...

  10. V3Det&Bigdetection下载记录

    V3Det dataset https://opendatalab.com/V3Det BigDetection