当前位置:   article > 正文

PDManer 自定义 mybatis 的代码模板_pdmaner java代码

pdmaner java代码

目录

前言

PDManer

代码模板

Mapper

Mapper.xml

Entity


前言

平时一直使用PDManer对业务进行数据库表建模,建完模后可以自动生成基础的业务代码,以 mybatis 为例,生成 Entity,Mapper,Mapper.xml 代码。

PDManer里有默认的代码模板,不过在实际开发中需要根据实际情况做调整,便需要自定义代码模板。

记录下实际项目中用过的 mybatis 代码模板,以便以后复用。

PDManer

代码模板

Mapper

  1. {{ var today=new Date();
  2. var fullYear=today.getFullYear();
  3. var month=today.getMonth() + 1;
  4. var days=today.getDate();
  5. var pkVarName = "id";
  6. var pkDataType = "String";
  7. it.entity.fields.forEach(function(field){
  8. if(field.primaryKey){
  9. pkVarName = field.defKey,false;
  10. pkDataType = field["type"];
  11. return;
  12. }
  13. });
  14. var pkgName = it.entity.env.base.nameSpace;
  15. var beanClass = it.entity.env.base.codeRoot;
  16. var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
  17. var serviceClass = beanClass+'Service';
  18. var serviceVarName= beanVarName+'Service';
  19. }}package {{=pkgName}}.mapper;
  20. $blankline
  21. import java.util.List;
  22. import org.apache.ibatis.annotations.Mapper;
  23. import org.apache.ibatis.annotations.Param;
  24. import {{=pkgName}}.beans.entity.{{=beanClass}};
  25. $blankline
  26. /**
  27. * {{=it.entity.defName}};({{=it.entity.defKey}})表数据库访问层
  28. * @author : name
  29. * @date : {{=fullYear}}-{{=month}}-{{=days}}
  30. */
  31. @Mapper
  32. public interface {{=beanClass}}Mapper{
  33. //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓模板生成的基础方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  34. /**
  35. * 通过ID查询单条数据
  36. *
  37. * @param {{=pkVarName}} 主键
  38. * @return 实例对象
  39. */
  40. {{=beanClass}} selectById({{=pkDataType}} {{=pkVarName}});
  41. /**
  42. * 查询列表
  43. *
  44. * @param {{=beanVarName}} 查询条件
  45. * @return 对象列表
  46. */
  47. List<{{=beanClass}}> selectList({{=beanClass}} {{=beanVarName}});
  48. /**
  49. * 统计总行数
  50. *
  51. * @param {{=beanVarName}} 查询条件
  52. * @return 总行数
  53. */
  54. long count({{=beanClass}} {{=beanVarName}});
  55. /**
  56. * 新增数据
  57. *
  58. * @param {{=beanVarName}} 实例对象
  59. * @return 影响行数
  60. */
  61. int insert({{=beanClass}} {{=beanVarName}});
  62. /**
  63. * 批量新增数据
  64. *
  65. * @param entities List<{{=beanClass}}> 实例对象列表
  66. * @return 影响行数
  67. */
  68. int insertBatch(@Param("entities") List<{{=beanClass}}> entities);
  69. /**
  70. * 批量新增或按主键更新数据
  71. *
  72. * @param entities List<{{=beanClass}}> 实例对象列表
  73. * @return 影响行数
  74. */
  75. int insertOrUpdateBatch(@Param("entities") List<{{=beanClass}}> entities);
  76. /**
  77. * 更新数据
  78. *
  79. * @param {{=beanVarName}} 实例对象
  80. * @return 影响行数
  81. */
  82. int update({{=beanClass}} {{=beanVarName}});
  83. /**
  84. * 通过主键删除数据
  85. *
  86. * @param {{=pkVarName}} 主键
  87. * @return 影响行数
  88. */
  89. int deleteById({{=pkDataType}} {{=pkVarName}});
  90. //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑模板生成的基础方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  91. $blankline
  92. //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓自定义方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  93. $blankline
  94. $blankline
  95. $blankline
  96. //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑自定义方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
  97. }

