内嵌模式,就是把在应用中引入H2,启动应用的同时,会把H2数据服务也启动,应用中既包含了H2数据库的服务端,同时应用又作为客户端来连接H2数据库
使用springBoot做一个内嵌式H2的栗子
先创建一个springBoot的项目
H2的依赖:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
在配置文件中配置开启H2数据的控制台显示
spring.datasource.platform=h2
spring.h2.console.enabled=true
logging.level.root=INFO
默认密码是sa,可以在控制台进行数据库的操作
使用建表语句创建一个信息info表
CREATE TABLE info
(id bigint,
name varchar);
H2内存模式连接配置
内存模式就是数据库文件存在于内存中,没有持久化,当应用进程关闭时数据库与数据表会消失,配置文件如下:
spring.datasource.platform=h2
spring.h2.console.enabled=true
logging.level.root=INFO
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:hello
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
做了上面的建表操作
当重启我们的应用时,数据没有持久化,消失了
H2嵌入模式连接配置
嵌入模式就是数据库文件存在于应用当前的硬盘内,进行了持久化,当应用进程关闭时数据库与数据表不会消失,配置文件如下:
spring.datasource.platform=h2
spring.h2.console.enabled=true
logging.level.root=INFO
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:F:/H2/hello
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
做了上面同样的建表操作、并且添加数据,应用重启后,数据仍然还在
说明H2数据库进行了持久化,应用重启后,数据还在,按照url上面的路劲jdbc:h2:file:F:/H2/hello去查询了一下,果然在我的电脑上生成了持久化的文件
利用spring data连接H2数据库
编写实体类
package com.ftd.h2.entity;
import javax.persistence.*;
import java.io.Serializable;
* @author ZhaoWeinan
@Entity
public class Info implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
public Long getId() {
return id;
public void setId(Long id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
@Override
public String toString() {
return "Info{" +
"id=" + id +
", name='" + name + '\'' +
'}';
编写Repository类
package com.ftd.h2.mapper;
import com.ftd.h2.entity.Info;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
* @author ZhaoWeinan
* @date 2018/3/14
@Repository
public interface InfoRepository extends JpaRepository<Info,Long> {
编写controller类
package com.ftd.h2.controlloer;
import com.ftd.h2.entity.Info;
import com.ftd.h2.mapper.InfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
* @author ZhaoWeinan
* @date 2018/3/14
@RestController
public class InfoController {
@Autowired
private InfoRepository repository;
@RequestMapping(value = "info/{id}",method = RequestMethod.GET)
public Info getInfoById(@PathVariable Long id){
return repository.getOne(id);
看一下效果:
服务器模式
服务器模式就是把H2数据库与应用单独部署,应用作为客户端去连接H2数据库
先下载安装H2数据库
找到安装bin目录下的批处理文件运行
H2数据库的服务启动起来了,默认控制台端口为8082
修改程序中的数据库连接配置
修改如下:
spring.datasource.platform=h2
spring.h2.console.enabled=true
logging.level.root=INFO
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:tcp://localhost//F:/H2/hello
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
利用刚才的spring boot程序测试
内存、嵌入、服务器三种模式在开发中的不同主要体现在,数据库连接URL配置这一块
spring.datasource.url=jdbc:h2:mem:hello
spring.datasource.url=jdbc:h2:file:F:/H2/hello
spring.datasource.url=jdbc:h2:tcp://localhost//F:/H2/hello
至于选用那种模式看你们开发中的具体情况而试,没有最好的技术选型,只有更合适的技术选型
H2数据库就给大家简单介绍到这里,欢迎大家来交流,指出文中一些说错的地方,让我加深认识,谢谢!
zczandzcz
- 1.4w
-
CoderV的进阶笔记
MySQL
Redis
- 1771
-
doodlewind
React.js
React Native