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. LinkedList中将对象按照某一属性排序

    例如,链表 treelist 声明如下: LinkedList<TreeNode> treelist = new LinkedList<TreeNode>(); 其中 Tree ...

  2. 每天一个linux命令-wc命令

    语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...

  3. fabric-ca-server

    fabric-ca-server start -b admin:adminpw -d --db.type mysql --db.datasource "root:rootpwd@tcp(17 ...

  4. JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(下)(各种乱码、验证码、重定向和转发)

    HttpServletRequest获取请求头信息  (1)获取客户机请求头 String getHeader(String name) Enumeration<String> getHe ...

  5. MECE分析法(Mutually Exclusive Collectively Exhaustive)

    什么是MECE分析法? MECE,是Mutually Exclusive Collectively Exhaustive,中文意思是“相互独立,完全穷尽”. 也就是对于一个重大的议题,能够做到不重叠. ...

  6. 转:PHP高效率写法(详解原因)

    FROM : http://my.oschina.net/No5stranger/blog/157585#OSC_h3_1 1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4 ...

  7. python文档生成工具:pydoc、sphinx;django如何使用sphinx?

    文档生成工具: 自带的pydoc,比较差 建议使用sphinx 安装: pip install sphinx 安装主题: 由各种主题,我选择常用的sphinx_rtd_theme pip instal ...

  8. 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 ...

  9. 【BLE】CC2541之发现服务与特征值

    一.简介 本文以SimpleBLECentral工程为例,解析CC2541作为主机时是如何发现从机的服务和特征值的. 二.实验平台 协议栈版本:BLE-CC254x-1.4.0 编译软件:IAR 8. ...

  10. Druid连接池简介和配置

    Druid是什么?有什么作用?  Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. Druid ...