Mybatis中的延迟加载的使用方法
Mybatis中的延迟加载的使用方法
在Mybatis中查询订单,然后带出商品信息和快递信息的配置方法 orderMapper.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">
<mappernamespace="com.taotao.store.order.mapper.OrderMapper">
<sqlid="tableName">tb_order</sql>
<resultMaptype="Order"id="pojoResultMap"autoMapping="true">
<idcolumn="order_id"property="orderId"/>
<associationproperty="orderShipping"javaType="OrderShipping"column="order_id"select="queryOrderShippingByOrderId"autoMapping="true"></association>
<collectionproperty="orderItems"javaType="Arraylist"ofType="OrderItem"autoMapping="true"select="queryOrderItemByOrderId"column="order_id">
</collection>
</resultMap>
<selectid="queryOrderItemByOrderId"resultType="OrderItem"parameterType="String">
SELECT * FROM tb_order_item WHERE order_id = #{orderId};
</select>
<selectid="queryOrderShippingByOrderId"resultType="OrderShipping"parameterType="String">
SELECT * FROM tb_order_shipping WHERE order_id = #{orderId};
</select>
<selectid="queryList"resultMap="pojoResultMap">
SELECT
*
FROM
<includerefid="tableName"/>
</select>
<selectid="queryByID"resultMap="pojoResultMap">
SELECT
*
FROM
<includerefid="tableName"/>
WHERE order_id = #{id};
</select>
<selectid="queryByWhere"parameterType="Where"resultMap="pojoResultMap">
SELECT
*
FROM
<includerefid="tableName"/>
WHERE ${where.column} ${where.operater} #{where.value} LIMIT 1;
</select>
<selectid="queryListByWhere"parameterType="Where"resultMap="pojoResultMap">
SELECT
*
FROM
<includerefid="tableName"/>
WHERE ${where.column} ${where.operater} #{where.value};
</select>
<insertid="save">
INSERT INTO <includerefid="tableName"/> VALUES (#{orderId},#{payment},#{paymentType},#{postFee},#{status},#{createTime},#{updateTime},#{paymentTime},#{consignTime},#{endTime},#{closeTime},#{shippingName},#{shippingCode},#{userId},#{buyerMessage},#{buyerNick},#{buyerRate});
INSERT INTO tb_order_item VALUES
<foreachcollection="orderItems"item="item"separator=",">
(#{item.itemId},#{orderId},#{item.num},#{item.title},#{item.price},#{item.totalFee},#{item.picPath})
</foreach>
;
INSERT INTO tb_order_shipping VALUES (#{orderId},#{orderShipping.receiverName},#{orderShipping.receiverPhone},#{orderShipping.receiverMobile},#{orderShipping.receiverState},#{orderShipping.receiverCity},#{orderShipping.receiverDistrict},#{orderShipping.receiverAddress},#{orderShipping.receiverZip},NOW(),NOW());
</insert>
<updateid="update">
UPDATE <includerefid="tableName"/>
<set>
<iftest="payment !=null and payment != ''">
payment = #{payment},
</if>
<iftest="postFee !=null and postFee != ''">
post_fee = #{postFee},
</if>
<iftest="status !=null and status != ''">
status = #{status},
</if>
<iftest="updateTime !=null and updateTime != ''">
update_time = #{updateTime},
</if>
<iftest="paymentTime !=null and paymentTime != ''">
payment_time = #{paymentTime},
</if>
<iftest="consignTime !=null and consignTime != ''">
consign_time = #{consignTime},
</if>
<iftest="endTime !=null and endTime != ''">
end_time = #{endTime},
</if>
<iftest="closeTime !=null and closeTime != ''">
close_time = #{closeTime},
</if>
<iftest="shippingName !=null and shippingName != ''">
shipping_name = #{shippingName},
</if>
<iftest="shippingCode !=null and shippingCode != ''">
shipping_code = #{shippingCode},
</if>
<iftest="buyerMessage !=null and buyerMessage != ''">
buyer_message = #{buyerMessage},
</if>
<iftest="buyerRate !=null and buyerRate != ''">
buyer_rate = #{buyerRate},
</if>
</set>
WHERE order_id = #{orderId};
</update>
<deleteid="deleteByID"parameterType="Long">
DELETE FROM <includerefid="tableName"/> WHERE order_id = #{orderId};
DELETE FROM tb_order_item WHERE order_id = #{orderId};
</delete>
<deleteid="deleteByIDS"parameterType="list">
DELETE FROM <includerefid="tableName"/> WHERE order_id IN
<foreachcollection="ids"item="id"open="("close=")"separator=",">
#{id}
</foreach>;
DELETE FROM tb_order_item WHERE order_id IN
<foreachcollection="ids"item="id"open="("close=")"separator=",">
#{id}
</foreach>;
</delete>
<updateid="paymentOrderScan"parameterType="Date">
UPDATE tb_order SET
status = 6,
update_time = NOW(),
close_time = NOW(),
end_time = NOW()
WHERE status = 1 AND payment_type = 1 AND create_time <= #{date}
</update>
</mapper>
其中此代表延迟加载
<resultMaptype="Order"id="pojoResultMap"autoMapping="true">
<idcolumn="order_id"property="orderId"/>
<associationproperty="orderShipping"javaType="OrderShipping"column="order_id"select="queryOrderShippingByOrderId"autoMapping="true"></association>
<collectionproperty="orderItems"javaType="Arraylist"ofType="OrderItem"autoMapping="true"select="queryOrderItemByOrderId"column="order_id">
</collection>
</resultMap>
调用方法:
http://order.taotao.com/order/query/31537859410409
返回数据JSON
{
"orderId":"31537859410409",
"payment":"5288",
"paymentType":null,
"postFee":"0",
"status":1,
"createTime":1537859410000,
"updateTime":1537859410000,
"paymentTime":null,
"consignTime":null,
"endTime":null,
"closeTime":null,
"shippingName":null,
"shippingCode":null,
"userId":3,
"buyerMessage":null,
"buyerNick":"zhang123",
"buyerRate":0,
"orderItems":[
{
"itemId":9,
"orderId":"31537859410409",
"num":1,
"title":"苹果(Apple)iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机3",
"price":5288,
"totalFee":5288,
"picPath":"http://image.taotao.com/images/2015/03/06/2015030610045320609720.jpg"
}
],
"orderShipping":{
"orderId":"31537859410409",
"receiverName":"张志君",
"receiverPhone":"",
"receiverMobile":"15800000000",
"receiverState":"上海",
"receiverCity":"上海",
"receiverDistrict":"闵行区",
"receiverAddress":"三鲁公路3279号 明浦广场 3号楼 205室",
"receiverZip":"200000",
"created":1537859410000,
"updated":1537859410000
}
}
查询出的结果包括订单信息以及订单商品信息还有快递信息
主要内容是根据传智播客视频学习总结的小结。
转载于:https://www.cnblogs.com/lr393993507/p/9700055.html
Mybatis中的延迟加载的使用方法相关推荐
- 在mybatis用mysql的代码块_关于Mybatis 中使用Mysql存储过程的方法
1.存储过程的简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用 ...
- Mybatis 中的延迟加载
1.问题提出 在之前的文章一对多中,当我们有一个用户,他又一百个账户. (1)在查询用户时,要不要把关联的账户信息查询出来? (2)在查询账户时,要不要把关联的用户信息查询出来? 对于第一个问题,我们 ...
- Mybatis中,SQLSessionFactoryBuilder使用build方法时做了哪些事?
目录 探究Configuration 探究parse方法 启动断点调试 过程 总结 当我们上手mybatis时,对于mybatis如何读取xml配置文件,获取SQLSessionFactory的底层源 ...
- 【Java Web开发指南】Mybatis 中的延迟加载
延迟加载和立即加载 延迟加载 在真正的使用数据时才发起查询,不用的时候不查.按需加载(懒加载).立即加载 不管用不用,只要一调用方法,马上发起查询. 使用方法: <settings>< ...
- MyBatis中使用YEAR和MONTH方法获取时间查询参数的年和月
场景 实体类中的时间属性为 private Date tjny; 而在数据库中存储的是年和月两个字段. 需要将前端传递的时间参数分别获取年和月并对数据库中的数据进行筛选查询. 注: 博客: https ...
- mybatis与php,浅谈mybatis中的#和$的区别
浅谈mybatis中的#和$的区别 发布于 2016-07-30 11:14:47 | 236 次阅读 | 评论: 0 | 来源: 网友投递 MyBatis 基于Java的持久层框架MyBatis 本 ...
- mybatis学习(55):延迟加载
MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. 注意:MyBatis的延迟加载只是对关联对象的查询 ...
- MyBatis中if - else if - else 的使用
http://www.leftso.com/blog/765.html 不过有他的替代 choose,写法如下: <choose><when test="params!= ...
- mybatis 中 if else 用法
mybaits 中没有 else 要用 chose when otherwise 代替 下面就是MyBatis中的if....else...表示方法 <choose><when te ...
最新文章
- python基于值得内存_为什么说Python采用的是基于值的内存管理模式
- 三角量测(Triangulation)之再学习
- 优化调整Oracle 8i数据库
- 基于DirectX的游戏引擎-Xworld
- unicode字符、python乱码问题
- Python数据结构:二分查找
- LeetCode2.两数相加
- c语言程序设计实验8,C语言程序设计实验八结构体.doc
- wsus3.0手工卸载和安装
- paip.简化字-手写参考二简字..共98个
- 电赛公开课整理(二),电路基础,截图+模电知识【16000字】【原创】
- c语言编程 设计密码锁,单片机控制安全密码锁的设计(附程序,pcb,原理图)
- urlrewrite java_Java中URL重写(urlrewrite+Maven)
- VMware虚拟机如何迁移到阿里云
- echarts图表给柱形图的每个柱子设置不同颜色(包括每个柱子设置渐变颜色)
- web 服务器安全维护,Web服务器安全攻击及防护机制详解
- HTML5菜单栏特效
- (环境搭建+复现)74CMS模版注入+文件包含getshell
- 如何绘制神经网络结构图,如何设计神经网络结构
- 微信公众号的web开发者工具的自助解绑功能终于有了!
热门文章
- 一个基于 osip 库的 UAC 和 UAS 的代码整理(转)
- mysql-python安装时EnvironmentError: mysql_config not found
- javalibrary 线上_《NBA2K21》MT模式线上3V3任务一览 :: 游民星空 GamerSky.com
- html canvas 画注释框
- linux 和 windows usb插拔侦测
- 缓存服务的更新策略有哪些?
- python中path的用法,python中path的用法
- php 解析返回的xml,php解析cas返回的xml解决思路
- 【算法】剑指 Offer 22. 链表中倒数第k个节点
- 【Elasticsearch】关于 Analyzers 的一切,第2部分