1 screw(螺丝钉)介绍
1.2 特点
- 简洁、轻量、设计良好
- 多数据库支持
- 多种格式文档
- 灵活扩展
- 支持自定义模板
1.2 文档生成支持
1.3 文档截图
1.3.1 html
1.3.2 word
1.3.3 markdwon
2 SpringBoot使用
2.1 依赖
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
2.2 测试
package com.example.adminlayui;
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.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SpringBootTest
class ScrewApplicationTests {
@Test
void contextLoads() throws SQLException, IOException {
String dbName = "admin2";
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("root");
config.setPassword("123456");
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));
}
createHtml(ds, userDir, versionStr, ignoreTable, ignorePrefix, ignoreSuffix);
createSql(dbName, ds, userDir, versionStr, ignoreTable, ignorePrefix, 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();
}
public static void createSql(String dbName, DataSource dataSource, String userDir, String versionStr, List<String> ignoreTable, List<String> ignorePrefix, List<String> ignoreSuffix) throws IOException, 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();
} finally {
if (osw != null) {
osw.close();
}
}
}
}
2.3 结果
3 生成类型
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 生成Markdwon
EngineConfig engineConfig = EngineConfig.builder()
.fileOutputDir(userDir)
.openOutputDir(false)
.fileType(EngineFileType.MD)
.produceType(EngineTemplateType.freemarker)
.build();
4 扩展pojo生成
4.1 依赖
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-extension</artifactId>
<version>${lastVersion}</version>
</dependency>
4.2 代码
void pojoGeneration() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/screw");
hikariConfig.setUsername("screw");
hikariConfig.setPassword("screw");
hikariConfig.addDataSourceProperty("useInformationSchema", "true");
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
DataSource dataSource = new HikariDataSource(hikariConfig);
ProcessConfig processConfig = ProcessConfig.builder()
.designatedTableName(new ArrayList<>())
.designatedTablePrefix(new ArrayList<>())
.designatedTableSuffix(new ArrayList<>()).build();
PojoConfiguration config = new PojoConfiguration();
config.setPath("/cn/smallbun/screw/");
config.setPackageName("cn.smallbun.screw");
config.setUseLombok(false);
config.setDataSource(dataSource);
config.setNameStrategy(new HumpNameStrategy());
config.setProcessConfig(processConfig);
new PojoExecute(config).execute();
}
|