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 ...
随机推荐
- .NET入行之工作后
成长这条路上,有一些事已经渐渐的消失模糊的脑海里,而有一些事,则历历在目,终生铭记. 一切都是从大二下学期快结束的那段培训课程开始的,从此也算是入门了,这也是一条不归路. 在.NET入行之工作前一文中 ...
- SourceTree 简单使用 for Mac
系统版本: 10.12.6 SourceTree版本:2.3.1 (中文版)SourceThree安装包 密码:9jc3 传送门 目录 1.创建gitHub账号和仓库 2.sourceTree管理gi ...
- 入侵拿下DVBBS php官网详细过程(图)
几 个月前,DVBBS php2.0暴了一个可以直接读出管理员密码的sql注入漏洞,当时这个漏洞出来的时候,我看的心痒,怎么还会有这么弱智的漏洞,DVBBS php2.0这套代码我还没仔细看过,于是5 ...
- 安装node/npm/webpack步骤
nodejs软件的下载地址:https://nodejs.org/en/ 1.只要安装好了nodejs,就自动安装好了npm包. 2.在cmd中通过命令node -version查看是否安装好node ...
- 解析:type t_string is table of varchar2(32767) index by binary_integer
@ 理解一: table 相当于是数组,这里定义了一个数组类型t_string; INDEX BY BINARY_INTEGER这里是定义数组下标是整数,因为ORACLE中下标可以是字符串. VARC ...
- maven 随笔
<build> <plugins> <!--打包源代码--> <plugin> <artifactId>maven-source-plugi ...
- Gvim安装nerd_tree插件
1.先去官网下载nerd_tree插件 http://www.vim.org/scripts/script.php?script_id=1658 2.解压缩将nerd_tree目录下的doc目录和pl ...
- Java面试系列之HashMap大扫盲汇总
PS:整理的稍微有点急,不足之处,望各路道友指正,List相关可以查看前一篇随笔! HashMap的工作原理是近年来常见的Java面试题,几乎每个Java程序员都知道HashMap,都知道哪里要用Ha ...
- Spring定时任务quartz表达式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp32 0 0 12 * * ?---------------在每天中午12: ...
- VB与C#语言部分不用的地方Part1
1. 数据类型: ① 日期型(Date) 表示日期和时间用两个“#”符号把日期和时间的值括起来,如:#08/20/2001#.#2001-08-20#. ② 变体型(Variant) ...