ProductMapper.xml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.mapper.ProductMapper">
  6. <sql id="Base_Column_List">
  7. product.id,product_state,product_name,product_icons,product_type_ids,product_bid_price,product_sell_price
  8. </sql>
  9. <resultMap id="productWithProductType" type="com.example.entity.Product">
  10. <id property="id" column="id"></id>
  11. <result property="productState" column="product_state"/>
  12. <result property="productName" column="product_name"/>
  13. <result property="productIcons" column="product_icons"/>
  14. <result property="productBidPrice" column="product_bid_price"/>
  15. <result property="productSellPrice" column="product_sell_price"/>
  16. <result property="productTypeIds" column="product_type_ids" jdbcType="VARCHAR" typeHandler="com.example.typeHandler.ArrayIntegerTypeHadnler" />
  17. <!-- 店铺产品联合属性 List -->
  18. <collection property="productTypes" javaType="list" ofType="com.example.entity.ProductType">
  19. <id property="id" column="productTypeId"/>
  20. <result property="productTypeName" column="productTypeName"/>
  21. </collection>
  22. </resultMap>
  23. <select id="selectAll" resultMap="productWithProductType">
  24. select product.*,
  25. product_type.id as productTypeId,
  26. product_type_name as productTypeName
  27. from product
  28. left join product_type on JSON_CONTAINS(product.product_type_ids,CAST(product_type.id as JSON),'$')
  29. <where>
  30. <if test="id != null"> and product.id = #{id}</if>
  31. <if test="productState != null"> and product_state like concat('%', #{productState}, '%')</if>
  32. <if test="productName != null"> and product_name like concat('%', #{productName}, '%')</if>
  33. <if test="productIcons != null"> and product_icons like concat('%', #{productIcons}, '%')</if>
  34. <if test="productTypeIds != null"> and product_type_ids like concat('%', #{productIds,jdbcType=VARCHAR,typeHandler=com.example.typeHandler.ArrayIntegerTypeHadnler}, '%')</if>
  35. <if test="productBidPrice != null"> and product_bid_price = #{productBidPrice}</if>
  36. <if test="productSellPrice != null"> and product_sell_price = #{productSellPrice}</if>
  37. </where>
  38. order by product_type.id desc
  39. </select>
  40. <select id="selectById" resultType="com.example.entity.Product">
  41. select
  42. <include refid="Base_Column_List" />
  43. from product
  44. where id = #{id}
  45. </select>
  46. <select id="selectByStoreId" resultType="com.example.entity.Product">
  47. select product.*, store.id as storeId
  48. from store
  49. left join product on JSON_CONTAINS(store.products_ids,CAST(product.id as JSON),'$')
  50. where store.id = #{storeId}
  51. </select>
  52. <delete id="deleteById">
  53. delete from product
  54. where id = #{id}
  55. </delete>
  56. <insert id="insert" parameterType="com.example.entity.Product" useGeneratedKeys="true" keyProperty="id">
  57. insert into product
  58. <trim prefix="(" suffix=")" suffixOverrides=",">
  59. <if test="id != null">id,</if>
  60. <if test="productState != null">product_state,</if>
  61. <if test="productName != null">product_name,</if>
  62. <if test="productIcons != null">product_icons,</if>
  63. <if test="productTypeIds != null">product_type_ids,</if>
  64. <if test="productBidPrice != null">product_bid_price,</if>
  65. <if test="productSellPrice != null">product_sell_price,</if>
  66. </trim>
  67. <trim prefix="values (" suffix=")" suffixOverrides=",">
  68. <if test="id != null">#{id},</if>
  69. <if test="productState != null">#{productState},</if>
  70. <if test="productName != null">#{productName},</if>
  71. <if test="productIcons != null">#{productIcons},</if>
  72. <if test="productTypeIds != null">#{productTypeIds},</if>
  73. <if test="productBidPrice != null">#{productBidPrice},</if>
  74. <if test="productSellPrice != null">#{productSellPrice},</if>
  75. </trim>
  76. </insert>
  77. <update id="updateById" parameterType="com.example.entity.Product">
  78. update product
  79. <set>
  80. <if test="productState != null">
  81. product_state = #{productState},
  82. </if>
  83. <if test="productName != null">
  84. product_name = #{productName},
  85. </if>
  86. <if test="productIcons != null">
  87. product_icons = #{productIcons},
  88. </if>
  89. <if test="productTypeIds != null">
  90. product_type_ids = #{productTypeIds,jdbcType=VARCHAR,typeHandler=com.example.typeHandler.ArrayIntegerTypeHadnler},
  91. </if>
  92. <if test="productBidPrice != null">
  93. product_bid_price = #{productBidPrice},
  94. </if>
  95. <if test="productSellPrice != null">
  96. product_sell_price = #{productSellPrice},
  97. </if>
  98. </set>
  99. where id = #{id}
  100. </update>
  101. </mapper>