摘录地址:https://www.cnblogs.com/gao241/p/3480472.html
H2是一个开源的嵌入式引擎,采用java语言编写,不受平台的限制,同样H2提供了一个十分便利的web控制台用于操作和管理内容。H2还提供兼容模式,可以兼容一些主流的,所以采用H2作为开发期的非常便捷。
一、引入Maven依赖
在maven中定义H2的版本特性
<properties> <h2.version>1.3.172</h2.version> </properties>
添加H2依赖
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2.version}</version> <scope>test</scope> </dependency>
只在硬盘中运行,关掉连接后将被清空,适于测试环境
连接字符串:
jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
因为不指定DBName,则以私有形式启动,只准许一个连接
2、嵌入式
持久化存储为单个文件
连接字符串:
jdbc:h2:file:~/.h2/DBName;AUTO_SERVER=TRUE
~/.h2/DBName表示文件的储存位置,所以第一次连接则会自动创建
3、服务模式
H2支持三种服务模式:
启动tcp服务相连字符串示例:
jdbc:h2:tcp://localhost/~/test使用用户主目录
jdbc:h2:tcp://localhost//data/test使用绝对路径
4、打通字符串参数5、启动服务模式,打开H2 Console web网页
启动服务h2 内存,在命令行中执行
java -cp h2*.jar org.h2.tools.Server
执行如下命令,获取选项页面及默认值
java -cp h2*.jar org.h2.tools.Server -?
常用的选项如下:
另外,使用maven也可以启动H2服务
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>1.0.0</modelVersion> <version>1.0.0</version> <groupId>groupid</groupId> <artifactId>h2-console</artifactId> <name>H2 Console</name> <packaging>pom</packaging> <properties> <h2.version>1.3.172</h2.version> </properties> <dependencies> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2.version}</version> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>org.h2.tools.Server</mainClass> <arguments> <argument>-web</argument> <argument>-webPort</argument> <argument>8090</argument> <argument>-browser</argument> </arguments> </configuration> </plugin> </plugins> </build> </project>
在命令行中执行如下命令启动H2 Console
mvn exec:java
如果建立一个bat文件
@echo off
call mvn exec:java
pause
此操作相当于执行了如下命令:
java -jar h2-1.3.168.jar -web -webPort 8090 -browser
1、Properties配置
java应用程序关于的Properties配置文档示例如下:
#h2 database settings jdbc.driver=org.h2.Driver jdbc.url=jdbc:h2:file:~/.h2/quickstart;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1 jdbc.username=sa jdbc.password= #connection pool settings jdbc.pool.maxIdle=5 jdbc.pool.maxActive=40
2、初始化
(1)、在Maven中初始化
可以创建一个Profile,专门用来初始化。在maven中可以通过maven-antrun-plugin执行ant任务,在ant任务中使用sql标签可以执行sql脚本文档,顶配实例如下:
<profile> <id>refresh-db</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <configuration> <target> <property file="src/main/resources/application.properties" /> <sql driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}" password="${jdbc.password}" onerror="continue" encoding="${project.build.sourceEncoding}"> <classpath refid="maven.test.classpath" /> <transaction src="src/main/resources/sql/h2/schema.sql"/> <transaction src="src/test/resources/data/h2/import-data.sql"/> </sql> </target> </configuration> </plugin> </plugins> </build> </profile>
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-118453-1.html
照他的观点
试试DF21和DF26的实战
指同盟国)