First create a springboot project, and is not described in detail here.
Project structure
pom.xml configuration
<?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 Configuration
Since I use the springboot2.1.18 version, after this note is mainly recorded on a schema version 2.x configuration, initialization-mode: always, the configuration of the primary role is to make sql files stored in the automatic insertion classpath database, according to yml file initialization-mode after the mouse to select ctrl click into read-source analysis shows
First, enter the file DataSourceProperties.java
// initializationMode represents values received from the configuration file yml
public void setInitializationMode(DataSourceInitializationMode initializationMode) { this.initializationMode = initializationMode; }
// enumeration value DataSourceInitializationMode class
public enum DataSourceInitializationMode { ALWAYS, EMBEDDED, NEVER; private DataSourceInitializationMode() { } }
Source portion is recorded enumeration values recorded during initialization,
In DataSourceInitializer.java file isEnabled method will be determined based on the three boolean values enumerated
//只有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; }
Look below to create a schema part of the source code
/ ** * The Schema IF the Create Necessary. * @Return { @code to true IF} The Schema Created WAS * @see DataSourceProperties getSchema # () * / public Boolean createSchema () { List <the Resource> = getScripts scripts ( "Spring. datasource.schema ", the this .properties.getSchema ()," Schema " ); iF (! scripts.isEmpty ()) {
// isEnabled method call to determine whether to execute the CLASSPATH sql file, and when the configuration file yml ALAYWS value only take effect, create the database tables will be performed after verification by runScript method 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); }
Source not posted step by step, in short, in future versions 2.x add a lot of rules configuration, when you see the code is executed without being given did not achieve the desired results, this time we should check the relevant configuration is missing.
result: