1. 程式人生 > 實用技巧 >資料庫進階五

資料庫進階五

<!-- screw核心 -->
<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.3</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、配置資料來源

配置資料來源,設定useInformationSchema可以獲取tables註釋資訊。

spring.datasource.url=jdbc:mysql://45.93.1.5:3306/fire?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.xa.properties.useInformationSchema=true


3、編寫測試類
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 org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; import javax.sql.DataSource; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @SpringBootTest @RunWith(SpringRunner.class) public class ScrewApplicationTests { @Autowired ApplicationContext applicationContext; @Test public void contextLoads() { DataSource dataSourceMysql = applicationContext.getBean(DataSource.class); // 生成檔案配置 EngineConfig engineConfig = EngineConfig.builder() // 生成檔案路徑,自己mac本地的地址,這裡需要自己更換下路徑 .fileOutputDir("E:/data/doc") // 開啟目錄 .openOutputDir(false) // 檔案型別 .fileType(EngineFileType.HTML) // 生成模板實現 .produceType(EngineTemplateType.freemarker).build(); // 生成文件配置(包含以下自定義版本號、描述等配置連線) Configuration config = Configuration.builder() .version("1.0.3") .description("生成文件資訊描述") .dataSource(dataSourceMysql) .engineConfig(engineConfig) .produceConfig(getProcessConfig()) .build(); // 執行生成 new DocumentationExecute (config).execute(); } /** * 配置想要生成的表+ 配置想要忽略的表 * * @return 生成表配置 */ public static ProcessConfig getProcessConfig() { // 忽略表名 List<String> ignoreTableName = Arrays.asList("a", "test_group"); // 忽略表字首,如忽略a開頭的資料庫表 List<String> ignorePrefix = Arrays.asList("a", "t"); // 忽略表字尾 List<String> ignoreSuffix = Arrays.asList("_test", "czb_"); return ProcessConfig.builder() //根據名稱指定表生成 .designatedTableName(Arrays.asList("fire_user")) //根據表字首生成 .designatedTablePrefix(new ArrayList<> ()) //根據表字尾生成 .designatedTableSuffix(new ArrayList<>()) //忽略表名 .ignoreTableName(ignoreTableName) //忽略表字首 .ignoreTablePrefix(ignorePrefix) //忽略表字尾 .ignoreTableSuffix(ignoreSuffix).build(); } }
View Code