mybatis_SQL映射(3)
文章摘录自:http://blog.csdn.net/y172158950/article/details/17304645
1. 表关联
a) 嵌套查询(传说中的1+N问题)
- <resultMap id="userResult3" type="User">
- <association property="role" column="role_id" javaType="Role" select="selectRole"/>
- </resultMap>
- <select id="selectUser2" parameterType="int" resultMap="userResult3">
- select _id id, _name name, _password password, _role_id role_id from _user where _id = #{id};
- </select>
- <select id="selectRole" parameterType="int" resultType="Role">
- select _id id, _name name, _grade grade from _role where _id = #{id};
- </select>
i. 是role_id[sleect结果集column别名],不是_role_id[_user表column]。
b) 嵌套结果
- <resultMap id="userResult" type="User">
- <constructor>
- <idArg column="u_id" javaType="int"/>
- </constructor>
- <result property="name" column="u_name" />
- <result property="password" column="u_password" />
- <association property="role" column="r_id" javaType="Role">
- <id property="id" column="r_id"/>
- <result property="name" column="r_name"/>
- <result property="grade" column="r_grade"/>
- </association>
- </resultMap>
- <select id="selectUser" parameterType="int" resultMap="userResult">
- select u._id u_id, u._name u_name, u._password u_password,
- r._id r_id, r._name r_name, r._grade r_grade from _user u
- left join _role r on u._role_id=r._id where u._id =#{id};
- </select>
i. 别名的意义:每个字段名称必须唯一,重名的情况会照成错误的返回。[太土了]
ii.蛋疼的构造方法:<constructor>定义了javabean的构造方法
- public User(Integer id) { //此处必须写Integer,写int报错
- this.id = id;
- }
iii. 另外一种写法:resultMap的重用
- <resultMap id="userResult" type="User">
- <constructor>
- <idArg column="u_id" javaType="int"/>
- </constructor>
- <result property="name" column="u_name" />
- <result property="password" column="u_password" />
- <association property="role" column="r_id" resultMap="roleResult">
- </association>
- </resultMap>
- <resultMap id="roleResult" type="Role">
- <id property="id" column="r_id" />
- <result property="name" column="r_name"/>
- <result property="grade" column="r_grade"/>
- </resultMap>
2. 集合的用法
- <select id="selectUser4" parameterType="int" resultType="User">
- select _id id, _name name, _password password, _role_id role_id from _user where _role_id = #{id};
- </select>
- <select id="selectRole4" parameterType="int" resultMap="selectRole4">
- select _id id, _name name, _grade grade from _role where _id = #{id};
- </select>
- <resultMap id="selectRole4" type="Role">
- <collection property="users" column="id" ofType="User" select="selectUser4"/>
- </resultMap>
i. 仍要注意,resultMap中column都是查询结果集的别名
mybatis_SQL映射(3)的更多相关文章
- mybatis_SQL映射(4)鉴别器
摘录自:http://blog.csdn.net/y172158950/article/details/17505739 鉴别器:有时一个单独的数据库查询也许返回很多不同(但是希望有些关联)数据类型的 ...
- mybatis_SQL映射(1)
文章摘录自:http://blog.csdn.net/y172158950/article/details/17258377 1. select的映射 <select id="sele ...
- mybatis_SQL映射(2)
文章摘录自:http://blog.csdn.net/y172158950/article/details/17258377 1. sql的重用:定义一个sql片段,可在任何SQL语句中重用该片段. ...
- Hibernatel框架关联映射
Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...
- hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- .NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper
好久没有写文章,工作甚忙,但每日还是关注.NET领域的开源项目.五一休息,放松了一下之后,今天就给大家介绍一个轻量级的对象映射工具Tiny Mapper:号称是.NET平台最快的对象映射组件.那就一起 ...
- ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系
ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...
随机推荐
- Ubuntu16.04 IDE: 用Vim逐步打造一个IDE
目前打造完成的IDE主要有: terminator+Bundle+NERDtree+YCF(youcompleteme)+UltiSnips+新创建文件自动补充注释和作者,版权信息等 1,当任务比较多 ...
- [js高手之路] vue系列教程 - 绑定class与行间样式style(6)
一.绑定class属性的方式 1.通过数组的方式,为元素绑定多个class <style> .red { color:red; /*color:#ff8800;*/ } .bg { bac ...
- Java工程读取resources中资源文件路径问题
正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径,即相对于当前类的路径.在本地工程和服务器中读取文件的方式有所不同,以下图配置文件为例. 本地读取资 ...
- 标准模式 怪异模式 盒模型 doctype
在页面顶部设置 doctype是为了统一标准 浏览器有标准模式和怪异模式 而这两种模式最大区别就是 盒模型的解析不同 ============================== 图片摘自网络 === ...
- onoffswitch-checkbox
@foreach (EmailSubscription es in Model) { if(true){ <div class="onoffswitch"> ...
- console报错类型
常见 console报错 Error 错误 EvalError 全局错误 RangeError 引用(范围)错误 ReferenceError 参数(参考)错误 SyntaxError 语法错误 ty ...
- 【转载】LINUX上MYSQL优化三板斧
现在MySQL运行的大部分环境都是在Linux上的,如何在Linux操作系统上根据MySQL进行优化,我们这里给出一些通用简单的策略.这些方法都有助于改进MySQL的性能. 闲话少说,进入正题. 一. ...
- 深入理解JVM(五)——垃圾回收器
轻松学习JVM(五)——垃圾回收器 上一篇我们介绍了常见的垃圾回收算法,不同的算法各有各的优缺点,在JVM中并不是单纯的使用某一种算法进行垃圾回收,而是将不同的垃圾回收算法包装在不同的垃圾回收器当中, ...
- echarts3 中 热力图的属性大全
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 迭代器中next()的用法
>>> g = (x ** 2 for x in range(10)) >>> next(g) 0 >>> next(g) 1 >>& ...