开发完之后,就一大堆文档需要写的,其中就有库表设计文档,所以直接写个接口,直接把库表转为库表设计文档就行,省一大堆时间摸鱼了。直接贴代码。
pom文件引入依赖
<!--引入数据库表结构文档生成器Screw依赖--> <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.5</version> </dependency> |
新建controller类
import cn.smallbun.screw.core.Configuration; import cn.smallbun.screw.core.engine.EngineConfig; import cn.smallbun.screw.core.engine.EngineFileType; import cn.smallbun.screw.core.engine.EngineTemplateType; import cn.smallbun.screw.core.execute.DocumentationExecute; import cn.smallbun.screw.core.process.ProcessConfig; import com.google.common.base.Splitter; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.io.InputStreamResource; import org.springframework.http.ContentDisposition; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.sql.DataSource; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * 数据库表转为库表设计文档,将数据库内的表生成数据库设计文档格式 * @author lfq */ @RestController @RequestMapping("/table") public class TableToTableDocument { /** * screw配置的文件类型 HTML->HTML文件 WORD->WORD文件 MD->Markdown文件 * 默认生成word文档 */ private static final EngineFileType FILE_OUTPUT_TYPE = EngineFileType.WORD; /** 文档输出地址 */ private static final String fileOutputDir = "/data/tabledocument"; /** 文档名称*/ private static final String docFileName = "tabledocument"; @Autowired private ApplicationContext applicationContext; /** * 生成数据库表结构文档 */ /** * * @param driverClassName MySQL驱动 * @param dbUrl 数据库URL characterEncoding=UTF-8: 防止生成后文档乱码 * @param dbUsername MySQL数据库账号 * @param dbPassword MySQL数据库密码 * @param ignoreTableName 忽略的表名,不需要生成的表,多个以英文逗号隔开 * @param ignoreTablePrefix 忽略的表名前缀,多个以英文逗号隔开 * @param generatorTableName 需要生成的表名,多个以英文逗号隔开 * @param generatorTablePrefix 需要生成的表前缀,多个以英文逗号隔开 */ @GetMapping("/toTableDocument") public ResponseEntity<InputStreamResource> toTableDocument(String driverClassName, String dbUrl, String dbUsername, String dbPassword, String ignoreTableName, String ignoreTablePrefix, String generatorTableName, String generatorTablePrefix){ DataSource dataSource; if (StringUtils.isNotBlank(driverClassName) && StringUtils.isNotBlank(dbUrl) && StringUtils.isNotBlank(dbUsername) && StringUtils.isNotBlank(dbPassword)){ HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setDriverClassName(driverClassName); hikariConfig.setJdbcUrl(dbUrl); hikariConfig.setUsername(dbUsername); hikariConfig.setPassword(dbPassword); hikariConfig.addDataSourceProperty("useInformationSchema", "true"); hikariConfig.setMinimumIdle(2); hikariConfig.setMaximumPoolSize(5); dataSource = new HikariDataSource(hikariConfig); }else { //使用yml配置的数据源 dataSource = applicationContext.getBean(DataSource.class); } //创建screw的引擎配置 EngineConfig engineConfig = EngineConfig.builder() .fileOutputDir(fileOutputDir) .openOutputDir(false) .fileType(FILE_OUTPUT_TYPE) .produceType(EngineTemplateType.freemarker) .fileName(docFileName).build(); //创建screw的配置:生成文档配置(包含以下自定义版本号、描述等配置连接) Configuration config = Configuration.builder() .version("1.0.0") .description("数据库设计文档生成") .dataSource(dataSource) .engineConfig(engineConfig) .build(); if (StringUtils.isNotBlank(ignoreTableName) || StringUtils.isNotBlank(ignoreTablePrefix) || StringUtils.isNotBlank(generatorTableName) || StringUtils.isNotBlank(generatorTablePrefix)) { config.setProduceConfig(getProcessConfig(ignoreTableName, ignoreTablePrefix, generatorTableName, generatorTablePrefix)); } new DocumentationExecute(config).execute(); String filename = docFileName + ".doc"; File file = new File(fileOutputDir + "/" + filename); if (file.exists()) { try { InputStream inputStream = new FileInputStream(file); if (inputStream != null) { InputStreamResource inputStreamResource = new InputStreamResource(inputStream); HttpHeaders httpHeaders = new HttpHeaders(); ContentDisposition contentDisposition = ContentDisposition.builder("attachment") .filename(new String(filename.getBytes("utf-8"), "iso-8859-1")).build(); httpHeaders.setContentDisposition(contentDisposition); return ResponseEntity.ok().headers(httpHeaders).contentType(MediaType.APPLICATION_OCTET_STREAM) .body(inputStreamResource); } } catch (Exception e) {} } return null; } /** * 配置想要生成的表、想要忽略的表 * @return */ private ProcessConfig getProcessConfig(String ignoreTableName, String ignoreTablePrefix, String generatorTableName, String generatorTablePrefix) { List<String> ignoreTableNameList = new ArrayList<>(); List<String> ignoreTablePrefixList = new ArrayList<>(); List<String> generatorTableNameList = new ArrayList<>(); List<String> generatorTablePrefixList = new ArrayList<>(); //忽略表 if (StringUtils.isNotBlank(ignoreTableName)){ ignoreTableNameList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(ignoreTableName); } //忽略表前缀,如忽略test_开头的数据库表 if (StringUtils.isNotBlank(ignoreTablePrefix)){ ignoreTablePrefixList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(ignoreTablePrefix); } // 需要生成数据库文档的表 如果generatorTablePrefix设置值不为空,则 generatorTableName 中的表名要去掉前缀,不然会重复生成,并且表名和字段注释有误 if (StringUtils.isNotBlank(generatorTableName)){ generatorTableNameList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(generatorTableName); } // 需要生成数据库文档的表前缀 if (StringUtils.isNotBlank(generatorTablePrefix)){ generatorTablePrefixList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(generatorTablePrefix); } //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时,将生成指定表,其余表不生成、并跳过忽略表配置 ProcessConfig processConfig = ProcessConfig.builder() //根据名称指定表生成 .designatedTableName(generatorTableNameList) //根据表前缀生成 .designatedTablePrefix(generatorTablePrefixList) //忽略表名 .ignoreTableName(ignoreTableNameList) //忽略表前缀 .ignoreTablePrefix(ignoreTablePrefixList) .build(); return processConfig; } } |
生成文档效果: