mapper.xml.vm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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="${packageName}.mapper.${ClassName}Mapper">
  6. <resultMap type="${ClassName}" id="${ClassName}Result">
  7. #foreach ($column in $columns)
  8. <result property="${column.javaField}" column="${column.columnName}" />
  9. #end
  10. #if($table.tree)
  11. <result property="parentName" column="parent_name" />
  12. #end
  13. </resultMap>
  14. #if($table.sub)
  15. <resultMap id="${ClassName}${subClassName}Result" type="${ClassName}" extends="${ClassName}Result">
  16. <collection property="${subclassName}List" notNullColumn="sub_${subTable.pkColumn.columnName}" javaType="java.util.List" resultMap="${subClassName}Result" />
  17. </resultMap>
  18. <resultMap type="${subClassName}" id="${subClassName}Result">
  19. #foreach ($column in $subTable.columns)
  20. <result property="${column.javaField}" column="sub_${column.columnName}" />
  21. #end
  22. </resultMap>
  23. #end
  24. <sql id="select${ClassName}Vo">
  25. select#foreach($column in $columns) $column.columnName#if($velocityCount != $columns.size()),#end#end from ${tableName}
  26. </sql>
  27. <select id="select${ClassName}List" parameterType="${ClassName}" resultMap="${ClassName}Result">
  28. <include refid="select${ClassName}Vo"/>
  29. <where>
  30. #foreach($column in $columns)
  31. #set($queryType=$column.queryType)
  32. #set($javaField=$column.javaField)
  33. #set($javaType=$column.javaType)
  34. #set($columnName=$column.columnName)
  35. #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  36. #if($column.query)
  37. #if($column.queryType == "EQ")
  38. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName = #{$javaField}</if>
  39. #elseif($queryType == "NE")
  40. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName != #{$javaField}</if>
  41. #elseif($queryType == "GT")
  42. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &gt; #{$javaField}</if>
  43. #elseif($queryType == "GTE")
  44. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &gt;= #{$javaField}</if>
  45. #elseif($queryType == "LT")
  46. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &lt; #{$javaField}</if>
  47. #elseif($queryType == "LTE")
  48. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName &lt;= #{$javaField}</if>
  49. #elseif($queryType == "LIKE")
  50. <if test="$javaField != null #if($javaType == 'String' ) and $javaField.trim() != ''#end"> and $columnName like concat(concat('%', #{$javaField}), '%')</if>
  51. #elseif($queryType == "BETWEEN")
  52. <if test="params.begin$AttrName != null and params.begin$AttrName != '' and params.end$AttrName != null and params.end$AttrName != ''"> and $columnName between #{params.begin$AttrName} and #{params.end$AttrName}</if>
  53. #end
  54. #end
  55. #end
  56. </where>
  57. #if($table.tree)
  58. order by ${tree_parent_code}
  59. #end
  60. </select>
  61. <select id="select${ClassName}ById" parameterType="${pkColumn.javaType}" resultMap="#if($table.sub)${ClassName}${subClassName}Result#else${ClassName}Result#end">
  62. #if($table.crud)
  63. <include refid="select${ClassName}Vo"/>
  64. where ${pkColumn.columnName} = #{${pkColumn.javaField}}
  65. #elseif($table.tree)
  66. select#foreach($column in $columns) t.$column.columnName,#end p.${tree_name} as parent_name
  67. from ${tableName} t
  68. left join ${tableName} p on p.${pkColumn.columnName} = t.${tree_parent_code}
  69. where t.${pkColumn.columnName} = #{${pkColumn.javaField}}
  70. #elseif($table.sub)
  71. select#foreach($column in $columns) a.$column.columnName#if($velocityCount != $columns.size()),#end#end,
  72. #foreach($column in $subTable.columns) b.$column.columnName as sub_$column.columnName#if($velocityCount != $subTable.columns.size()),#end#end
  73. from ${tableName} a
  74. left join ${subTableName} b on b.${subTableFkName} = a.${pkColumn.columnName}
  75. where a.${pkColumn.columnName} = #{${pkColumn.javaField}}
  76. #end
  77. </select>
  78. <insert id="insert${ClassName}" parameterType="${ClassName}">
  79. #if($pkColumn.increment)
  80. <selectKey keyProperty="${pkColumn.javaField}" resultType="long" order="BEFORE">
  81. SELECT seq_${tableName}.NEXTVAL as ${pkColumn.javaField} FROM DUAL
  82. </selectKey>
  83. #end
  84. insert into ${tableName}
  85. <trim prefix="(" suffix=")" suffixOverrides=",">
  86. #foreach($column in $columns)
  87. <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">$column.columnName,</if>
  88. #end
  89. </trim>
  90. <trim prefix="values (" suffix=")" suffixOverrides=",">
  91. #foreach($column in $columns)
  92. <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">#{$column.javaField},</if>
  93. #end
  94. </trim>
  95. </insert>
  96. <update id="update${ClassName}" parameterType="${ClassName}">
  97. update ${tableName}
  98. <trim prefix="SET" suffixOverrides=",">
  99. #foreach($column in $columns)
  100. #if($column.columnName != $pkColumn.columnName)
  101. <if test="$column.javaField != null#if($column.javaType == 'String' && $column.required) and $column.javaField != ''#end">$column.columnName = #{$column.javaField},</if>
  102. #end
  103. #end
  104. </trim>
  105. where ${pkColumn.columnName} = #{${pkColumn.javaField}}
  106. </update>
  107. <delete id="delete${ClassName}ById" parameterType="${pkColumn.javaType}">
  108. delete from ${tableName} where ${pkColumn.columnName} = #{${pkColumn.javaField}}
  109. </delete>
  110. <delete id="delete${ClassName}ByIds" parameterType="String">
  111. delete from ${tableName} where ${pkColumn.columnName} in
  112. <foreach item="${pkColumn.javaField}" collection="array" open="(" separator="," close=")">
  113. #{${pkColumn.javaField}}
  114. </foreach>
  115. </delete>
  116. #if($table.sub)
  117. <delete id="delete${subClassName}By${subTableFkClassName}s" parameterType="String">
  118. delete from ${subTableName} where ${subTableFkName} in
  119. <foreach item="${subTableFkclassName}" collection="array" open="(" separator="," close=")">
  120. #{${subTableFkclassName}}
  121. </foreach>
  122. </delete>
  123. <delete id="delete${subClassName}By${subTableFkClassName}" parameterType="Long">
  124. delete from ${subTableName} where ${subTableFkName} = #{${subTableFkclassName}}
  125. </delete>
  126. <insert id="batch${subClassName}">
  127. insert all
  128. <foreach item="item" index="index" collection="list">
  129. into ${subTableName}(#foreach($column in $subTable.columns) $column.columnName#if($velocityCount != $subTable.columns.size()),#end#end) values (#foreach($column in $subTable.columns) #{item.$column.javaField}#if($velocityCount != $subTable.columns.size()),#end#end)
  130. </foreach>
  131. SELECT 1 FROM DUAL
  132. </insert>
  133. #end
  134. </mapper>