Mapper.xml

  1. {{ var today=new Date();
  2. var fullYear=today.getFullYear();
  3. var month=today.getMonth() + 1;
  4. var days=today.getDate();
  5. var pkVarName = "id";
  6. var pkDataType = "String";
  7. var pkField = "id";
  8. it.entity.fields.forEach(function(field){
  9. if(field.primaryKey){
  10. pkField = field.defKey;
  11. pkVarName = field.defKey;
  12. pkDataType = field["type"];
  13. return;
  14. }
  15. });
  16. var pkgName = it.entity.env.base.nameSpace;
  17. var beanClass = it.entity.env.base.codeRoot;
  18. var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
  19. var serviceClass = beanClass+'Service';
  20. var serviceVarName= beanVarName+'Service';
  21. }}<?xml version="1.0" encoding="UTF-8"?>
  22. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  23. <mapper namespace="{{=pkgName}}.mapper.{{=beanClass}}Mapper">
  24. <resultMap type="{{=pkgName}}.beans.entity.{{=beanClass}}" id="{{=beanClass}}Map">
  25. {{~it.entity.fields:field:index}}
  26. <result property="{{=it.func.camel(field.defKey,false)}}" column="{{=field.defKey}}"/>
  27. {{~}}
  28. </resultMap>
  29. $blankline
  30. <!--↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓模板生成的基础方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-->
  31. <!-- 通过ID查询单条数据 -->
  32. <select id="selectById" resultMap="{{=beanClass}}Map">
  33. select
  34. {{=it.entity.fields.map(function(e,i){
  35. if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
  36. return e.defKey}).join(',')}}
  37. from {{=it.entity.defKey}}
  38. where {{=pkField}} = #{{{=pkVarName}}} and delete_flag = 0
  39. </select>
  40. $blankline
  41. <!--查询列表-->
  42. <select id="selectList" resultMap="{{=beanClass}}Map">
  43. select
  44. {{=it.entity.fields.map(function(e,i){
  45. if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
  46. return e.defKey}).join(',')}}
  47. from {{=it.entity.defKey}}
  48. <where>
  49. delete_flag = 0
  50. {{~it.entity.fields:field:index}}
  51. {{?field.defKey != 'delete_flag'}}<if test={{?field.type == 'Integer'}}"{{=it.func.camel(field.defKey,false)}} != null"{{??}}"{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''"{{?}}>
  52. and {{?field.defKey == 'desc' || field.defKey == 'type' || field.defKey == 'key'}}`{{=field.defKey}}`{{??}}{{=field.defKey}}{{?}} = #{{{=it.func.camel(field.defKey,false)}}}
  53. </if>{{?}}
  54. {{~}}
  55. </where>
  56. </select>
  57. $blankline
  58. <!--统计总行数-->
  59. <select id="count" resultType="java.lang.Long">
  60. select count(1)
  61. from {{=it.entity.defKey}}
  62. <where>
  63. delete_flag = 0
  64. {{~it.entity.fields:field:index}}
  65. {{?field.defKey != 'delete_flag'}}<if test={{?field.type == 'Integer'}}"{{=it.func.camel(field.defKey,false)}} != null"{{??}}"{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''"{{?}}>
  66. and {{?field.defKey == 'desc' || field.defKey == 'type' || field.defKey == 'key' || field.defKey == 'level'}}`{{=field.defKey}}`{{??}}{{=field.defKey}}{{?}} = #{{{=it.func.camel(field.defKey,false)}}}
  67. </if>{{?}}
  68. {{~}}
  69. </where>
  70. </select>
  71. $blankline
  72. <!--新增数据-->
  73. <insert id="insert" keyProperty="{{=pkField}}" useGeneratedKeys="true">
  74. insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){
  75. if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
  76. return e.defKey}).join(',')}})
  77. values ({{=it.entity.fields.map(function(e,i){
  78. if (e.defKey == 'create_date' || e.defKey == 'update_date') return 'now()';
  79. if (e.defKey == 'delete_flag') return '0';
  80. return '#{'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
  81. </insert>
  82. $blankline
  83. <!-- 批量新增数据 -->
  84. <insert id="insertBatch" keyProperty="{{=pkField}}" useGeneratedKeys="true">
  85. insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){
  86. if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
  87. return e.defKey}).join(',')}})
  88. values
  89. <foreach collection="entities" item="entity" separator=",">
  90. ({{=it.entity.fields.map(function(e,i){
  91. if (e.defKey == 'create_date' || e.defKey == 'update_date') return 'now()';
  92. if (e.defKey == 'delete_flag') return '0';
  93. return '#{entity.'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
  94. </foreach>
  95. </insert>
  96. $blankline
  97. <!-- 批量新增或按主键更新数据 -->
  98. <insert id="insertOrUpdateBatch" keyProperty="{{=pkField}}" useGeneratedKeys="true">
  99. insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){
  100. if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level') return '`'+e.defKey+'`';
  101. return e.defKey}).join(',')}})
  102. values
  103. <foreach collection="entities" item="entity" separator=",">
  104. ({{=it.entity.fields.map(function(e,i){
  105. if (e.defKey == 'create_date' || e.defKey == 'update_date') return 'now()';
  106. if (e.defKey == 'delete_flag') return '0';
  107. return '#{entity.'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
  108. </foreach>
  109. on duplicate key update
  110. {{=it.entity.fields.filter(function(e){
  111. if (e.defKey == 'create_date' || e.defKey == 'delete_flag')
  112. return false;
  113. return true;
  114. }).map(function(e,i){
  115. if (e.defKey == 'update_date')
  116. return 'update_date=now()';
  117. if (e.defKey == 'desc' || e.defKey == 'type' || e.defKey == 'key' || e.defKey == 'level')
  118. return '`' + e.defKey + '`' + '=values('+e.defKey+')' ;
  119. return e.defKey + '=values('+it.func.camel(e.defKey,false)+')';}).join(',\n\t\t')
  120. }}
  121. </insert>
  122. $blankline
  123. <!-- 更新数据 -->
  124. <update id="update">
  125. update {{=it.entity.defKey}}
  126. <set>
  127. {{~it.entity.fields:field:index}}
  128. {{?field.defKey != 'create_date' && field.defKey != 'update_date'}}<if test={{?field.type == 'Integer'}}"{{=it.func.camel(field.defKey,false)}} != null"{{??}}"{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''"{{?}}>
  129. {{?field.defKey == 'desc' || field.defKey == 'type' || field.defKey == 'key' || field.defKey == 'level'}}`{{=field.defKey}}`{{??}}{{=field.defKey}}{{?}} = #{{{=it.func.camel(field.defKey,false)}}},
  130. </if>{{??}} {{continue;}} {{?}}
  131. {{~}}
  132. update_date=now()
  133. </set>
  134. where {{=pkField}} = #{{{=pkVarName}}}
  135. </update>
  136. $blankline
  137. <!--通过主键删除-->
  138. <delete id="deleteById">
  139. update {{=it.entity.defKey}} set update_date = now(), delete_flag = 1 where {{=pkField}} = #{{{=pkVarName}}}
  140. </delete>
  141. <!--↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑模板生成的基础方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑-->
  142. $blankline
  143. <!--↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓自定义方法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-->
  144. $blankline
  145. <!--↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑自定义方法↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑-->
  146. </mapper>

Entity

  1. {{ var today=new Date();
  2. var fullYear=today.getFullYear();
  3. var month=today.getMonth() + 1;
  4. var days=today.getDate();
  5. var pkVarName = "id";
  6. var pkDataType = "String";
  7. it.entity.fields.forEach(function(field){
  8. if(field.primaryKey){
  9. pkVarName = it.func.camel(field.defKey,false);
  10. pkDataType = field["type"];
  11. return;
  12. }
  13. });
  14. var pkgName = it.entity.env.base.nameSpace;
  15. var beanClass = it.entity.env.base.codeRoot;
  16. var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
  17. var serviceClass = beanClass+'Service';
  18. var serviceVarName= beanVarName+'Service';
  19. }}package {{=pkgName}}.beans.entity;
  20. $blankline
  21. import java.io.Serializable;
  22. import java.util.Date;
  23. $blankline
  24. /**
  25. * {{=it.entity.defName}};{{=it.entity.comment}}
  26. * @author : name
  27. * @date : {{=fullYear}}-{{=month}}-{{=days}}
  28. */
  29. public class {{=beanClass}} implements Serializable,Cloneable{
  30. {{~it.entity.fields:field:index}}
  31. /** {{=it.func.join(field.defName,field.comment,';')}} */
  32. private {{=field.type}} {{=it.func.camel(field.defKey,false)}} ;
  33. {{~}}
  34. $blankline
  35. {{~it.entity.fields:field:index}}
  36. /** {{=it.func.join(field.defName,field.comment,';')}} */
  37. public {{=field.type}} get{{=it.func.camel(field.defKey,true)}}(){
  38. return this.{{=it.func.camel(field.defKey,false)}};
  39. }
  40. /** {{=it.func.join(field.defName,field.comment,';')}} */
  41. public void set{{=it.func.camel(field.defKey,true)}}({{=field.type}} {{= it.func.camel(field.defKey,false) }}){
  42. this.{{=it.func.camel(field.defKey,false)}}={{=it.func.camel(field.defKey,false)}};
  43. }
  44. {{~}}
  45. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/796295
推荐阅读
相关标签
  

闽ICP备14008679号