JavaSpring中级联查询
一对一级联查询映射文件PersonMapper.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.dao.PersonDao"> <!--方法1:嵌套查询,执行两个SQL语句-->
<resultMap type = "com.po.Person" id = "cardAndPerson1">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property = "card" column="idcard_id" javaType="com.po.Idcard"
select = "com.dao.IdCardDao.selectCodeById"/>
</resultMap>
<select id="selectPersonById1" parameterType="Integer" resultMap="cardAndPerson1">
select * from person where id=#{id}
</select> <!--方法2:嵌套查询,执行一个SQL语句-->
<resultMap type = "com.po.Person" id="cardAndPerson2">
<id property = "id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="card" javaType="com.po.Idcard">
<id property = "id" column="idcard_id"/>
<result property = "code" column="code"/>
</association>
</resultMap>
<select id = "selectPersonById2" parameterType="Integer" resultMap="cardAndPerson2">
select p.*,ic.code
from person p,idcard ic
where p.idcard_id = ic.id and p.id=#{id}
</select> <!--使用POJO存储结果-->
<select id = "selectPersonById3" parameterType="Integer" resultType="com.pojo.SelectPersonById">
select p.*,ic.code
from person p,idcard ic
where p.idcard_id = ic.id and p.id=#{id}
</select>
</mapper>
IdCardMapper.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.IdCardDao">
<select id="selectCodeById" parameterType="Integer"
resultType="com.po.Idcard">
select * from idcard where id = #{id}
</select>
</mapper>
一对多查询映射文件OrdersMapper.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.OrdersDao">
<select id="selectOrdersById" parameterType="Integer"
resultType="com.po.Orders">
select * from orders where user_id = #{id}
</select>
</mapper>
UserMapper.XML代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--命名空间应该是对应接口的包名+接口名 -->
<mapper namespace="com.dao.UserDao">
<!--id应该是接口中的方法,结果类型如没有配置别名则应该使用全名称 -->
<resultMap type="com.po.MyUser" id="userAndOrders1">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<result property="usex" column="usex"/>
<!--一对多级联查询,ofType表示集合中的元素类型,将uid传递给selectOrdersById-->
<collection property="ordersList" ofType="com.po.Orders" column="uid"
select ="com.dao.OrdersDao.selectOrdersById"/>
</resultMap>
<select id="selectUserOrdersById1" parameterType="Integer"
resultMap="userAndOrders1">
select * from user where uid = #{id}
</select> <resultMap type="com.po.MyUser" id="userAndOrders2">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<result property="usex" column="usex"/>
<collection property="ordersList" ofType="com.po.Orders">
<id property="id" column="id"/>
<result property="ordersn" column="ordersn"/>
</collection>
</resultMap>
<select id="selectUserOrdersById2" parameterType="Integer"
resultMap="userAndOrders2">
select u.*,o.id,o.ordersn
from user u,orders o
where u.uid = o.user_id and u.uid=#{id}
</select>
<--!使用POJO存储结果-->
<select id="selectUserOrdersById3" parameterType="Integer"
resultType="com.pojo.SelectUserOrdersById">
select u.*,o.id,o.ordersn
from user u,orders o
where u.uid = o.user_id and u.uid=#{id}
</select>
</mapper>
多对多查询,映射文件OrdersMapper.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--命名空间应该是对应接口的包名+接口名 -->
<mapper namespace="com.dao.UserDao">
<!--id应该是接口中的方法,结果类型如没有配置别名则应该使用全名称 -->
<resultMap type="com.po.MyUser" id="userAndOrders1">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<result property="usex" column="usex"/>
<collection property="ordersList" ofType="com.po.Orders" column="uid"
select ="com.dao.OrdersDao.selectOrdersById"/>
</resultMap>
<select id="selectUserOrdersById1" parameterType="Integer"
resultMap="userAndOrders1">
select * from user where uid = #{id}
</select> <resultMap type="com.po.MyUser" id="userAndOrders2">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<result property="usex" column="usex"/>
<collection property="ordersList" ofType="com.po.Orders">
<id property="id" column="id"/>
<result property="ordersn" column="ordersn"/>
</collection>
</resultMap>
<select id="selectUserOrdersById2" parameterType="Integer"
resultMap="userAndOrders2">
select u.*,o.id,o.ordersn
from user u,orders o
where u.uid = o.user_id and u.uid=#{id}
</select> <select id="selectUserOrdersById3" parameterType="Integer"
resultType="com.pojo.SelectUserOrdersById">
select u.*,o.id,o.ordersn
from user u,orders o
where u.uid = o.user_id and u.uid=#{id}
</select>
</mapper>
JavaSpring中级联查询的更多相关文章
- Django中前端界面实现级联查询
Django前端界面实现级联查询 一.前端界面中 <span scope="col" colspan="6"> 院系:<select id=& ...
- Mybatis中使用级联查询,一对多的查询
一.需求描述 自己在开发一个小程序的过程中,需要做的一个查询是稍微比较复杂的查询,根据用户信息去查询用户所对应的宠物信息. 一个用户可能对应多个宠物,所以在用户和宠物信息的对应关系就是一对多的关系. ...
- Mybatis 级联查询 (一对多 )
后台系统中 涉及到添加试卷 问题 答案的一个模块的.我需要通过试卷 查询出所有的试题,以及试题的答案.这个主要要使用到Mybatis的级联查询. 通过试卷 查询出与该试卷相关的试题(一对多),查询出试 ...
- Mybatis 之级联查询 一对多配置
Mybatis级联 查询相对于hibenate是有点麻烦,但是相应好处也是有的,Mybatis轻量.根据自己要的字段配置方便 一对多配置用 <collection property=&quo ...
- oracle使用connect by进行级联查询 树型菜单
Oracle使用connect by进行级联查询 树型菜单(转) connect by可以用于级联查询,常用于对具有树状结构的记录查询某一节点的所有子孙节点或所有祖辈节点. 来看一个示例,现假设我们拥 ...
- 使用cglib实现数据库框架的级联查询
写在前面的 这一章是之前写的<手把手教你写一个Java的orm框架> 的追加内容.因为之前写的数据库框架不支持级联查询这个操作,对于有关联关系的表用起来还是比较麻烦,于是就准备把这个功能给 ...
- Jquery 实现select 3级级联查询
实现级联效果的思路: 1. 页面加载时,先显示第一级select,第二.三级的select隐藏,根据第一级select值的改变,再显示第二级select,依次类推: 2.只从后台获取第一级select ...
- 【SSH网上商城项目实战05】完成数据库的级联查询和分页
转自:https://blog.csdn.net/eson_15/article/details/51320212 上一节我们完成了EasyUI菜单的实现.这一节我们主要来写一下CategorySer ...
- 树概念及使用connect by进行级联查询
树 树,大家都见过,以这种形式的数据关系,就是树.下面看一张图,了解什么是根节点(树干).节点或分叉.叶(叶节点) connect by 级联查询 connect by可以用于级联查询,常用于对具有树 ...
随机推荐
- CountDownLatch 计算器(具有回调功能)
final CountDownLatch cdl = new CountDownLatch(1); new Thread(new Runnable() { @Override public void ...
- 1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、212345等. 要求:”4”不能在第三位,”3”与”5”不能相连。
private static String[] mustExistNumber = new String[] { "1", "2", "2" ...
- JMock2入门
说明:学习JMock官网的Getting Started的笔记 例子:为测试publish/subscribe发布/订阅信息系统的publisher(发布者),mock subscriber(订阅者) ...
- 【three.js第五课】光线的添加和感光材料
材料分类: MeshBasicMaterial:基础网孔材料,一个以简单着色(平面或线框)方式来绘制几何形状的材料.MeshLambertMaterial:兰伯特网孔材料,一种非发光材料(兰伯特)的表 ...
- Springboot系列(七) 集成接口文档swagger,使用,测试
Springboot 配置接口文档swagger 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...
- 使用GML的八方向自动寻路
使用GML的八方向自动寻路 本教程适合无基础人员使用. 提示 本教程中仅使用了最简单的方法,并且有一些错误和不规范之处.请谅解一下,在评论区提出,我会修改.古人曰"教学相长",希望 ...
- 2.react-插件
PC: antd(蚂蚁金服)https://ant.design/index-cn 移动: mobile-antd(蚂蚁金服)https://mobile.ant.design =========== ...
- Java面试系列第2篇-Object类中的方法
Java的Object是所有引用类型的父类,定义的方法按照用途可以分为以下几种: (1)构造函数 (2)hashCode() 和 equals() 函数用来判断对象是否相同 (3)wait().wai ...
- 基于Koa实现留言版demo
学习node.koa,随手做了一个留言板demo. 基本功能如下: 未登录用户可以查看主题列表和主题内容. 用户注册和登录功能. 登录用户可以发表.修改.删除自己的主题. 登录用户主题列表下方有发表主 ...
- ES6中对函数的扩展
ES6一路扩展,字符串.数组.数值.对象无一“幸免”,ES6说要雨露均沾,函数也不能落下,今天,就来讲解ES6对函数的扩展. 参数的默认值 在开发中,给函数的参数指定默认值,是很普遍很常见的一个需求, ...