mybatis中实现一对一,一对多查询
在实际的开发中我们经常用到的是一对一查询和一对多查询。而多对多的实现是通过中间来实现,这里就没有给出来了
比如:
订单和用户是一对一的关系(一个订单只能对应一个用户)
订单和订单明细是一对多的关系(一个订单可以对应多个订单明细)
程序结构图:
所采用的mybatis版本3.2.3
对应的jar包如下图所示
对应库表的信息
用户表:
订单表:
订单明细表:
对应的实体类的信息
用户实体类;
用户扩展实体类:
订单实体类:
订单明细实体类:
==============================【一对一(resultType方式) begin】==============================
OrderContent.xml
<!-- 一对一: 通过查询商品,查询出对应的用户的信息 -->
<!-- 方式1:resultType -->
<select id="findOrderAndRelateUserByResultType" resultType="com.ithema.mybatis.po.OrderVo">
SELECT
orders.id,
orders.order_number,
orders.user_id,
user.username,
user.address
FROM orders,
USER
WHERE orders.user_id = user.id
</select>
OrderContent.java
/**
* resultType的方式实现通过订单查询用户(一对一)
* @return
*/
List<OrderVo> findOrderAndRelateUserByResultType();
查询结果:
数据库结果:
==============================【一对一(resultType方式) end】==============================
==============================【一对一(resultMap方式) begin】==============================
OrderContent.xml
<!-- 方式2:resultMap -->
<!-- 定义一个resultMap,用于映射订单信息和用户信息 id:在同一个命名空间下唯一
type:要映射到的java类型,在Orders实体中定义一个User属性,用于映射User的信息 -->
<resultMap type="com.ithema.mybatis.po.Orders" id="orderAndRelateUserMap">
<id column="id" property="id" />
<result column="order_number" property="order_number" />
<result column="user_id" property="user_id" />
<!-- association标签用于实现一对一映射 property:将关联的信息映射到Orders对象中的哪个属性中 javaType:映射属性的java类型 -->
<association property="user" javaType="com.ithema.mybatis.po.User">
<!-- id:关联的用户信息的唯一约束 property:id指定的映射关联的对象的那个属性 -->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="address" property="address" />
</association>
</resultMap>
<select id="findOrderAndRelateUserByResultMap" resultMap="orderAndRelateUserMap">
SELECT
orders.id,
orders.order_number,
orders.user_id,
user.username,
user.address
FROM orders,
USER
WHERE orders.user_id = user.id
</select>
OrderContent.java
/**
* resultMap的方式实现通过订单查询用户(一对一)
* @return
*/
List<Orders> findOrderAndRelateUserByResultMap();
测试结果:
==============================【一对一(resultMap方式) end】==============================
==============================【一对多(resultMap方式) begin】==============================
OrderContent.xml
<!-- 一对多: 通过查询订单查询对应的订单明细信息
一对多的查询只能通过resultMap的方式实现
在Orders对象中加入一个list集合表示订单明细的信息
-->
<!-- 定义一个订单和订单明细的一对多的映射Map -->
<resultMap type="com.ithema.mybatis.po.Orders" id="orderAndOrderDetailMap">
<!-- 订单对象的唯一标识 -->
<id column="id" property="id"/>
<result column="order_number" property="order_number"/>
<!-- 一对多的映射要使用collection标签
property:将明细信息映射到哪个集合对象中
ofType:所要映射的集合对象的类型
-->
<collection property="orderDetails" ofType="com.ithema.mybatis.po.OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="item_num" property="item_num"/>
<result column="item_price" property="item_price"/>
</collection>
</resultMap>
<select id="findOrderAndOrderDetail" resultMap="orderAndOrderDetailMap">
SELECT
orders.id,
orders.order_number,
orderdetail.id orderdetail_id,
orderdetail.item_num,
orderdetail.item_price
FROM orders,
orderdetail
WHERE orders.id = orderdetail.orders_id
</select>
OrderContent.java
/**
* resultMap的方式实现通过订单查询对应的订单明细(一对多)
* @return
*/
List<Orders> findOrderAndOrderDetail();
测试结果:
数据库结果:
==============================【一对多(resultMap方式) end】=============================
mybatis中实现一对一,一对多查询的更多相关文章
- Mybatis中多表联查,查询出来的字段出现重名,造成数据异常的解决方法!
在做一对多出现的问题,引发的思考:当数据库表中,主表的主键id和明细表的中的字段名相同时怎么办?Mybatis进行自动映射赋值的时候会不会出现异常? 注意:M ...
- Mybatis中动态SQL多条件查询
Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...
- MyBatis的关联关系 一对一 一对多 多对多
一对一示例 一个妻子对应一个丈夫 数据库表设计时 在妻子表中添加一个丈夫主键的作为外键 1 对应的JavaBean代码虽然在数据库里只有一方配置的外键,但是这个一对一是双向的关系. Husband实体 ...
- Mybatis中的关联映射和查询
一.商品订单数据模型 1.数据表 这里定义了四个表,分别表示用户,商品,订单,和订单详情. 用户表user CREATE TABLE `user` ( `id` int(11) NOT NULL AU ...
- springboot整合mybatis-plus基于纯注解实现一对一(一对多)查询
因为目前所用mybatis-plus版本为3.1.1,感觉是个半成品,所有在实体类上的注解只能支持单表,没有一对一和一对多关系映射,且该功能还在开发中,相信mybatis-plus开发团队在不久的将来 ...
- Mybatis笔记四:Mybatis中的resultType和resultMap查询操作实例详解
resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题.比 ...
- Mybatis 中的转义字符及常用查询
转译符 1.特殊字符转译 < < 小于 > > 大于 & & 与 ' ’ 单引号 " " 双引号 需要注意的是分号是必不可少的. 比如 a ...
- MyBatis中collection (一对一,一对多)
MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
今天把notice表中的content字段改为long字段后,含有该字段的使用CTE的查询语句报错了.提示一下错误 ### Cause: java.sql.SQLException: 流已被关闭 ; ...
随机推荐
- Android之AbsoluteLayout(绝对布局)
1.属性简介 为了适配不同机型,绝对布局使用很少! android:layout_x="50dp" android:layout_y="100dp"也只有在Ab ...
- PAT甲级——A1066 Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT甲级——A1005 Spell It Right
题目描述 Given a non-negative integer N, your task is to compute the sum of all the digits of N, and out ...
- 03jQuery对象初识(二)筛选器2
1.下面的筛选都是非常常用的 <ul> <li id="l0">l0</li> <li>l1</li> <li&g ...
- vue 模版组件用法
第一种 //首先,别忘了引入vue.js <div id="user_name_01"></div> <script src="../nod ...
- Jqgrid 序号列宽度调整
// 遍历jqgrid 使其序号列宽度为45 function setwidth() { $("table[role='grid']").each(function () {//j ...
- dubbo入门学习(四)-----dubbo配置
配置来源 首先,从Dubbo支持的配置来源说起,默认有四种配置来源: JVM System Properties,-D参数 Externalized Configuration,外部化配置 Servi ...
- 转:fork与vfork的区别
源地址:http://blog.csdn.net/jianchi88/article/details/6985326 有大量驱动文章 fork()与vfock()都是创建一个进程,那他们有什么区别呢? ...
- spring boot项目启动报DataSource错误
初建一个简单的spring boot 项目,启动后会报错. Exception encountered during context initialization - cancelling refre ...
- 安装springsource-tool-suite插件成功之后找不到spring的处理办法
最近学习spring,安装springsource-tool-suite插件,成功之后,在help-installation details里面可以找到安装的spring插件,却在window-pre ...