1、scrw(螺丝钉)简介

1.1、特点

  • 简洁、轻量、设计良好
  • 多数据库支持
  • 多种格式文档
  • 灵活扩展
  • 支持自定义模板

1.2、文档生成支持

  • html
  • word
  • markdown

2、SpringBoot中的使用

2.1、导入相关依赖

<!--数据库文档核心依赖-->
 <dependency>
   <groupId>cn.smallbun.screw</groupId>
   <artifactId>screw-core</artifactId>
   <version>1.0.2</version>
 </dependency>
 <!-- HikariCP -->
 <dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP</artifactId>
   <version>3.4.5</version>
 </dependency>
 <!--mysql driver-->
 <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.20</version>
 </dependency>

2.2、业务流程

根据自己数据库地址,以及相关配置,修改相关地方即刻。{上手即用}

@SpringBootTest
 class LandCodingApplicationTests {
     @Test
     void contextLoads() throws SQLException, IOException {
         //todo 修改为自己的库名
         String dbName = "xxxx";
         HikariConfig config = new HikariConfig();
         config.setDriverClassName("com.mysql.cj.jdbc.Driver");
         config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/" + dbName + " ?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8&useInformationSchema=true");
         config.setUsername("你的数据库用户名");
         config.setPassword("你的数据库密码");
         config.addDataSourceProperty("useInformationSchema", "true");
         config.setMinimumIdle(2);
         config.setMaximumPoolSize(5);
         DataSource ds = new HikariDataSource(config);
         //生成文件路径
         String userDir = System.getProperty("user.dir") + "\\src\\test\\java\\com\\pdool\\";
         System.out.println(userDir);
         SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMdd");
         String versionStr = dataFormat.format(new Date());
         //忽略表
         List<String> ignoreTable = new ArrayList<>();
         //忽略表前缀
         List<String> ignorePrefix = new ArrayList<>();
         //忽略表后缀
         List<String> ignoreSuffix = new ArrayList<>();
         ignoreSuffix.add("_test");
         ignoreSuffix.add("test");
 
         for (int i = 0; i < 10; i++) {
             ignoreSuffix.add(String.valueOf(i));
         }
         //生成HTML模板
         createHtml(ds, userDir, versionStr, ignoreTable, ignorePrefix, ignoreSuffix);
         //生成SQL建表语句
         createSql(dbName, ds, userDir, versionStr, ignoreTable, ignorePrefix, ignoreSuffix);
     }
 
     /**
      * 创建html
      *
      * @param dataSource
      * @param userDir
      * @param versionStr
      * @param ignoreTable
      * @param ignorePrefix
      * @param ignoreSuffix
      */
     public static void createHtml(DataSource dataSource, String userDir, String versionStr, List<String> ignoreTable, List<String> ignorePrefix, List<String> ignoreSuffix) {
         //生成配置
         EngineConfig engineConfig = EngineConfig.builder()
                 //生成文件路径
                 .fileOutputDir(userDir)
                 //打开目录
                 .openOutputDir(false)
                 //文件类型
                 .fileType(EngineFileType.HTML)
                 //生成模板实现
                 .produceType(EngineTemplateType.freemarker)
                 .build();
 
         ProcessConfig processConfig = ProcessConfig.builder()
                 //忽略表名
                 .ignoreTableName(ignoreTable)
                 //忽略表前缀
                 .ignoreTablePrefix(ignorePrefix)
                 //忽略表后缀
                 .ignoreTableSuffix(ignoreSuffix)
                 .build();
 
         Configuration config = Configuration.builder()
                 //版本
                 .version(versionStr)
                 //数据库描述
                 .description("数据库文档")
                 //数据源
                 .dataSource(dataSource)
                 //生成配置
                 .engineConfig(engineConfig)
                 //生成配置
                 .produceConfig(processConfig).build();
 
         new DocumentationExecute(config).execute();
     }
 
     /**
      * 生成建表sql
      *
      * @param dbName
      * @param dataSource
      * @param userDir
      * @param versionStr
      * @param ignoreTable
      * @param ignorePrefix
      * @param ignoreSuffix
      * @throws IOException
      * @throws SQLException
      */
     public static void createSql(String dbName, DataSource dataSource, String userDir, String versionStr, List<String> ignoreTable, List<String> ignorePrefix, List<String> ignoreSuffix) throws IOException, SQLException, SQLException {
         Statement tmt = null;
         PreparedStatement pstmt = null;
         List<String> createSqlList = new ArrayList<>();
         String sql = "select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = '" + dbName + "' and TABLE_TYPE = 'BASE TABLE'";
         tmt = dataSource.getConnection().createStatement();
         pstmt = dataSource.getConnection().prepareStatement(sql);
         ResultSet res = tmt.executeQuery(sql);
         while (res.next()) {
             String tableName = res.getString(1);
             if (tableName.contains("`")) {
                 continue;
             }
             if (ignoreTable.contains(tableName)) {
                 continue;
             }
             boolean isContinue = false;
             for (String prefix : ignorePrefix) {
 
                 if (tableName.startsWith(prefix)) {
                     isContinue = true;
                     break;
                 }
             }
             if (isContinue) {
                 continue;
             }
             for (String suffix : ignoreSuffix) {
                 if (tableName.startsWith(suffix)) {
                     isContinue = true;
                     break;
                 }
             }
             if (isContinue) {
                 continue;
             }
             ResultSet rs = pstmt.executeQuery("show create Table `" + tableName + "`");
 
             while (rs.next()) {
                 createSqlList.add("DROP TABLE IF EXISTS '" + tableName + "'");
                 createSqlList.add(rs.getString(2));
             }
         }
 
         String head = "-- 数据库建表语句 \r\n";
         head += "-- db:" + dbName + " version: " + versionStr + "\r\n";
         String collect = String.join(";\r\n", createSqlList);
         collect = head + collect + ";";
         string2file(collect, userDir + dbName + "_" + versionStr + ".sql");
     }
 
     public static void string2file(String collect, String dirStr) throws IOException {
         System.out.println("文件地址  " + dirStr);
         OutputStreamWriter osw = null;
         try {
             osw = new OutputStreamWriter(new FileOutputStream(new File(dirStr)), StandardCharsets.UTF_8);
             osw.write(collect);
             osw.flush();
         } catch (FileNotFoundException e) {
             throw new RuntimeException(e);
         } finally {
             if (osw != null) {
                 osw.close();
             }
         }
     }
 
 
 }

3、数据库文档输出类型改变

在[生成配置中修改]

在 2.2 中查找到生成配置相关字段即可

3.1、生成html

//生成配置
 EngineConfig engineConfig = EngineConfig.builder()
     //生成文件路径
     .fileOutputDir(userDir)
     //打开目录
     .openOutputDir(false)
     //文件类型
     .fileType(EngineFileType.HTML)
     //生成模板实现
     .produceType(EngineTemplateType.freemarker)
     .build();

3.2、生成Word

//生成配置
 EngineConfig engineConfig = EngineConfig.builder()
     //生成文件路径
     .fileOutputDir(userDir)
     //打开目录
     .openOutputDir(false)
     //文件类型
     .fileType(EngineFileType.WORD)
     //生成模板实现
     .produceType(EngineTemplateType.freemarker)
     .build();

## 3.3、生成md

//生成配置
 EngineConfig engineConfig = EngineConfig.builder()
     //生成文件路径
     .fileOutputDir(userDir)
     //打开目录
     .openOutputDir(false)
     //文件类型
     .fileType(EngineFileType.MD)
     //生成模板实现
     .produceType(EngineTemplateType.freemarker)
     .build();