摘要:情況是這樣的原本的框架的模板是不支持的注解的,需要手動寫。自己折騰了個多小時,建立在的基礎上進行修改。可以選擇生成文件時,不覆蓋某個目錄下的文件,并支持生成注解。
情況是這樣的:
原本mybatis-plus的框架的模板是不支持swagger的注解的,需要手動寫。
自己折騰了1個多小時,建立在mybatis-plus的基礎上進行修改??梢赃x擇生成文件時,不覆蓋某個目錄下的文件,并支持生成swagger注解。這里把代碼貼出來,主要是為了方便以后的兄弟們?。?!
需要聲明的是,本人的是maven工程,springBoot項目!
工程目錄:
GeneratorCode
package com.asita.renovation; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.io.File; import java.nio.file.Paths; public class GeneratorCode { private static String packageName = "com.asita.renovation"; private static String outDir = "F:java_web enovationsrcmainjava"; private static String entity = "entity"; private static String mapper = "mapper"; private static String service = "service"; private static String impl = "service.impl"; private static String controller = "controller"; private static String xml = "mapper.xml"; private static boolean isOverEntity = true; private static boolean isOverController = false; private static boolean isOverService = false; private static boolean isOverServiceImpl = false; private static boolean isOverMapper = false; private static boolean isOverXml = false; private static String entityVM = "/templates/entity.vm"; private static String controllerVM = "/templates/controller.vm"; private static String serviceVM = ""; private static String serviceImplVM = ""; private static String mapperVM = ""; private static String xmlVM = ""; private static String [] baseDir = {entity, mapper, service, impl, controller}; public static void main(String[] args) { //user -> UserService, 設置成true: user -> IUserService boolean serviceNameStartWithI = true; generateByTables(serviceNameStartWithI, packageName, "bedroom_ratio", "boq", "common_project", "construction_detail", "construction_detail_item","construction_unit", "loan_template", "main_material_list", "manager", "material_list", "material_suppiler_template", "membership_fee", "room_area_formula", "room_area_formula_template", "toll_switch", "user", "work_template"); } private static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) { GlobalConfig config = new GlobalConfig(); String dbUrl = "jdbc:mysql://localhost:3306/renovation?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=true"; DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL) .setUrl(dbUrl) .setUsername("root") .setPassword("#") .setDriverName("com.mysql.cj.jdbc.Driver"); StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig .setCapitalMode(false) .setEntityLombokModel(true) .setDbColumnUnderline(true) .setRestControllerStyle(true) .entityTableFieldAnnotationEnable(false) .setNaming(NamingStrategy.underline_to_camel) //修改替換成你需要的表名,多個表名傳數組 .setInclude(tableNames); config.setActiveRecord(true) .setAuthor("陳少平") .setOutputDir(outDir) .setBaseResultMap(true) .setBaseColumnList(true) .setFileOverride(true) .setEnableCache(false) // XML ResultMap .setBaseResultMap(true) // XML columList; .setBaseColumnList(true); if (!serviceNameStartWithI) { config.setServiceName("%sService"); } PackageConfig pcf = initPackage(); TemplateConfig tc = initTemplateConfig(packageName); new AutoGenerator().setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo(pcf) .setTemplate(tc) .execute(); } /** * 根據自己的需要,修改哪些包下面的 要覆蓋還是不覆蓋 * @param packageName */ private static TemplateConfig initTemplateConfig(String packageName) { TemplateConfig tc = new TemplateConfig(); for(String tmp : baseDir) { initVM(tc); File file = new File(Paths.get(outDir, String.join("/", packageName.split(".")), tmp).toString()); String[] list = file.list(); if(list != null && list.length > 0) { if(!isOverController) { tc.setController(null); } if(!isOverService) { tc.setService(null); } if(!isOverServiceImpl) { tc.setServiceImpl(null); } if(!isOverEntity) { tc.setEntity(null); } if(!isOverMapper) { tc.setEntity(null); } if(!isOverXml) { tc.setXml(null); } } } return tc; } private static void initVM(TemplateConfig tc) { if(stringIsNotNull(entityVM)) { tc.setEntity(entityVM); } if(stringIsNotNull(mapperVM)) { tc.setMapper(mapperVM); } if(stringIsNotNull(serviceImplVM)) { tc.setServiceImpl(serviceImplVM); } if(stringIsNotNull(serviceVM)) { tc.setService(serviceVM); } if(stringIsNotNull(xmlVM)) { tc.setXml(xmlVM); } if(stringIsNotNull(controllerVM)) { tc.setController(controllerVM); } } /** * 簡單判斷字符串是不是為空 * @param s * @return */ private static boolean stringIsNotNull(String s) { if(null != s && !s.equals("") && s.length() > 0 && s.trim().length() > 0) { return true; } return false; **加粗文字** } /** * 初始化包目錄配置 * @return */ private static PackageConfig initPackage() { PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(packageName); packageConfig.setService(service); packageConfig.setServiceImpl(impl); packageConfig.setController(controller); packageConfig.setEntity(entity); packageConfig.setXml(xml); return packageConfig; } }
/templates/controller.vm
package ${package.Controller}; import org.springframework.web.bind.annotation.RequestMapping; import io.swagger.annotations.Api; #if(${restControllerStyle}) import org.springframework.web.bind.annotation.RestController; #else import org.springframework.stereotype.Controller; #end #if(${superControllerClassPackage}) import ${superControllerClassPackage}; #end /** * @author ${author} * @since ${date} */ @Api(tags = {"$!{table.comment}"}) #if(${restControllerStyle}) @RestController #else @Controller #end @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end") #if(${kotlin}) class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end #else #if(${superControllerClass}) public class ${table.controllerName} extends ${superControllerClass} { #else public class ${table.controllerName} { #end } #end
/templates/entity.vm
package ${package.Entity}; #foreach($pkg in ${table.importPackages}) import ${pkg}; #end #if(${entityLombokModel}) import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; #end /** * @author ${author} * @since ${date} */ @ApiModel(value ="$!{table.comment}") #if(${entityLombokModel}) @Data #if(${superEntityClass}) @EqualsAndHashCode(callSuper = true) #else @EqualsAndHashCode(callSuper = false) #end @Accessors(chain = true) #end #if(${table.convert}) @TableName("${table.name}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} extends Model<${entity}> { #else public class ${entity} implements Serializable { #end private static final long serialVersionUID = 1L; ## ---------- BEGIN 字段循環遍歷 ---------- #foreach($field in ${table.fields}) #if(${field.keyFlag}) #set($keyPropertyName=${field.propertyName}) #end #if("$!field.comment" != "") @ApiModelProperty(value = "${field.comment}") #end #if(${field.keyFlag}) ## 主鍵 #if(${field.keyIdentityFlag}) @TableId(value = "${field.name}", type = IdType.AUTO) #elseif(!$null.isNull(${idType}) && "$!idType" != "") @TableId(value = "${field.name}", type = IdType.${idType}) #elseif(${field.convert}) @TableId("${field.name}") #end ## 普通字段 #elseif(${field.fill}) ## ----- 存在字段填充設置 ----- #if(${field.convert}) @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) #else @TableField(fill = FieldFill.${field.fill}) #end #elseif(${field.convert}) @TableField("${field.name}") #end ## 樂觀鎖注解 #if(${versionFieldName}==${field.name}) @Version #end ## 邏輯刪除注解 #if(${logicDeleteFieldName}==${field.name}) @TableLogic #end private ${field.propertyType} ${field.propertyName}; #end ## ---------- END 字段循環遍歷 ---------- #if(!${entityLombokModel}) #foreach($field in ${table.fields}) #if(${field.propertyType.equals("boolean")}) #set($getprefix="is") #else #set($getprefix="get") #end public ${field.propertyType} ${getprefix}${field.capitalName}() { return ${field.propertyName}; } #if(${entityBuilderModel}) public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #else public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { #end this.${field.propertyName} = ${field.propertyName}; #if(${entityBuilderModel}) return this; #end } #end #end #if(${entityColumnConstant}) #foreach($field in ${table.fields}) public static final String ${field.name.toUpperCase()} = "${field.name}"; #end #end #if(${activeRecord}) @Override protected Serializable pkVal() { #if(${keyPropertyName}) return this.${keyPropertyName}; #else return null; #end } #end #if(!${entityLombokModel}) @Override public String toString() { return "${entity}{" + #foreach($field in ${table.fields}) #if($!{velocityCount}==1) "${field.propertyName}=" + ${field.propertyName} + #else ", ${field.propertyName}=" + ${field.propertyName} + #end #end "}"; } #end }
附上maven的配置,以及springBoot 的配置
com.baomidou mybatisplus-spring-boot-starter 1.0.5 com.baomidou mybatis-plus 2.3 org.apache.velocity velocity-engine-core 2.0 io.springfox springfox-swagger2 2.8.0 io.springfox springfox-swagger-ui 2.8.0
mybatis-plus: mapper-locations: classpath:/com/asita/renovation/mapper/xml/*Mapper.xml typeAliasesPackage: com.asita.renovation.entity global-config: #主鍵類型 0:"數據庫ID自增", 1:"用戶輸入ID",2:"全局唯一ID (數字類型唯一ID)", 3:"全局唯一ID UUID"; id-type: 0 #字段策略 0:"忽略判斷",1:"非 NULL 判斷"),2:"非空判斷" field-strategy: 2 #刷新mapper 調試神器 refresh-mapper: true #駝峰下劃線轉換 # db-column-underline: true # Sequence序列接口實現類配置 key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator configuration: map-underscore-to-camel-case: true cache-enabled: false
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/76602.html
摘要:打開,,選中,然后再選中,輸入項目的和,指定等配置,修改,打開項目,添加一些必要的目錄,最終項目框架目錄圖如下修改文件,指定各依賴和插件的版本等信息在標簽里面管理各依賴的版本號添加項目依賴管理依賴配置好之后,開始整合。 最近在回顧和總結一些技術,想到了把之前比較火的 SSM 框架重新搭建出來,作為一個小結,同時也希望本文章寫出來能對大家有一些幫助和啟發,因本人水平有限,難免可能會有一些...
摘要:代碼生成在基礎上,新增等模板拓展,生成通用分頁方法代碼生成模板數據庫新建表,即可生成后臺分頁基礎代碼,還有官網地址代碼生成步驟創建數據庫表,例如注意記得加上表注釋,字段列注釋,方便生成類注釋注釋 代碼生成 Generator spring-boot-plus在mybatis-plus基礎上,新增param/vo等模板拓展controller/service/mapper/xml,生成通...
摘要:代碼生成代碼生成內容在基礎上,新增等模板拓展,生成通用分頁方法代碼生成模板數據庫新建表,即可生成后臺分頁基礎代碼,還有官網地址代碼生成步驟創建數據庫表,例如注意記得加上表注釋,字段列注釋,方便生成類注釋注釋 spring-boot-plus 代碼生成 Generator 代碼生成內容 spring-boot-plus在mybatis-plus基礎上,新增param/vo等模板 拓展c...
摘要:最近在研究,順便就會看看數據庫連接這一塊的知識,所以當我發現有通用和這兩款網絡上比較火的簡化開發的優秀軟件之后。先創建一個的項目,可以參考我之前的文章的簡單教程一項目的創建。打開文件,將最新的相關的包都引用進來。 最近在研究springboot,順便就會看看數據庫連接這一塊的知識 ,所以當我發現有通用Mapper和MybatisPlus這兩款網絡上比較火的簡化mybatis開發的優秀軟...
閱讀 878·2021-10-13 09:39
閱讀 3531·2021-09-26 10:16
閱讀 2861·2019-08-30 15:54
閱讀 1037·2019-08-30 14:22
閱讀 2886·2019-08-29 15:39
閱讀 3253·2019-08-27 10:52
閱讀 809·2019-08-26 13:59
閱讀 1703·2019-08-26 12:20