mapper.xml.vm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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="${packageName}.mapper.${ClassName}Mapper">
  4. <sql id="${className}Columns">
  5. #foreach ($column in $columns)
  6. a.${column.columnName} as "${column.javaField}"#if($foreach.hasNext),
  7. #else
  8. #end
  9. #end
  10. </sql>
  11. <sql id="${className}Joins">
  12. </sql>
  13. <sql id="commonWhere">
  14. <if test="parentId != null and parentId != ''">
  15. and a.parent_id = #{parentId}
  16. </if>
  17. <if test="parentIds != null and parentIds != ''">
  18. and a.parent_ids like concat(#{parentIds},'%') and a.parent_ids &lt;&gt; #{parentIds}
  19. </if>
  20. #foreach($column in $columns)
  21. #set($queryType=$column.queryType)
  22. #set($javaField=$column.javaField)
  23. #set($javaType=$column.javaType)
  24. #set($columnName=$column.columnName)
  25. #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  26. #if($column.query || $columnName == $subTableFkName)
  27. ##非日期
  28. #if($javaType != 'Date')
  29. #if($column.queryType == "EQ")
  30. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  31. and a.$columnName = #{$javaField}
  32. </if>
  33. #elseif($queryType == "NE")
  34. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  35. and a.$columnName != #{$javaField}
  36. </if>
  37. #elseif($queryType == "GT")
  38. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  39. and a.$columnName &gt; #{$javaField}
  40. </if>
  41. #elseif($queryType == "GTE")
  42. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  43. and a.$columnName &gt;= #{$javaField}
  44. </if>
  45. #elseif($queryType == "LT")
  46. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  47. and a.$columnName &lt; #{$javaField}
  48. </if>
  49. #elseif($queryType == "LTE")
  50. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  51. and a.$columnName &lt;= #{$javaField}
  52. </if>
  53. #elseif($queryType == "LIKE")
  54. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  55. and a.$columnName like concat('%', #{$javaField}, '%')
  56. </if>
  57. #end
  58. #else
  59. ##日期
  60. #if($column.queryType == "EQ")
  61. <if test="begin$AttrName != null and begin$AttrName != ''">
  62. and date_format(a.$columnName,'%y%m%d') = date_format(#{begin$AttrName},'%y%m%d')
  63. </if>
  64. #elseif($queryType == "GT")
  65. <if test="begin$AttrName != null and begin$AttrName != ''">
  66. and date_format(a.$columnName,'%y%m%d') &gt; date_format(#{begin$AttrName},'%y%m%d')
  67. </if>
  68. #elseif($queryType == "GTE")
  69. <if test="begin$AttrName != null and begin$AttrName != ''">
  70. and date_format(a.$columnName,'%y%m%d') &gt;= date_format(#{begin$AttrName},'%y%m%d')
  71. </if>
  72. #elseif($queryType == "LT")
  73. <if test="end$AttrName != null and end$AttrName != ''">
  74. and date_format(a.$columnName,'%y%m%d') &lt; date_format(#{end$AttrName},'%y%m%d')
  75. /if>
  76. #elseif($queryType == "LTE")
  77. <if test="end$AttrName != null and end$AttrName != ''">
  78. and date_format(a.$columnName,'%y%m%d') &lt;= date_format(#{end$AttrName},'%y%m%d')
  79. </if>
  80. #elseif($queryType == "BETWEEN")
  81. <if test="begin$AttrName != null and begin$AttrName != ''">
  82. and date_format(a.$columnName,'%y%m%d') &gt;= date_format(#{begin$AttrName},'%y%m%d')
  83. </if>
  84. <if test="end$AttrName != null and end$AttrName != ''">
  85. and date_format(a.$columnName,'%y%m%d') &lt;= date_format(#{end$AttrName},'%y%m%d')
  86. </if>
  87. #end
  88. #end
  89. #end
  90. #end
  91. </sql>
  92. <sql id="commonSorts">
  93. order by a.tree_sorts
  94. </sql>
  95. <select id="get" resultType="${ClassName}">
  96. SELECT
  97. <include refid="${className}Columns"/>
  98. FROM ${tableName} a
  99. <include refid="${className}Joins"/>
  100. WHERE a.id = #{id}
  101. </select>
  102. <select id="findList" resultType="${ClassName}">
  103. SELECT
  104. <include refid="${className}Columns"/>
  105. FROM ${tableName} a
  106. <include refid="${className}Joins"/>
  107. <where>
  108. a.del_flag = #{DEL_FLAG_NORMAL}
  109. <include refid="commonWhere"/>
  110. </where>
  111. <include refid="commonSorts"/>
  112. </select>
  113. <select id="findCount" resultType="integer">
  114. SELECT
  115. count(1)
  116. FROM ${tableName} a
  117. where a.del_flag = #{DEL_FLAG_NORMAL}
  118. <include refid="commonWhere"/>
  119. </select>
  120. <select id="findListWithUnique" resultType="${ClassName}">
  121. SELECT
  122. a.id
  123. FROM ${tableName} a
  124. <include refid="${className}Joins"/>
  125. <where>
  126. a.del_flag = #{DEL_FLAG_NORMAL}
  127. <if test="notEqualId != null and notEqualId != ''">
  128. AND id != #{notEqualId}
  129. </if>
  130. #foreach($column in $columns)
  131. #set($javaField=$column.javaField)
  132. #set($javaType=$column.javaType)
  133. #set($columnName=$column.columnName)
  134. #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  135. #if($column.isUnique == "1")
  136. <if test="$javaField != null #if($javaType == 'String' )and $javaField.trim() != ''#end">
  137. and $columnName = #{$javaField}
  138. </if>
  139. #end
  140. #end
  141. </where>
  142. </select>
  143. <!-- 根据条件查询节点 -->
  144. <select id="findMaxSort" resultType="integer">
  145. SELECT
  146. max(a.tree_sort)
  147. FROM ${tableName} a
  148. WHERE a.del_flag = #{DEL_FLAG_NORMAL}
  149. <if test="parentId != null and parentId != ''">
  150. and a.parent_id = #{parentId}
  151. </if>
  152. </select>
  153. <!-- 根据条件查询节点 -->
  154. <select id="search${ClassName}Tree" parameterType="java.util.Map" resultType="${ClassName}">
  155. select
  156. <include refid="${className}Columns"/>
  157. from ${tableName} a
  158. <where>
  159. and a.del_flag = '0'
  160. <include refid="commonWhere"/>
  161. </where>
  162. <include refid="commonSorts"/>
  163. </select>
  164. <!--根据ids获取所有的节点-->
  165. <select id="search${ClassName}TreeByIds" parameterType="java.util.List" resultType="${ClassName}">
  166. select
  167. <include refid="${className}Columns"/>
  168. from ${tableName} a
  169. <where>
  170. and a.del_flag = '0'
  171. and ( 1 != 1
  172. <foreach collection="ids" item="idsItem">
  173. or a.id in
  174. <foreach collection="idsItem" item="id" open="(" separator="," close=")">
  175. #{id}
  176. </foreach>
  177. </foreach>
  178. )
  179. </where>
  180. <include refid="commonSorts"/>
  181. </select>
  182. <insert id="insert">
  183. INSERT INTO ${tableName}(
  184. #foreach($column in $columns)
  185. $column.columnName#if($foreach.hasNext),
  186. #end
  187. #end
  188. ) VALUES (
  189. #foreach($column in $columns)
  190. #{$column.javaField}#if($foreach.hasNext),
  191. #end
  192. #end
  193. )
  194. </insert>
  195. #if($hasDisableEnable)
  196. <!--更新状态-->
  197. <update id="updateStatus">
  198. UPDATE ${tableName} SET
  199. status = #{status},
  200. version = version + 1
  201. WHERE id = #{id}
  202. </update>
  203. #end
  204. <update id="update">
  205. UPDATE ${tableName} SET
  206. #foreach($column in $columns)
  207. #if($column.javaField != "id" && $column.javaField != "createBy" && $column.javaField != "createDept" && $column.javaField != "createTime" && $column.javaField != "id" && $column.javaField != "version" && $column.javaField != "delFlag")
  208. $column.columnName = #{$column.javaField},
  209. #end
  210. #end
  211. version = version + 1
  212. WHERE id = #{id} and version = #{version}
  213. </update>
  214. <update id="update${ClassName}Children" parameterType="java.util.List">
  215. update ${tableName} set parent_ids =
  216. <foreach collection="${className}List" item="item" index="index"
  217. separator=" " open="case id" close="end">
  218. when #{item.id} then #{item.parentIds}
  219. </foreach>
  220. , tree_sorts =
  221. <foreach collection="${className}List" item="item" index="index"
  222. separator=" " open="case id" close="end">
  223. when #{item.id} then #{item.treeSorts}
  224. </foreach>
  225. , tree_level =
  226. <foreach collection="${className}List" item="item" index="index"
  227. separator=" " open="case id" close="end">
  228. when #{item.id} then #{item.treeLevel}
  229. </foreach>
  230. where id in
  231. <foreach collection="${className}List" item="item" index="index"
  232. separator="," open="(" close=")">
  233. #{item.id}
  234. </foreach>
  235. </update>
  236. <update id="delete">
  237. UPDATE ${tableName} SET
  238. del_flag = #{DEL_FLAG_DELETE}
  239. WHERE id = #{id}
  240. </update>
  241. </mapper>