• mybatis plus自定义模板



    mybatis plus自定义模板

              

                     

                                      

    默认模板

                

    mybatis generator默认模板

                          

             

    模板文件说明

    1. entity.java.*:java语言实体类模板文件
    2. entity.kt.*:kotlin语言实体类模板文件
    3. mapper.java.*:mapper接口类模板文件
    4. mapper.xml.*:mapper xml文件模板文件
    5. controller.*:controller模板文件
    6. service.java.*:service接口模板文件
    7. serviceImpl.java.*:service实现类模板文件
    8. 后缀说明:
    9. btl:beetl模板
    10. ftl:freemarker模板
    11. vm:velocity模板

              

                  

                                      

    使用示例

                

    默认模板生成的实体类:@Getter、@Setter注解,未使用@Data注解

    1. @Getter
    2. @Setter
    3. @TableName("t_person")
    4. @ApiModel(value = "Person对象", description = "")
    5. public class Person extends Model {
    6. private static final long serialVersionUID = 1L;
    7. @TableId(value = "id", type = IdType.AUTO)
    8. private Integer id;
    9. private String name;
    10. private Integer age;
    11. @Override
    12. public Serializable pkVal() {
    13. return this.id;
    14. }
    15. }

                  

    自定义实体类模板,使用@Data注解

                          

            

    复制entity.java.vm到resources/entity目录下,引入@Data注解,替换掉@Getter、@Setter

    1. package ${package.Entity};
    2. #foreach($pkg in ${table.importPackages})
    3. import ${pkg};
    4. #end
    5. #if(${swagger})
    6. import io.swagger.annotations.ApiModel;
    7. import io.swagger.annotations.ApiModelProperty;
    8. #end
    9. #if(${entityLombokModel})
    10. import lombok.Data;
    11. import lombok.EqualsAndHashCode;
    12. #if(${chainModel})
    13. import lombok.experimental.Accessors;
    14. #end
    15. #end
    16. /**
    17. *

    18. * $!{table.comment}
    19. *

    20. *
    21. * @author ${author}
    22. * @since ${date}
    23. */
    24. #if(${entityLombokModel})
    25. @Data
    26. @EqualsAndHashCode(callSuper = true)
    27. #if(${chainModel})
    28. @Accessors(chain = true)
    29. #end
    30. #end
    31. #if(${table.convert})
    32. @TableName("${schemaName}${table.name}")
    33. #end
    34. #if(${swagger})
    35. @ApiModel(value = "${entity}对象", description = "$!{table.comment}")
    36. #end
    37. #if(${superEntityClass})
    38. public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
    39. #elseif(${activeRecord})
    40. public class ${entity} extends Model<${entity}> {
    41. #elseif(${entitySerialVersionUID})
    42. public class ${entity} implements Serializable {
    43. #else
    44. public class ${entity} {
    45. #end
    46. #if(${entitySerialVersionUID})
    47. private static final long serialVersionUID = 1L;
    48. #end
    49. ## ---------- BEGIN 字段循环遍历 ----------
    50. #foreach($field in ${table.fields})
    51. #if(${field.keyFlag})
    52. #set($keyPropertyName=${field.propertyName})
    53. #end
    54. #if("$!field.comment" != "")
    55. #if(${swagger})
    56. @ApiModelProperty("${field.comment}")
    57. #else
    58. /**
    59. * ${field.comment}
    60. */
    61. #end
    62. #end
    63. #if(${field.keyFlag})
    64. ## 主键
    65. #if(${field.keyIdentityFlag})
    66. @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
    67. #elseif(!$null.isNull(${idType}) && "$!idType" != "")
    68. @TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
    69. #elseif(${field.convert})
    70. @TableId("${field.annotationColumnName}")
    71. #end
    72. ## 普通字段
    73. #elseif(${field.fill})
    74. ## ----- 存在字段填充设置 -----
    75. #if(${field.convert})
    76. @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
    77. #else
    78. @TableField(fill = FieldFill.${field.fill})
    79. #end
    80. #elseif(${field.convert})
    81. @TableField("${field.annotationColumnName}")
    82. #end
    83. ## 乐观锁注解
    84. #if(${field.versionField})
    85. @Version
    86. #end
    87. ## 逻辑删除注解
    88. #if(${field.logicDeleteField})
    89. @TableLogic
    90. #end
    91. private ${field.propertyType} ${field.propertyName};
    92. #end
    93. ## ---------- END 字段循环遍历 ----------
    94. #if(!${entityLombokModel})
    95. #foreach($field in ${table.fields})
    96. #if(${field.propertyType.equals("boolean")})
    97. #set($getprefix="is")
    98. #else
    99. #set($getprefix="get")
    100. #end
    101. public ${field.propertyType} ${getprefix}${field.capitalName}() {
    102. return ${field.propertyName};
    103. }
    104. #if(${chainModel})
    105. public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
    106. #else
    107. public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
    108. #end
    109. this.${field.propertyName} = ${field.propertyName};
    110. #if(${chainModel})
    111. return this;
    112. #end
    113. }
    114. #end
    115. ## --foreach end---
    116. #end
    117. ## --end of #if(!${entityLombokModel})--
    118. #if(${entityColumnConstant})
    119. #foreach($field in ${table.fields})
    120. public static final String ${field.name.toUpperCase()} = "${field.name}";
    121. #end
    122. #end
    123. #if(${activeRecord})
    124. @Override
    125. public Serializable pkVal() {
    126. #if(${keyPropertyName})
    127. return this.${keyPropertyName};
    128. #else
    129. return null;
    130. #end
    131. }
    132. #end
    133. #if(!${entityLombokModel})
    134. @Override
    135. public String toString() {
    136. return "${entity}{" +
    137. #foreach($field in ${table.fields})
    138. #if($!{foreach.index}==0)
    139. "${field.propertyName}=" + ${field.propertyName} +
    140. #else
    141. ", ${field.propertyName}=" + ${field.propertyName} +
    142. #end
    143. #end
    144. "}";
    145. }
    146. #end
    147. }

               

    CodeGenerator:代码生成器使用自定义模板

    1. public class CodeGenerator {
    2. public static void main(String[] args){
    3. FastAutoGenerator.create("jdbc:mysql://localhost:3366/lihu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8",
    4. "root","123456")
    5. .globalConfig(builder -> builder
    6. .author("lihu").enableSwagger()
    7. .outputDir("src"+ File.separator +"main" + File.separator + "java")
    8. .build())
    9. .strategyConfig(builder -> builder
    10. .enableCapitalMode().addInclude("t_person").addTablePrefix("t_")
    11. .entityBuilder().idType(IdType.ASSIGN_UUID)
    12. .enableLombok().enableActiveRecord().fileOverride()
    13. .columnNaming(NamingStrategy.underline_to_camel)
    14. .versionColumnName("version").versionPropertyName("version")
    15. .mapperBuilder().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper")
    16. .enableBaseResultMap().enableBaseColumnList().fileOverride()
    17. .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl")
    18. .controllerBuilder().enableRestStyle().formatFileName("%sController")
    19. .build())
    20. .packageConfig(builder -> builder
    21. .parent("com.example.demo")
    22. .entity("pojo").mapper("dao")
    23. .service("service").serviceImpl("service.impl").controller("controller")
    24. .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mappers/"))
    25. //设置mapper文件的自定义路径
    26. .build())
    27. .templateConfig(builder -> builder.entity("/entity/entity.java.vm"))
    28. //使用自定义的实体类模板生成类
    29. .execute();
    30. }
    31. }

                      

    Person:自定义的模板生成的实体类

    1. @Data
    2. @EqualsAndHashCode(callSuper = true)
    3. @TableName("t_person")
    4. @ApiModel(value = "Person对象", description = "")
    5. public class Person extends Model {
    6. private static final long serialVersionUID = 1L;
    7. @TableId(value = "id", type = IdType.AUTO)
    8. private Integer id;
    9. private String name;
    10. private Integer age;
    11. @Override
    12. public Serializable pkVal() {
    13. return this.id;
    14. }
    15. }

                 

                        

  • 相关阅读:
    MySQL数据类型
    最新版的Python写春联,支持行书隶书楷书,不再有缺失汉字
    (delphi11最新学习资料) Object Pascal 学习笔记---第10章第1节(通过引用设置属性)
    离线数仓(1):什么是数据仓库
    CSS 选择器-认识并应用选择器
    docker日常使用命令大全
    微信小程序名称、简称设置规范
    java.lang.Float类下toString(float f)方法具有什么功能呢?
    多元正态分布-参数估计-书后习题回顾总结
    北斗导航 | BDS RTK高精度定位算法在形变检测中的应用(算法原理讲解)
  • 原文地址:https://blog.csdn.net/weixin_43931625/article/details/126657888