mybatis处理一对一查询
有班级表,老师表,要求给定班级id查出班级信息和班级对应的老师信息
1、使用嵌套结果方式
sql语句:
<select id="findClasses" parameterType="int" resultMap="findClassesMap">
select c.*,t.t_name from classes c,teacher t where c.t_id = t.t_id and c.c_id =#{id} ;
</select>
<resultMap type="Classes" id="findClassesMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
2、使用嵌套查询方式
select * from class where c_id = #{id}
select * from teacher where t_id = #{teacher_id} //使用上一个查询的结果teacher_id
<select id="findClasses2" parameterType="int" resultMap="findClasses2Map">
select * from classes where c_id=#{id}
</select>
<resultMap type="Classes" id="findClasses2Map">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" select="findTeacher" column="t_id">
</association>
</resultMap>
<select id="findTeacher" parameterType="int" resultType="Teacher">
select t_id id,t_name name from teacher where t_id = #{t_id}
</select>
在association节点中配置两个属性column指上次查询结果哪个字段作为下一次查询的参数条件 select指对应的查询
mybatis处理一对一查询的更多相关文章
- mybatis进阶--一对一查询
所谓的一对一查询,就是说我们在查询一个表的数据的时候,需要关联查询其他表的数据. 需求 首先说一个使用一对一查询的小需求吧:假设我们在查询某一个订单的信息的时候,需要关联查询出创建这个订单对应的用户信 ...
- Mybatis实现一对一查询 对ResultType和ResultMap分析
实现一对一查询: ResultMap:使用ResultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加 列名对应的属性,即可完成映射. 如果没有查询结果的特殊要求建议使用Resul ...
- Mybatis高级查询之一对一查询的四种方法
目录 1. 一对一查询 1.1 一对一嵌套结果查询 1.2 使用resultMap配置一对一映射 1.3 使用resultMap的association标签配置一对一映射 1.4 associatio ...
- MyBatis:一对一关联查询
MyBatis从入门到放弃三:一对一关联查询 前言 简单来说在mybatis.xml中实现关联查询实在是有些麻烦,正是因为起框架本质是实现orm的半自动化. 那么mybatis实现一对一的关联查询则是 ...
- mybatis的一对一,一对多查询,延迟加载,缓存介绍
一对一查询 需求 查询订单信息关联查询用户信息 sql语句 /*通过orders关联查询用户使用user_id一个外键,只能关联查询出一条用户记录就可以使用内连接*/ SELECT orders.*, ...
- MyBatis一对一查询
---------------------siwuxie095 MyBatis 一对一查询 以订单和用户为例,即 相对订 ...
- mybatis由浅入深day02_2一对一查询_2.3方法二:resultMap_resultType和resultMap实现一对一查询小结
2.3 方法二:resultMap 使用resultMap,定义专门的resultMap用于映射一对一查询结果. 2.3.1 sql语句 同resultType实现的sql SELECT orders ...
- 【MyBatis学习08】高级映射之一对一查询
从这一篇博文开始,将总结一下mybatis中的几个高级映射,即一对一.一对多.多对多查询,这篇先总结一下mybatis中的一对一查询. 为了模拟这些需求,事先要建立几个表,不同的表之间将对应上面提到 ...
- mybatis学习笔记(10)-一对一查询
mybatis学习笔记(10)-一对一查询 标签: mybatis mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实 ...
随机推荐
- C语言基础--循环 递归打印乘法表
for循环打印乘法表: #include <stdio.h> // for循环打印乘法表 int main(int argc, const char * argv[]) { //矩形 ; ...
- 使用python 提取网页的特定数据转
http://blog.csdn.net/nwpulei/article/details/7272832
- Android通过HttpURLConnection链接到网络,并获取网络数据
1.判断网络是否连接 private void networkIsconnected(String str){ ConnectivityManager connMgr = (ConnectivityM ...
- kernel source reading notepad
__init ,标记内核启动时所用的初始化代码,内核启动完成后就不再使用.其所修饰的内容被放到.init.text section中 __exit,标记模块退出代码,对非模块无效 to be cont ...
- Enable SSHD on Ubuntu
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring
- tomcat简单文服
1.修改tomcat配置文件 web.xml 将listings的value值改为true. DefaultServlet在Tomcat中主要是做目录列表(Directory Listing)用. 2 ...
- Galaxy Classification
10.3 Data Preparation After removing a large number of the columns from the raw SDSS dataset, introd ...
- 第十章:Intent详解
[正文] Intent组件虽然不是四大组件,但却是连接四大组件的桥梁,学习好这个知识,也非常的重要. 一.什么是Intent 1.Intent的概念: Android中提供了Intent机制来协助应用 ...
- PCL中point cloud的数据类型
出处: http://wiki.ros.org/pcl/Overview 1.数据类型 1.1 ROS中point cloud数据类型 sensor mesgs::PointCloud sensor ...
- 强大的wget
转载自:http://www.cnblogs.com/lidp/archive/2010/03/02/1696447.html 需要下载某个目录下面的所有文件.命令如下 wget -c -r -np ...