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 ...
随机推荐
- LinkedList中将对象按照某一属性排序
例如,链表 treelist 声明如下: LinkedList<TreeNode> treelist = new LinkedList<TreeNode>(); 其中 Tree ...
- 每天一个linux命令-wc命令
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- fabric-ca-server
fabric-ca-server start -b admin:adminpw -d --db.type mysql --db.datasource "root:rootpwd@tcp(17 ...
- JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(下)(各种乱码、验证码、重定向和转发)
HttpServletRequest获取请求头信息 (1)获取客户机请求头 String getHeader(String name) Enumeration<String> getHe ...
- MECE分析法(Mutually Exclusive Collectively Exhaustive)
什么是MECE分析法? MECE,是Mutually Exclusive Collectively Exhaustive,中文意思是“相互独立,完全穷尽”. 也就是对于一个重大的议题,能够做到不重叠. ...
- 转:PHP高效率写法(详解原因)
FROM : http://my.oschina.net/No5stranger/blog/157585#OSC_h3_1 1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4 ...
- python文档生成工具:pydoc、sphinx;django如何使用sphinx?
文档生成工具: 自带的pydoc,比较差 建议使用sphinx 安装: pip install sphinx 安装主题: 由各种主题,我选择常用的sphinx_rtd_theme pip instal ...
- how to fix bug in daily work
0 QE will begin test the product when system is stable. so they may log a lot of issues, and our dai ...
- 【BLE】CC2541之发现服务与特征值
一.简介 本文以SimpleBLECentral工程为例,解析CC2541作为主机时是如何发现从机的服务和特征值的. 二.实验平台 协议栈版本:BLE-CC254x-1.4.0 编译软件:IAR 8. ...
- Druid连接池简介和配置
Druid是什么?有什么作用? Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. Druid ...