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 ...
随机推荐
- 记一次VS Code崩溃的解决(Win10扫描自动回复系统文件)
早上修改Vue.js框架搭建的项目,正高兴着,突然电脑崩溃,重启后VS code打不开,报错如下: DWrite.dll丢失 然后查看了一下 C:\windows\system32\下 DWrite ...
- /etc/profile /etc/bashrc ~/.bash_profile ~/.bashrc ~/.bash_logout 说明及区别
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一 ...
- Springmvc+mybatis的定时器配置文件spring-quartz.xml
<!-- 定时器配置文件---.xml 一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左至右,这些元素的定义如下: 1.秒(0–59) 2.分钟(0–59) 3.小时(0– ...
- PyQt4 初试牛刀一
建立了一个MainWindow,创建最基本的菜单栏.状态栏.工具栏,并重新定义了"X"关闭的默认行为. # -*- coding: utf-8 -*- import sys fro ...
- Struts2简介以及初步搭建配置
一.基本介绍 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互. ...
- java继承和多态举例
public class Test1 { public static void main(String[] args) { System.out.println(new Dog().name);//狗 ...
- HTML添加样式三种办法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux的学习笔记_Day1
为什么要开始学习Linux命令? 首先当然是因为工作需要了,现在的工作是负责银行调度的系统的源系统接入的工作,经常要到生产部署版本.所以--买了一本<Linux命令行与shell脚本编程大全&g ...
- 谈一谈EasyUI中TreeGrid的过滤功能
写在最前面 这个星期一直在纠结easyui的treegrid的过滤功能,原因呢,自然是项目中一个莫名奇妙的需求. easyui虽说是后端程序员的前端框架,但是说句实话,除去api,让我直接写里面的节点 ...
- Java之JMX 详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt194 一.JMX简介 JMX是一种JAVA的正式规范,它主要目的是让程序有被 ...