1. ##引入mybatis支持
    2. $!{mybatisSupport.vm}
    3. ##设置保存名称与保存位置
    4. $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
    5. $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
    6. ##拿到主键
    7. #if(!$tableInfo.pkColumn.isEmpty())
    8. #set($pk = $tableInfo.pkColumn.get(0))
    9. #end
    10. <?xml version="1.0" encoding="UTF-8"?>
    11. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    12. <mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper">
    13. <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
    14. #foreach($column in $tableInfo.fullColumn)
    15. <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
    16. #end
    17. </resultMap>
    18. <!--查询单个-->
    19. <select id="queryById" resultMap="$!{tableInfo.name}Map">
    20. select
    21. #allSqlColumn()
    22. from $!tableInfo.obj.name
    23. where $!pk.obj.name = #{$!pk.name}
    24. </select>
    25. <!--查询指定行数据-->
    26. <select id="queryAllByLimit" resultMap="$!{tableInfo.name}Map">
    27. select
    28. #allSqlColumn()
    29. from $!tableInfo.obj.name
    30. <where>
    31. #foreach($column in $tableInfo.fullColumn)
    32. <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
    33. and $!column.obj.name = #{$!column.name}
    34. </if>
    35. #end
    36. </where>
    37. limit #{pageable.offset}, #{pageable.pageSize}
    38. </select>
    39. <!--统计总行数-->
    40. <select id="count" resultType="java.lang.Long">
    41. select count(1)
    42. from $!tableInfo.obj.name
    43. <where>
    44. #foreach($column in $tableInfo.fullColumn)
    45. <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
    46. and $!column.obj.name = #{$!column.name}
    47. </if>
    48. #end
    49. </where>
    50. </select>
    51. <!--新增所有列-->
    52. <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
    53. insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
    54. values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
    55. </insert>
    56. <insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
    57. insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
    58. values
    59. <foreach collection="entities" item="entity" separator=",">
    60. (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
    61. </foreach>
    62. </insert>
    63. <insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
    64. insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
    65. values
    66. <foreach collection="entities" item="entity" separator=",">
    67. (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
    68. </foreach>
    69. on duplicate key update
    70. #foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($velocityHasNext),
    71. #end#end
    72. </insert>
    73. <!--通过主键修改数据-->
    74. <update id="update">
    75. update $!{tableInfo.obj.name}
    76. <set>
    77. #foreach($column in $tableInfo.otherColumn)
    78. <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
    79. $!column.obj.name = #{$!column.name},
    80. </if>
    81. #end
    82. </set>
    83. where $!pk.obj.name = #{$!pk.name}
    84. </update>
    85. <!--通过主键删除-->
    86. <delete id="deleteById">
    87. delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
    88. </delete>
    89. </mapper>