springboot集成h2数据库

springboot集成h2数据库

简介

1、H2数据库是一个开源的关系型数据库。H2采用java语言编写,不受平台的限制,同时支持网络版和嵌入式版本,有比较好的兼容性,支持相当标准的sql标准

2、提供JDBC、ODBC访问接口,提供了非常友好的基于web的数据库管理界面

官网:
http://www.h2database.com/

引入依赖

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.6.RELEASEversion>
        <relativePath/> 
    parent>
...
	<properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <skipTests>trueskipTests>
    properties>
	<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.h2databasegroupId>
            <artifactId>h2artifactId>
            <scope>runtimescope>
        dependency>
    
    dependencies>

h2配置

#=============== h2  =======================
spring.datasource.driverClassName=org.h2.Driver
#配置h2内存数据库的连接地址
spring.datasource.url=jdbc:h2:mem:h2_demo
#Windows当前用户路径,持久化h2数据库
#spring.datasource.url = jdbc:h2:file:~/.h2/h2_demo;AUTO_SERVER=TRUE
#可执行程序的当前路径,持久化h2数据库
#spring.datasource.url = jdbc:h2:file:./db/h2_demo;AUTO_SERVER=TRUE
#指定的静态配置路径,持久化h2数据库
#spring.datasource.url = jdbc:h2:file:D:/db/.h2/h2_demo;AUTO_SERVER=TRUE
spring.datasource.username=sa
spring.datasource.password=sa
##数据初始化设置
#进行该配置后,每次启动程序,程序都会运行resources/db/schema.sql文件,对数据库的结构进行操作。
spring.datasource.schema=classpath:db/schema.sql
#进行该配置后,每次启动程序,程序都会运行resources/db/data.sql文件,对数据库的数据操作。
spring.datasource.data=classpath:db/data.sql
##h2 web console设置
#表明使用的数据库平台是h2
spring.datasource.platform=h2
# 进行该配置后,h2 web consloe就可以在远程访问了。否则只能在本机访问。
spring.h2.console.settings.web-allow-others=true
#进行该配置,你就可以通过YOUR_URL/h2访问h2 web consloe。YOUR_URL是你程序的访问URl。
spring.h2.console.path=/h2
#进行该配置,程序开启时就会启动h2 web consloe。当然这是默认的,如果你不想在启动程序时启动h2 web consloe,那么就设置为false。
spring.h2.console.enabled=true

数据初始化

初始化数据放在resouces/db文件夹下

schema.sql
create table if not exists USER (
  ID varchar(32) not null primary key,
  NAME VARCHAR(32),
  AGE int(2)
);
data.sql
INSERT INTO USER (ID,NAME,AGE) VALUES
('20210424', '阿萨', 22),
('20210425', '尔特', 23),
('20210426', '富贵', 24);

JPA演示h2

引入JPA依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-jpaartifactId>
dependency>

JPA配置

#=============== jpa  =======================
#jpa配置#启用SQL语句的日志记录
spring.jpa.show-sql=true
#设置ddl模式
spring.jpa.hibernate.ddl-auto=update

JpaRepository持久层

domain

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Getter
@Setter
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Column
    private String name;

    @Column
    private Integer age;
}

repository

import com.h2.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {
}

单元测试

import com.h2.dao.UserRepository;
import com.h2.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class H2ServerApplicationTests {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void contextLoads() {
        List<User> users = userRepository.findAll();
        for (User user : users) {
            System.out.println("{id : " + user.getId() + ", name : " + user.getName() + ", age : " + user.getAge() + "}");
        }
    }
}

H2数据库

http://localhost:port/xxx/h2

登录数据参考:h2数据库的连接地址配置

你可能感兴趣的:(springboot,h2,java,spring)