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. ...
- 完全定制UITabBarViewController
完全定制UITabBarViewController 效果 源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 TotalCusto ...
- cocos2d-x 保持屏幕点亮及自动变灰
很早之前遇到的问题,现在记录一下.有一家Android渠道(抱歉,时间太长了已经记不大清楚是哪一家了 oppo/联想/酷派?)在我们提交新版本时拒绝了,理由是:手机背光状态下,屏幕不会自动变灰. 这里 ...
- Asp.Net 拦截请求自定义处理
需求: 在Aps.Net 应用中,对于浏览器请求的部分url的地址自定义处理,不交给路由系统或页面. 解决方案: 在全局文件Global.asax中 ,提供Application_BeginReque ...
- Asp.Net WebApi开启Session回话
一.在WebApi项目中默认没有开启Session回话支持.需要在Global中的Init()方法中指定会员需要支持的类型 public class WebApiApplication : Syste ...
- git: error while loading shared libraries: libiconv.so.2
git安装之后出现:git: error while loading shared libraries: libiconv.so.2: cannot open shared object file: ...
- AD各种布线方法总结
1.常规布线:不详细说了,是个人就知道怎么弄.需要说明的是在布线过程中,可按小键盘的*键或大键盘的数字2键添加一个过孔:按L键可以切换布线层:按数字3可设定最小线宽.典型线宽.最大线宽的值进行切换. ...
- iOS截屏方法
//获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...
- (转)Unity3D工程版本管理方案
自:http://blog.dou.li/unity3d%E5%B7%A5%E7%A8%8B%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86%E6%96%B9%E6%A1%88 ...
- 【矩阵乘】【DP】【codevs 1305】Freda的道路
1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级: 大师 Master 题目描写叙述 Description Freda要到Rainbow的城堡去玩了. 我们能 ...