mybatis-resultMap使用与详解
1,当数据库的字段名与属性名称不一致时,在mybatis中如何处理?
第一种方式: 采用投影对字段重命名<select id="load" parameterType="int" resultMap="addressMap">
select * user_id as userId from t_address where id=#{id}
</select>
第二种方式: 使用resultMap
<resultMap type="Address" id="addressMap">
<!-- id是用来对主键映射的 -->
<!-- result是用来对一般属性映射的 -->
<!-- 只需要对不一致的字段进行映射,其它的没有必要, id一致是,可以不映射 -->
<id column="id" property="id"/>
<result column="postcode" property="postcode"/>
</resultMap>
<select id="load" parameterType="int" resultMap="addressMap">
select * from t_address where id=#{id}
</select>
2,resultMap中除了前面的id result两个属性之外,还有很多有趣的属性,
2.1 association取关联对象 (外键关联)
第一种方式 会发N+1条sql,不可取
<resultMap type="Address" id="addressMap">
<!-- association 用来作关联 property属性:要关联的对象名称 column属性:用哪一列进行关联
javaType属性: 要关联的对象类型 select属性:执行的sqlId
-->
<!-- association最大的问题就是取关联对象时,会发N条sql 因此以下取关联对象的方式不会使用的 -->
<association property="user" column="user_id" javaType="User"
select="com.yangwei.shop.entity.User.load" />
</resultMap>
<select id="load" parameterType="int" resultMap="addressMap">
select * from t_address where id=#{id}
</select>
第二种方式 只会发1条sql ,请使用这种 注意sql的写法 <resultMap type="Address" id="addressMap" autoMapping="true">
<!-- 设置了autoMapping之后,自己对象的属性会完成自动映射 -->
<id column="a_id" property="id" /> <-- 取关联对象-->
<association property="user" javaType="User">
<!-- 这里面的字段都是user表中的,全部必须手动映射,没有映射的将是null -->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="passwd" property="passwd"/>
<result column="nickname" property="nickname"/>
<result column="type" property="type"/>
</association>
</resultMap>
<select id="load" parameterType="int" resultMap="addressMap">
select *,t1.id as a_id from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id=#{id}
</select>
第三种方式: 与第二种差不多,只是将结果resultMap单独出来<resultMap type="Address" id="addressMap" autoMapping="true">
<!-- 设置了autoMapping之后,自己对象的属性会完成自动映射 -->
<id column="a_id" property="id" />
<association property="user" javaType="User" resultMap="userMap" />
</resultMap>
<resultMap type="User" id="userMap">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="passwd" property="passwd"/>
<result column="nickname" property="nickname"/>
<result column="type" property="type"/>
</resultMap>
<select id="load" parameterType="int" resultMap="addressMap">
select *,t1.id as a_id from t_address t1 right join t_user t2 on(t1.user_id=t2.id) where t1.id=#{id}
</select>
2.2 collection 获取对象中关联的集合数据
<resultMap type="User" id="userMap" autoMapping="true">
<!--User类中有 List<Address> address属性;-->
<!--column写不写都无所谓-->
<!--ofType必须设置 List里面的类型--> <id column="user_id" property="id"></id> <!--下面的查询会有两个id,需要指明用哪一个映射-->
<collection property="address" column="user_id" ofType="Address">
<id column="a_id" property="id" />
<result column="name" property="name"/>
<result column="postcode" property="postcode"/>
<result column="detail" property="detail"/>
<result column="phone" property="phone"/>
</collection>
</resultMap>
<select id="load" parameterType="int" resultMap="userMap">
select *,t2.id as a_id from t_user t1 left join t_address t2 on(t1.id=t2.user_id) where t1.id=#{id}
</select>
mybatis-resultMap使用与详解的更多相关文章
- MyBatis中@MapKey使用详解
MyBatis中@MapKey使用详解我们在上一篇文章中讲到在Select返回类型中是返回Map时,是对方法中是否存在注解@MapKey,这个注解我也是第一次看到,当时我也以为是纯粹的返回单个数据对象 ...
- idea spring+springmvc+mybatis环境配置整合详解
idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...
- Mybatis SQL映射文件详解
Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...
- Mybatis系列全解(四):全网最全!Mybatis配置文件XML全貌详解
封面:洛小汐 作者:潘潘 做大事和做小事的难度是一样的.两者都会消耗你的时间和精力,所以如果决心做事,就要做大事,要确保你的梦想值得追求,未来的收获可以配得上你的努力. 前言 上一篇文章 <My ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- Mybatis(三) 映射文件详解
前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...
- Mybatis代码生成器Mybatis-Generator使用详解
前提 最近在做创业项目的时候因为有比较多的新需求,需要频繁基于DDL生成Mybatis适合的实体.Mapper接口和映射文件.其中,代码生成器是MyBatis Generator(MBG),用到了My ...
随机推荐
- Ubuntu Docker 安装和配置 GitLab CI 持续集成
相关文章: Ubuntu Docker 简单安装 GitLab 劈荆斩棘:Gitlab 部署 CI 持续集成 目的:在 Ubuntu 服务器上,使用 Docker 安装和配置 GitLab Runne ...
- axios.js
Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...
- JavaScript 日期处理类库
Moment.js http://momentjs.cn/
- java web方面的面试问题,Spring MVC方面的面试问题,摘自java web轻量级开发面试教程
本文摘自java web轻量级开发面试教程: https://baike.baidu.com/item/Java%20Web%E8%BD%BB%E9%87%8F%E7%BA%A7%E5%BC%80%E ...
- $MarkDown$ 中使用$ \LaTeX$ 数学式
最近看了些机器学习的书籍, 想写点笔记记录下. 由于需要使用到很多的数学推导, 所以就看了下如何在 Markdown 中插入数学式,发现在 Markdown 中可以直接插入 LaTeX 数学式. 排版 ...
- 问题: 数据流中位数 求解 时间复杂度度 java
今天练习了一题: 数据流中位数 问题描述:数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. 案例: 持续进入数组的数的列表为:[1, 2, 3, 4, 5],则返回[1 ...
- python基础教程(四)
列表 本节继续讨论列表不同元组和字符串的地方:列表是可变的(mutable)----可以改变列表的内容,并且列表有很多有用的.专门的方法. List函数可以将一个字符串拆分成列表. >>& ...
- [2014-08-24]为 Xamarin Studio 创建的 Asp.Net Mvc 项目配置 gitignore
今天在尝试 Mac 下使用 Xamarin Studio (以下简称XS) 开发 Asp.Net Mvc 项目,发现XS没启用版本控制,故自己去命令行下使用 git init,想到需要一个.gitig ...
- Python Counter class
Counter class https://docs.python.org/2/library/collections.html#collections.Counter # class collect ...
- union-find算法Java实现
package practice; /*在一个全是点的图中,循环选择两点连通,之后判断两点是否在同一通路*/ public class Testmain { public static void ma ...