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
}
}
查询出的结果包括订单信息以及订单商品信息还有快递信息
主要内容是根据传智播客视频学习总结的小结。
Mybatis中的延迟加载的使用方法的更多相关文章
- 【MyBatis学习11】MyBatis中的延迟加载
1. 什么是延迟加载 举个例子:如果查询订单并且关联查询用户信息.如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息.把对用户信息的按需去查询就是延迟加载. 所以延迟加载即先从单表 ...
- mybatis中的延迟加载
一.延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 延迟加载:先 ...
- @MyBatis中的if...else...
<select id="selectSelective" resultMap="xxx" parameterType="xxx"> ...
- mybatis 中 if else 用法
mybaits 中没有 else 要用 chose when otherwise 代替 下面就是MyBatis中的if....else...表示方法 <choose> <when t ...
- Mybatis中的N+1问题与延迟加载
0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...
- Mybatis中配置Mapper的方法
在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...
- mybatis中的updateByExampleSelective方法怎么使用
mybatis中的updateByExampleSelective方法怎么使用.sendDetailMapper.updateByExampleSelective(sendDetail, m);参数m ...
- 【mybatis】mybatis中避免where空条件后面添加1=1垃圾条件的 优化方法
在mybatis中拼接查询语句,偶尔会出现where后面可能一个字段的值都没有,就导致所有条件无效,导致where没有存在的意义:但也有可能这些条件会存在.那解决这个问题的方法,最常见的就是: 在wh ...
- Mybatis中实体类属性与数据库列表间映射方法介绍
这篇文章主要介绍了Mybatis中实体类属性与数据列表间映射方法介绍,一共四种方法方法,供大家参考. Mybatis不像Hibernate中那么自动化,通过@Co ...
随机推荐
- Cannot subclass final class class com.sun.proxy.$Proxy
背景 这个错误是我在使用AOP动态切换数据库,实现数据库的读写分离的时候出现的问题,使用到的系统环境是: <spring.version>3.2.6.RELEASE</spring. ...
- java去除数组中重复的元素方法总结
/* * ArrayUnique.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.s ...
- IOS的唯一标识符问题(转)
引用地址 http://www.zhihu.com/question/22599526/answer/21938878 网上搜了下IOS手机标志的种类,直接引用过来. UDID [[UIDevice ...
- spring-boot:run启动时,指定spring.profiles.active
Maven启动指定Profile通过-P,如mvn spring-boot:run -Ptest,但这是Maven的Profile. 如果要指定spring-boot的spring.profiles. ...
- 详细解读Volley(一)—— 基本Request对象 & RequestQueue
Volley它非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕.所以不建议用它去进行下载文件.加载大图的操作.有人可能会问,如 ...
- 数据库实例: STOREBOOK > 用户 > 编辑 用户: SYSTEM
ylbtech-Oracle:数据库实例: STOREBOOK > 用户 > 编辑 用户: SYSTEM 编辑 用户: SYSTEM 1. 一般信息返回顶部 1.1, 1.2, 2 ...
- iOS:在cell中使用倒计时的最佳方法
一.简单介绍 在UITableViewCell中每条数据中显示该内容的倒计时, 并随时间进行倒数,这是很多电商app最常见的活动推销功能模块,自然想到用的就是计时器了. 二.基本想法 想法1:在每个c ...
- [转]Chart.js入门教程
Chart.js是一个简单.面向对象.为设计者和开发者准备的图表绘制工具库. 相信大部分人都一样,看到一大筐用文本或者表格形式呈现的数据就头疼.因为这种呈现方式也太无聊了吧...而且这对于我们处理原始 ...
- [PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers
Hello everyone, this is the second post of the series. . Background =============== In my environm ...
- 移动前端调试工具-Weinre真机调试
之前做移动前端调试页面的时候就是简单的使用Chrome模拟器调试,能满足基本基本的需求,后来发现了基于Web Inspector(Webkit)的远程调试工具Weinre,可以在PC端直接调试运行在移 ...