无聊的时候写的,很简单,娱乐而已。
基于 Mybatis-plus 的代码生成器加上 GUI 实现,可以可视化的生成代码,方便大家再编写项目时生成一些鸡肋,直接将生成的鸡肋导入项目即可。
效果展示GUI 界面
生成的鸡肋
实战
导入依赖
导入用到的依赖。这里自动生成 Swagger 注释 API,Lombok 注解。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.5.5</version></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.4.3.4</version></dependency><!--mybatis-plus代码生成器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version><scope>provided</scope></dependency><!--freemarker模板--><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version><scope>compile</scope></dependency><!--swagger--><dependency><groupId>io.swagger</groupId><artifactId>swagger-annotations</artifactId><version>1.6.3</version></dependency>
GUI 布局
使用 IDEA 进行最基础的布局。
代码生成工具类
对生成策略进行一些简单的配置。
MyBatis-Plus 代码生成器详细讲解的博客:MyBatis-Plus——代码生成器(3.5.1+版本)_ZOOM_Jie -CSDN博客
/*** 代码生成工具类*/publicclassGenerateCodeUtils { /* 判断参数是否为空,代码生成 */publicstaticvoidisTrue(StringsqlUrl, StringsqlUserName, StringsqlPassword, StringoutPutDir, StringxmlOutPutDir, StringsqlTable, Stringauthor, StringpackageName, StringmodelName) throwsException { if (isNotNull(sqlUrl) &&isNotNull(sqlUserName) &&isNotNull(sqlPassword) &&isNotNull(sqlTable) &&isNotNull(packageName) &&isNotNull(modelName)) { GenerateCode(sqlUrl, sqlUserName, sqlPassword, outPutDir, xmlOutPutDir, sqlTable, author, packageName, modelName); } else { thrownewException("字段为空"); } } /* 判断参数是否为空 */publicstaticbooleanisNotNull(Stringstr) { return!"".equals(str) &&str!=null; } // 处理 all 情况protectedstaticList<String>getTables(Stringtables) { return"all".equals(tables) ?Collections.emptyList() : Arrays.asList(tables.split(",")); } /* 调用 Mybatis-plus 的代码生成器 */publicstaticvoidGenerateCode(StringsqlUrl, StringsqlUserName, StringsqlPassword, StringoutPutDir, StringxmlOutPutDir, StringsqlTable, Stringauthor, StringpackageName, StringmodelName) { FastAutoGenerator.create(sqlUrl, sqlUserName, sqlPassword) // 全局配置 .globalConfig((scanner, builder) ->builder.author(author) .outputDir(outPutDir) .commentDate("yyyy-MM-dd hh:mm:ss") .dateType(DateType.TIME_PACK) .enableSwagger() .fileOverride() .enableSwagger() ) // 包配置 .packageConfig((scanner, builder) ->builder.parent(packageName) .moduleName(modelName) .entity("pojo") .service("service") .serviceImpl("serviceImpl") .mapper("mapper") .xml("mapper") .other("utils") .pathInfo(Collections.singletonMap(OutputFile.mapperXml, xmlOutPutDir)) ) // 策略配置 .strategyConfig((scanner, builder) -> { builder.addInclude(getTables(sqlTable)) .serviceBuilder() .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl") .entityBuilder() .enableLombok() .disableSerialVersionUID() .logicDeleteColumnName("deleted") .naming(NamingStrategy.underline_to_camel) .columnNaming(NamingStrategy.underline_to_camel) .addTableFills(newColumn("create_time", FieldFill.INSERT), newColumn("modify_time", FieldFill.INSERT_UPDATE)) .enableTableFieldAnnotation() .controllerBuilder() .formatFileName("%sController") .enableRestStyle() .mapperBuilder() .superClass(BaseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sMapper"); }) .templateEngine(newFreemarkerTemplateEngine()) .execute(); } }
GUI 窗口类(部分代码)
GUI 组件的布局,添加一些监听器。
/** GUI 窗口类*/publicclasscodeGernalextendsJFrame { publicstaticvoidmain(String[] args) { JFrameframe=newJFrame("codeGernal"); frame.setContentPane(newcodeGernal().AutoGenerateCode); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setResizable(false); frame.setVisible(true); } publicvoidGenerate() { StringsqlUrl=SQLURL.getText(); StringsqlUserName=SQLUserName.getText(); StringsqlPassword=SQLPassword.getText(); StringoutDir=System.getProperty("user.dir") +"\\代码生成器"; StringoutPutDir=System.getProperty("user.dir") +"\\代码生成器\\src\\main\\java"; StringxmlOutPutDir=System.getProperty("user.dir") +"\\代码生成器\\src\\main\\resources\\"; StringsqlTable=SQLTable.getText(); Stringauthor=Author.getText(); StringpackageName=PackageName.getText(); StringmodelName=ModelName.getText(); /* 获取输入框的值,调用 GenerateCodeUtils 的方法 */try { GenerateCodeUtils.isTrue(sqlUrl, sqlUserName, sqlPassword, outPutDir, xmlOutPutDir, sqlTable, author, packageName, modelName); OutPutDir.setText(outDir); TipsLabel.setText("代码生成成功"); } catch (Exceptionexception) { TipsLabel.setText(exception.getMessage()); } } publiccodeGernal() { /* GenerateButton 鼠标单击事件 */GenerateButton.addMouseListener(newMouseAdapter() { publicvoidmouseClicked(MouseEvente) { Generate(); } }); /* ResetButton 鼠标单击事件 */ResetButton.addMouseListener(newMouseAdapter() { publicvoidmouseClicked(MouseEvente) { SQLURL.setText("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8"); SQLUserName.setText(""); SQLPassword.setText(""); SQLTable.setText("Enter table names separated by English commas or enter \"all\""); Author.setText(""); PackageName.setText(""); ModelName.setText(""); OutPutDir.setText("Auto generate code path"); TipsLabel.setText(""); } }); /* SQLTable 获得失去焦点事件 */SQLTable.addFocusListener(newFocusAdapter() { publicvoidfocusGained(FocusEvente) { if ("Enter table names separated by English commas or enter \"all\"".equals(SQLTable.getText())) { SQLTable.setText(""); } } }); SQLTable.addFocusListener(newFocusAdapter() { publicvoidfocusLost(FocusEvente) { if ("".equals(SQLTable.getText())) { SQLTable.setText("Enter table names separated by English commas or enter \"all\""); } } }); } }
源码
源码+jar包+EXE可执行文件下载地址:JavaWeb鸡肋代码生成器.zip-Java文档类资源-CSDN文库
扩展
打包成 .jar 包
IDEA 将普通 Java 项目打包成 Jar 包并运行_ZOOM_Jie -CSDN博客
打包成.exe可执行文件
使用 exe4j 将 jar 包生成 .exe 文件(敲详细)_ZOOM_Jie -CSDN博客
打包成安装包