<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<version>2.1.214version>
dependency>
SpringBoot版本是2.3.12.RELEASE,下面有些配置看SpringBoot版本的,比如spring.datasource.schema是这个版本以下的,spring.sql.init.data是更高版本的
./dbfile 是把持久文件生成到程序启动路径下,你也可以自定义其他路径
# 数据源配置
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.schema=classpath:db/h2-schema.sql
spring.datasource.url=jdbc:h2:file:./dbfile
spring.datasource.username=root
spring.datasource.password=root
# 下面是控制台的配置
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
先看以下两个常量,代表url开头与后半段,后半段的类型有mem内存模式、file本地文件模式、tcp|ssl远程模式,含义如下(参考http://www.h2database.com/html/features.html#embedded_databases)
| Topic | URL Format and Examples |
|---|---|
| Embedded (local) connection | jdbc:h2:[file:][] jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only) |
| In-memory (private) | jdbc:h2:mem: |
| In-memory (named) | jdbc:h2:mem: jdbc:h2:mem:test_mem |
| Server mode (remote connections) using TCP/IP | jdbc:h2:tcp://[:]/[
|
| Server mode (remote connections) using TLS | jdbc:h2:ssl://[:]/[
|
org.h2.engine.Constants
/**
* The database URL prefix of this database.
*/
public static final String START_URL = "jdbc:h2:";
/**
* The database URL format in simplified Backus-Naur form.
*/
public static final String URL_FORMAT = START_URL +
"{ {.|mem:}[name] | [file:]fileName | " +
"{tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]";
去驱动里查看,ConnectionInfo里把url截取,然后判断类型,当是file时,把持久化标识persistent标位了true,后续数据库启动和运行都会用到这个标识判断是否持久化
org.h2.engine.ConnectionInfo
/**
* Create a connection info object.
*
* @param u the database URL (must start with jdbc:h2:)
* @param info the connection properties or {@code null}
* @param user the user name or {@code null}
* @param password
* the password as {@code String} or {@code char[]}, or
* {@code null}
*/
public ConnectionInfo(String u, Properties info, String user, Object password) {
u = remapURL(u);
originalURL = url = u;
if (!u.startsWith(Constants.START_URL)) {
throw getFormatException();
}
if (info != null) {
readProperties(info);
}
if (user != null) {
prop.put("USER", user);
}
if (password != null) {
prop.put("PASSWORD", password);
}
readSettingsFromURL();
Object timeZoneName = prop.remove("TIME ZONE");
if (timeZoneName != null) {
timeZone = TimeZoneProvider.ofId(timeZoneName.toString());
}
setUserName(removeProperty("USER", ""));
name = url.substring(Constants.START_URL.length());
parseName();
convertPasswords();
String recoverTest = removeProperty("RECOVER_TEST", null);
if (recoverTest != null) {
FilePathRec.register();
try {
Utils.callStaticMethod("org.h2.store.RecoverTester.init", recoverTest);
} catch (Exception e) {
throw DbException.convert(e);
}
name = "rec:" + name;
}
}
private void parseName() {
if (".".equals(name)) {
name = "mem:";
}
if (name.startsWith("tcp:")) {
remote = true;
name = name.substring("tcp:".length());
} else if (name.startsWith("ssl:")) {
remote = true;
ssl = true;
name = name.substring("ssl:".length());
} else if (name.startsWith("mem:")) {
persistent = false;
if ("mem:".equals(name)) {
unnamed = true;
}
} else if (name.startsWith("file:")) {
name = name.substring("file:".length());
persistent = true;
} else {
persistent = true;
}
if (persistent && !remote) {
name = IOUtils.nameSeparatorsToNative(name);
}
}