ResultMap映射

  1. <!-- 映射 -->
  2. <resultMap id="clazzTimesMap" type="com.cskaoyan.bean.one2more.Clazz">
  3. <id column="id" property="id"/>
  4. <result column="name" property="name"/>
  5. <!--
  6. collection : 如果引用类型是一个集合,那么我们就使用 collection标签
  7. ofType: 集合里面元素的类型
  8. select : 第二次查询SQL语句的坐标
  9. column: 传递的列名
  10. -->
  11. <collection property="studentList"
  12. ofType="com.cskaoyan.bean.one2more.Student"
  13. select="com.cskaoyan.mapper.ClazzMapper.queryStudentByClazzId"
  14. column="id"/>
  15. </resultMap>
  16. <!-- 第二次查询 -->
  17. <select id="queryStudentByClazzId" resultType="com.cskaoyan.bean.one2more.Student">
  18. select id,name,age,gender,clazz_id as clazzId from student where clazz_id = #{id}
  19. </select>
  20. <!-- 一对多的分次查询 查询入口-->
  21. <select id="queryClazzWithStudentListByIdUseTimes" resultMap="clazzTimesMap">
  22. select id,name from clazz where id = #{id}
  23. </select>
  1. <resultMap id="orderMap" type="com.wei.order.model.vo.ShowOrderVo">
  2. <id column="id" property="id"/>
  3. <result column="userId" property="userId"/>
  4. <result column="goodsDetailId" property="goodsDetailId"/>
  5. <result column="goods" property="goods"/>
  6. <result column="specId" property="spec"/>
  7. <result column="goodsNum" property="goodsNum"/>
  8. <result column="amount" property="amount"/>
  9. <result column="stateId" property="stateId"/>
  10. <result column="stateId" property="state"/>
  11. <association property="user" javaType="com.wei.order.model.vo.UserVo">
  12. <result column="nickname" property="nickname"/>
  13. <result column="name" property="name"/>
  14. <result column="address" property="address"/>
  15. <result column="phone" property="phone"/>
  16. </association>
  17. <association property="state"
  18. javaType="java.lang.String"
  19. column="stateId"
  20. select="com.wei.order.dao.OrderDao.selectStateById"/>
  21. <association property="spec"
  22. javaType="java.lang.String"
  23. column="specId"
  24. select="com.wei.order.dao.OrderDao.selectSpecById"/>
  25. </resultMap>
  26. <!--第二次查询,根据stateId查状态-->
  27. <select id="selectStateById" resultType="java.lang.String">
  28. select state from `condition`
  29. <where>
  30. <if test="state!=-1">
  31. id=#{stateId}
  32. </if>
  33. </where>
  34. </select>
  35. <!--第三次查询,根据型号id查型号-->
  36. <select id="selectSpecById" resultType="java.lang.String">
  37. SELECT specName as spec
  38. FROM `spec`
  39. WHERE id = #{specId}
  40. </select>
  41. <!--第一次查询-->
  42. <select id="selectAllOrderByBo" resultMap="orderMap">
  43. select id,
  44. userId,
  45. goodsId as goodsDetailId,
  46. goods,
  47. specId,
  48. goodsNum,
  49. amount,
  50. stateId,
  51. nickname,
  52. address,
  53. receiver as `name`,
  54. phone
  55. from `order2`
  56. <where>
  57. <if test="bo.id != null and bo.id != ''">
  58. id = #{bo.id}
  59. </if>
  60. <if test="bo.name != null and bo.name != ''">
  61. and nickname like concat("%", #{bo.name}, "%")
  62. </if>
  63. <if test="bo.address != null and bo.address != ''">
  64. and address like concat("%", #{bo.address}, "%")
  65. </if>
  66. <if test="bo.goods != null and bo.goods != ''">
  67. and goods like concat("%", #{bo.goods}, "%")
  68. </if>
  69. <if test="bo.moneyLimit1 != null and bo.moneyLimit1 != ''">
  70. and amount &lt;= #{bo.moneyLimit1}
  71. </if>
  72. <if test="bo.moneyLimit1 != null and bo.moneyLimit1 != ''">
  73. and amount &gt;= #{bo.moneyLimit1}
  74. </if>
  75. <if test="bo.state !=-1">
  76. and stateId = #{bo.state}
  77. </if>
  78. </where>
  79. limit #{bo.pagesize} offset ${(bo.currentPage - 1) * bo.pagesize}
  80. </select>

字符串拼接

  1. <select id="selectGoodsByName" resultType="com.wei.mall.model.vo.GoodsSearchVo">
  2. select img,
  3. price,
  4. `name`,
  5. typeId,
  6. id
  7. from `goods`
  8. where `name` like concat("%", #{keyword}, "%")
  9. </select>

foreach

  1. <update id="updateOrderByList" parameterType="list">
  2. <foreach collection="cartList" item="item">
  3. update `order2` set
  4. stateId = 1,
  5. goodsNum = #{item.goodsNum},
  6. amount = #{item.amount}
  7. where
  8. id = #{item.id};
  9. </foreach>
  10. </update>