TbItemMapper.xml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.pj.project.tb_item.TbItemMapper">
  4. <!-- 增 [G] -->
  5. <insert id="add">
  6. insert into
  7. tb_item (id, type_id, type_name, item_code, item_name, price, unit)
  8. values (#{id}, #{typeId}, #{typeName}, #{itemCode}, #{itemName}, #{price}, #{unit})
  9. </insert>
  10. <!-- 删 -->
  11. <delete id="delete">
  12. delete from tb_item
  13. where id = #{id}
  14. </delete>
  15. <!-- 改 [G] -->
  16. <update id="update">
  17. update tb_item set
  18. id = #{id},
  19. type_id = #{typeId},
  20. type_name = #{typeName},
  21. item_code = #{itemCode},
  22. item_name = #{itemName},
  23. price = #{price},
  24. unit = #{unit}
  25. where id = #{id}
  26. </update>
  27. <!-- ================================== 查询相关 ================================== -->
  28. <!-- select id, type_id, type_name, item_code, item_name, price, unit from tb_item -->
  29. <!-- 通用映射:手动模式 -->
  30. <resultMap id="model" type="com.pj.project.tb_item.TbItem">
  31. <result property="id" column="id" />
  32. <result property="typeId" column="type_id" />
  33. <result property="typeName" column="type_name" />
  34. <result property="itemCode" column="item_code" />
  35. <result property="itemName" column="item_name" />
  36. <result property="price" column="price" />
  37. <result property="unit" column="unit" />
  38. </resultMap>
  39. <!-- 公共查询sql片段 -->
  40. <sql id="select_sql">
  41. select *
  42. from tb_item
  43. </sql>
  44. <!-- 查 - 根据id -->
  45. <select id="getById" resultMap="model">
  46. <include refid="select_sql"></include>
  47. where id = #{id}
  48. </select>
  49. <!-- 查集合 - 根据条件(参数为空时代表忽略指定条件) [G] -->
  50. <select id="getList" resultMap="model">
  51. <include refid="select_sql"></include>
  52. <where>
  53. <if test=' this.has("id") '> and id = #{id} </if>
  54. <if test=' this.has("itemCode") '> and item_code = #{itemCode} </if>
  55. <if test=' this.has("itemName") '> and item_name = #{itemName} </if>
  56. <if test=' this.has("price") '> and price = #{price} </if>
  57. <if test=' this.has("status") '> and status = #{status} </if>
  58. </where>
  59. order by
  60. <choose>
  61. <when test='sortType == 1'> id desc </when>
  62. <when test='sortType == 2'> item_code desc </when>
  63. <when test='sortType == 3'> item_name desc </when>
  64. <when test='sortType == 4'> price desc </when>
  65. <when test='sortType == 5'> need desc </when>
  66. <otherwise> id desc </otherwise>
  67. </choose>
  68. </select>
  69. <!-- 查 - 根据id -->
  70. <select id="getById" resultMap="model">
  71. <include refid="select_sql"></include>
  72. where id = #{id}
  73. </select>
  74. <!-- 查集合 - 根据条件(参数为空时代表忽略指定条件) [G] -->
  75. <select id="getList" resultMap="model">
  76. <include refid="select_sql"></include>
  77. <where>
  78. <if test=' this.has("id") '> and id = #{id} </if>
  79. <if test=' this.has("typeId") '> and type_id = #{typeId} </if>
  80. <if test=' this.has("typeName") '> and type_name = #{typeName} </if>
  81. <if test=' this.has("itemCode") '> and item_code = #{itemCode} </if>
  82. <if test=' this.has("itemName") '> and item_name like concat('%', #{itemName} ,'%') </if>
  83. <if test=' this.has("price") '> and price = #{price} </if>
  84. <if test=' this.has("unit") '> and unit = #{unit} </if>
  85. </where>
  86. order by
  87. <choose>
  88. <when test='sortType == 1'> id desc </when>
  89. <when test='sortType == 2'> type_id desc </when>
  90. <when test='sortType == 3'> type_name desc </when>
  91. <when test='sortType == 4'> item_code desc </when>
  92. <when test='sortType == 5'> item_name desc </when>
  93. <when test='sortType == 6'> price desc </when>
  94. <when test='sortType == 7'> unit desc </when>
  95. <otherwise> id desc </otherwise>
  96. </choose>
  97. </select>
  98. </mapper>