Mybatis中的延迟加载的使用方法

在Mybatis中查询订单,然后带出商品信息和快递信息的配置方法 orderMapper.xml配置如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mappernamespace="com.taotao.store.order.mapper.OrderMapper">
  5. <sqlid="tableName">tb_order</sql>
  6. <resultMaptype="Order"id="pojoResultMap"autoMapping="true">
  7. <idcolumn="order_id"property="orderId"/>
  8. <associationproperty="orderShipping"javaType="OrderShipping"column="order_id"select="queryOrderShippingByOrderId"autoMapping="true"></association>
  9. <collectionproperty="orderItems"javaType="Arraylist"ofType="OrderItem"autoMapping="true"select="queryOrderItemByOrderId"column="order_id">
  10. </collection>
  11. </resultMap>
  12. <selectid="queryOrderItemByOrderId"resultType="OrderItem"parameterType="String">
  13. SELECT * FROM tb_order_item WHERE order_id = #{orderId};
  14. </select>
  15. <selectid="queryOrderShippingByOrderId"resultType="OrderShipping"parameterType="String">
  16. SELECT * FROM tb_order_shipping WHERE order_id = #{orderId};
  17. </select>
  18. <selectid="queryList"resultMap="pojoResultMap">
  19. SELECT
  20. *
  21. FROM
  22. <includerefid="tableName"/>
  23. </select>
  24. <selectid="queryByID"resultMap="pojoResultMap">
  25. SELECT
  26. *
  27. FROM
  28. <includerefid="tableName"/>
  29. WHERE order_id = #{id};
  30. </select>
  31. <selectid="queryByWhere"parameterType="Where"resultMap="pojoResultMap">
  32. SELECT
  33. *
  34. FROM
  35. <includerefid="tableName"/>
  36. WHERE ${where.column} ${where.operater} #{where.value} LIMIT 1;
  37. </select>
  38. <selectid="queryListByWhere"parameterType="Where"resultMap="pojoResultMap">
  39. SELECT
  40. *
  41. FROM
  42. <includerefid="tableName"/>
  43. WHERE ${where.column} ${where.operater} #{where.value};
  44. </select>
  45. <insertid="save">
  46. INSERT INTO <includerefid="tableName"/> VALUES (#{orderId},#{payment},#{paymentType},#{postFee},#{status},#{createTime},#{updateTime},#{paymentTime},#{consignTime},#{endTime},#{closeTime},#{shippingName},#{shippingCode},#{userId},#{buyerMessage},#{buyerNick},#{buyerRate});
  47. INSERT INTO tb_order_item VALUES
  48. <foreachcollection="orderItems"item="item"separator=",">
  49. (#{item.itemId},#{orderId},#{item.num},#{item.title},#{item.price},#{item.totalFee},#{item.picPath})
  50. </foreach>
  51. ;
  52. 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());
  53. </insert>
  54. <updateid="update">
  55. UPDATE <includerefid="tableName"/>
  56. <set>
  57. <iftest="payment !=null and payment != ''">
  58. payment = #{payment},
  59. </if>
  60. <iftest="postFee !=null and postFee != ''">
  61. post_fee = #{postFee},
  62. </if>
  63. <iftest="status !=null and status != ''">
  64. status = #{status},
  65. </if>
  66. <iftest="updateTime !=null and updateTime != ''">
  67. update_time = #{updateTime},
  68. </if>
  69. <iftest="paymentTime !=null and paymentTime != ''">
  70. payment_time = #{paymentTime},
  71. </if>
  72. <iftest="consignTime !=null and consignTime != ''">
  73. consign_time = #{consignTime},
  74. </if>
  75. <iftest="endTime !=null and endTime != ''">
  76. end_time = #{endTime},
  77. </if>
  78. <iftest="closeTime !=null and closeTime != ''">
  79. close_time = #{closeTime},
  80. </if>
  81. <iftest="shippingName !=null and shippingName != ''">
  82. shipping_name = #{shippingName},
  83. </if>
  84. <iftest="shippingCode !=null and shippingCode != ''">
  85. shipping_code = #{shippingCode},
  86. </if>
  87. <iftest="buyerMessage !=null and buyerMessage != ''">
  88. buyer_message = #{buyerMessage},
  89. </if>
  90. <iftest="buyerRate !=null and buyerRate != ''">
  91. buyer_rate = #{buyerRate},
  92. </if>
  93. </set>
  94. WHERE order_id = #{orderId};
  95. </update>
  96. <deleteid="deleteByID"parameterType="Long">
  97. DELETE FROM <includerefid="tableName"/> WHERE order_id = #{orderId};
  98. DELETE FROM tb_order_item WHERE order_id = #{orderId};
  99. </delete>
  100. <deleteid="deleteByIDS"parameterType="list">
  101. DELETE FROM <includerefid="tableName"/> WHERE order_id IN
  102. <foreachcollection="ids"item="id"open="("close=")"separator=",">
  103. #{id}
  104. </foreach>;
  105. DELETE FROM tb_order_item WHERE order_id IN
  106. <foreachcollection="ids"item="id"open="("close=")"separator=",">
  107. #{id}
  108. </foreach>;
  109. </delete>
  110. <updateid="paymentOrderScan"parameterType="Date">
  111. UPDATE tb_order SET
  112. status = 6,
  113. update_time = NOW(),
  114. close_time = NOW(),
  115. end_time = NOW()
  116. WHERE status = 1 AND payment_type = 1 AND create_time &lt;= #{date}
  117. </update>
  118. </mapper>

其中此代表延迟加载

  1. <resultMaptype="Order"id="pojoResultMap"autoMapping="true">
  2. <idcolumn="order_id"property="orderId"/>
  3. <associationproperty="orderShipping"javaType="OrderShipping"column="order_id"select="queryOrderShippingByOrderId"autoMapping="true"></association>
  4. <collectionproperty="orderItems"javaType="Arraylist"ofType="OrderItem"autoMapping="true"select="queryOrderItemByOrderId"column="order_id">
  5. </collection>
  6. </resultMap>

调用方法:

  1. http://order.taotao.com/order/query/31537859410409

返回数据JSON

  1. {
  2. "orderId":"31537859410409",
  3. "payment":"5288",
  4. "paymentType":null,
  5. "postFee":"0",
  6. "status":1,
  7. "createTime":1537859410000,
  8. "updateTime":1537859410000,
  9. "paymentTime":null,
  10. "consignTime":null,
  11. "endTime":null,
  12. "closeTime":null,
  13. "shippingName":null,
  14. "shippingCode":null,
  15. "userId":3,
  16. "buyerMessage":null,
  17. "buyerNick":"zhang123",
  18. "buyerRate":0,
  19. "orderItems":[
  20. {
  21. "itemId":9,
  22. "orderId":"31537859410409",
  23. "num":1,
  24. "title":"苹果(Apple)iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机3",
  25. "price":5288,
  26. "totalFee":5288,
  27. "picPath":"http://image.taotao.com/images/2015/03/06/2015030610045320609720.jpg"
  28. }
  29. ],
  30. "orderShipping":{
  31. "orderId":"31537859410409",
  32. "receiverName":"张志君",
  33. "receiverPhone":"",
  34. "receiverMobile":"15800000000",
  35. "receiverState":"上海",
  36. "receiverCity":"上海",
  37. "receiverDistrict":"闵行区",
  38. "receiverAddress":"三鲁公路3279号 明浦广场 3号楼 205室",
  39. "receiverZip":"200000",
  40. "created":1537859410000,
  41. "updated":1537859410000
  42. }
  43. }
查询出的结果包括订单信息以及订单商品信息还有快递信息

主要内容是根据传智播客视频学习总结的小结。

Mybatis中的延迟加载的使用方法的更多相关文章

  1. 【MyBatis学习11】MyBatis中的延迟加载

    1. 什么是延迟加载 举个例子:如果查询订单并且关联查询用户信息.如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息.把对用户信息的按需去查询就是延迟加载. 所以延迟加载即先从单表 ...

  2. mybatis中的延迟加载

    一.延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 延迟加载:先 ...

  3. @MyBatis中的if...else...

    <select id="selectSelective" resultMap="xxx" parameterType="xxx"> ...

  4. mybatis 中 if else 用法

    mybaits 中没有 else 要用 chose when otherwise 代替 下面就是MyBatis中的if....else...表示方法 <choose> <when t ...

  5. Mybatis中的N+1问题与延迟加载

    0.什么是N+1问题? 在查询中一下子取出所有属性,就会使数据库多执行几条毫无意义的SQL .实际中不需要把所有信息都加载进来,因为有些信息并不常用,加载它们会多执行几条毫无用处的 SQL,导致数据库 ...

  6. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  7. mybatis中的updateByExampleSelective方法怎么使用

    mybatis中的updateByExampleSelective方法怎么使用.sendDetailMapper.updateByExampleSelective(sendDetail, m);参数m ...

  8. 【mybatis】mybatis中避免where空条件后面添加1=1垃圾条件的 优化方法

    在mybatis中拼接查询语句,偶尔会出现where后面可能一个字段的值都没有,就导致所有条件无效,导致where没有存在的意义:但也有可能这些条件会存在.那解决这个问题的方法,最常见的就是: 在wh ...

  9. Mybatis中实体类属性与数据库列表间映射方法介绍

               这篇文章主要介绍了Mybatis中实体类属性与数据列表间映射方法介绍,一共四种方法方法,供大家参考.         Mybatis不像Hibernate中那么自动化,通过@Co ...

随机推荐

  1. Cannot subclass final class class com.sun.proxy.$Proxy

    背景 这个错误是我在使用AOP动态切换数据库,实现数据库的读写分离的时候出现的问题,使用到的系统环境是: <spring.version>3.2.6.RELEASE</spring. ...

  2. 完全定制UITabBarViewController

    完全定制UITabBarViewController 效果   源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 TotalCusto ...

  3. cocos2d-x 保持屏幕点亮及自动变灰

    很早之前遇到的问题,现在记录一下.有一家Android渠道(抱歉,时间太长了已经记不大清楚是哪一家了 oppo/联想/酷派?)在我们提交新版本时拒绝了,理由是:手机背光状态下,屏幕不会自动变灰. 这里 ...

  4. Asp.Net 拦截请求自定义处理

    需求: 在Aps.Net 应用中,对于浏览器请求的部分url的地址自定义处理,不交给路由系统或页面. 解决方案: 在全局文件Global.asax中 ,提供Application_BeginReque ...

  5. Asp.Net WebApi开启Session回话

    一.在WebApi项目中默认没有开启Session回话支持.需要在Global中的Init()方法中指定会员需要支持的类型 public class WebApiApplication : Syste ...

  6. git: error while loading shared libraries: libiconv.so.2

    git安装之后出现:git: error while loading shared libraries: libiconv.so.2: cannot open shared object file: ...

  7. AD各种布线方法总结

    1.常规布线:不详细说了,是个人就知道怎么弄.需要说明的是在布线过程中,可按小键盘的*键或大键盘的数字2键添加一个过孔:按L键可以切换布线层:按数字3可设定最小线宽.典型线宽.最大线宽的值进行切换. ...

  8. iOS截屏方法

    //获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...

  9. (转)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 ...

  10. 【矩阵乘】【DP】【codevs 1305】Freda的道路

    1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级: 大师 Master 题目描写叙述 Description Freda要到Rainbow的城堡去玩了. 我们能 ...