首先創建一個springboot項目,這里不再過多贅述。
項目結構
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.exampleatguigu</groupId> <artifactId>springboot-06-data-jdbc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-06-data-jdbc</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加JDBC依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 添加mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml配置:springboot所有跟數據交互的項目都是springdata,無論是jdbc、mybatis亦或者是Redis等,所以連接數據庫的配置是spring.datasource開頭的例如username等各種配置值。
官網截圖
由於本人采用的是springboot2.1.18版本,本次筆記主要是記錄一個2.x版本之后一個關於schema配置,initialization-mode: always ,該配置主要的作用是為了使classpath下存放的sql文件自動執行插入數據庫,根據yml文件中initialization-mode鼠標選中ctrl點擊進去之后閱讀源碼分析可知
首先進入DataSourceProperties.java文件
//initializationMode 表示從yml文件中接收的配置值
public void setInitializationMode(DataSourceInitializationMode initializationMode) { this.initializationMode = initializationMode; }
//DataSourceInitializationMode 類的枚舉值
public enum DataSourceInitializationMode { ALWAYS, EMBEDDED, NEVER; private DataSourceInitializationMode() { } }
上部分源碼記錄了在初始化時記錄的枚舉值,
在DataSourceInitializer.java文件中isEnabled方法中會根據上述三個枚舉值來進行boolean判斷
//只有initialization-mode配置值為ALWAYS時才會返回ture
private boolean isEnabled() { DataSourceInitializationMode mode = this.properties.getInitializationMode(); if (mode == DataSourceInitializationMode.NEVER) { return false; } if (mode == DataSourceInitializationMode.EMBEDDED && !isEmbedded()) { return false; } return true; }
下面再看創建schema部分源碼
/** * Create the schema if necessary. * @return {@code true} if the schema was created * @see DataSourceProperties#getSchema() */ public boolean createSchema() { List<Resource> scripts = getScripts("spring.datasource.schema", this.properties.getSchema(), "schema"); if (!scripts.isEmpty()) {
//調用isEnabled方法,來判斷是否執行classpath下的sql文件,且當yml文件中配置值為ALAYWS時才生效,通過校驗后才會執行runScript方法創建數據庫表 if (!isEnabled()) { logger.debug("Initialization disabled (not running DDL scripts)"); return false; } String username = this.properties.getSchemaUsername(); String password = this.properties.getSchemaPassword(); runScripts(scripts, username, password); } return !scripts.isEmpty(); }
private void runScripts(List<Resource> resources, String username, String password) { if (resources.isEmpty()) { return; } ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.setContinueOnError(this.properties.isContinueOnError()); populator.setSeparator(this.properties.getSeparator()); if (this.properties.getSqlScriptEncoding() != null) { populator.setSqlScriptEncoding(this.properties.getSqlScriptEncoding().name()); } for (Resource resource : resources) { populator.addScript(resource); } DataSource dataSource = this.dataSource; if (StringUtils.hasText(username) && StringUtils.hasText(password)) { dataSource = DataSourceBuilder.create(this.properties.getClassLoader()) .driverClassName(this.properties.determineDriverClassName()).url(this.properties.determineUrl()) .username(username).password(password).build(); } DatabasePopulatorUtils.execute(populator, dataSource); }
源碼就不一步步的貼了,總之在2.x以后的版本添加了很多規則配置,當你看到代碼執行沒有報錯卻沒有達到預期的效果,這時候就要檢查相關的配置是否缺少了。
然后啟動主程序,則會在yml配置連接數據庫中根據classpath下department.sql文件自動生成一張表。
結果: