【SpringBoot DB 系列】h2databse 集成示例 demo

【SpringBoot DB 系列】h2databse 集成示例 demo

h2dabase 根据内存的数据库,更常见于嵌入式数据库的运用场景git教程,依application靠小,功能齐全;一般来讲,正常的商业项目用到它的场景不appearancegiti,但是在一些giti轮胎特别的 casSpringe 中,还是比较有用的,比方用于单元测试,业缓存务缓存,一些简略的示例 demo 等;appstore本文将手把手教你创立一个承继 h2dabase 的项目,并支撑从 sql 中导入预定好的 schema 和 data

I. 项目创立

本文对应的示例 demo,单元测试常用方法采用Spspringcloudr缓存视频变成本地视频inggithub中文官网网页Boot 2.2.1.RELEASE + maven 3.5gitee.3 + IDEA进行开发

1.spring框架 pom 装备

关于如何springboot创立一个Spring springboot 项目本giticomfort是什么轮胎文就不介绍了,在咱们创立好的项目中,pom.xml文件如下

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

sp单元测试的主要内容ring缝隙点重视一下dependency中的com.h2databa单元测试的主要内容se,另外两个非有必要,仅仅在后面的appstore单元测试家长评语试用例中会用到appstorgiticomfort是什么轮胎e,引荐加上

从上面的引入也能够知道,咱们将凭借 JPA 来操作数据库

2. 特点配git教程

既然单元测试是什么是连接数据库,当然少不了数据库的相关装备,在项目的资源路appreciate径下,新建装备文git指令applica缓存视频怎样转入相册tion.properties

# 数据库的相关装备
spring.datasource.url=jdbc:h2:~/h2-db
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

上面的装备办法,和咱们的 mysql 数据库装备没有什么特别的,这儿的 url 请注意一下

  • jdbc:h2:~/h2-db缓存的视频在哪: 嵌入式运用姿态,会在用缓存视频变成本地视频户根目录单元测试集成测试体系测试下生成一个名为h2-db.mv.db的文件(数据库的 schema 和 d column 就存在里边)
  • jdbc:h2:mem:DBName;DB_CLOSEappearance_DELAYgiticomfort是什么轮胎=-1: 内存形式,应用重启之后数据库会清git缓存视频怎样转入相册指令空,所以在测试用例中,能够考虑用这种

除了上面缓存视频在手机哪里找嵌入式的运用姿态之外,h单元测试中设计测试用例的依据是2-dabase 还支撑经过 tcp 办法,指定一个长途的目录

  • jdbcGigiti轮胎t:h2:tcp://localhost/~/test

上面是sprinappearg翻译 h2dagiticomfort是什么轮胎basegiti 的基springboot本装备,为了更spring是什么意思友好的展现,咱们敞开了 h2dabase 的 web conSpringsole 控制台

##h2 web console设置
spring.datasource.platform=h2
#进行该装备后,h2 web consloe就能够在长途拜访了。不然只能在本机拜访。
spring.h2.console.settings.web-allow-others=true
#进行该装备,你就能够经过YOUR_URL/h2拜访h2 web consloe
spring.h2.console.path=/h2
#进行该装备,程序敞开时就会发动h2 web consloe
spring.h2.console.enabled=true

最好敞开一下 jpa 的 sql 句子

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

II. 实例测试

上面装备搞完之后,基本上就能够说是完成了 h2dabase 的集成了

0. 进口

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

SpringBoot 应缓存用的发动进口,上面履行之后,咱们就可springboot面试单元测试家长评语以经过http://localhost缓存视频在手机哪里找:8080/hGit2拜访 h2dabaspringboot面试题se 的控制台,注意下面框处的内容,与前APP面的装备文件保持一致

【SpringBoot DB 系列】h2databse 集成示例 demo

登录之后,就是一个主张的apple数据库操作控制台了

1. Entity单元测试springboot面试题中规划测试用例的根据是 定义单元测试评语怎么写

下面这个归于 JPA 的知识点,对于 jpa 有兴趣的小伙伴,能够springcloud看一下前面的单元测试评语怎么写《JPA 系列教程》

@Entity
@Table(name = "test")
public class TestEntity {
    @Id
    private Integer id;
    @Column
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

2. Repository 接口

数据application库操作接口,直接运用默认的 curd 即可,并没有额定的添加办法

@Repository
public interface TestRepository extends CrudRepository<TestEntity, Integer> {
}

3.appointment 测试 case

接下来给几个 CURD 的spring框架测试 cAPPase,来演示一下咱们的集成作用

@RestController
public class TestController {
    @Autowired
    private TestRepository testRepository;
    @GetMapping("/save")
    public TestEntity save(Integer id, String name) {
        TestEntity testEntity = new TestEntity();
        testEntity.setId(id);
        testEntity.setName(name);
        return testRepository.save(testEntity);
    }
    @GetMapping("/update")
    public TestEntity update(Integer id, String name) {
        Optional<TestEntity> entity = testRepository.findById(id);
        TestEntity testEntity = entity.get();
        testEntity.setName(name);
        return testRepository.save(testEntity);
    }
    @GetMapping("/list")
    public Iterable list() {
        return testRepository.findAll();
    }
    @GetMapping("/get")
    public TestEntity get(Integer id) {
        return testRepository.findById(id).get();
    }
    @GetMapping("/del")
    public boolean del(Integer id) {
        testRepository.deleteById(id);
        return true;
    }
}

实测 case 如下

# 新增一条记载
curl 'http://localhost:8080/save?id=1&name=一灰灰'
# 查询记载
curl 'http://localhost:8080/get?id=1'
# 修正记载
curl 'http://localhost:8080/update?id=1&name=一灰灰Blog'
# 查询悉数
curl 'http://localhost:8080/list'
# 删除记载
curl 'http://localhost:8080/del?id=1'

【SpringBoot DB 系列】h2databse 集成示例 demo

4. sql 文件导入

注意咱们前面的spring漏洞存清理一切过程,没单元测试能发现约80的软件缺陷有任何一个地方有阐明需要主动去创立一个名为tspring是什么意思est的表,这github永久回家地址一点和咱单元测试集成测试系统测试们了解的 mysqlgithub中文官网网页 是不一样的;

某些时分咱们或许期望将准备缓存文件夹名称好的 sql 文件来初始化缓存视频变成本地视频数据库,这个时分能够如下操作单元测试评语怎么写

对应的 sql 文件

表结构 schema-h2.sql

DROP TABLE IF EXISTS book_to_book_store;
DROP TABLE IF EXISTS book_store;
DROP TABLE IF EXISTS book;
DROP TABLE IF EXISTS author;
DROP SEQUENCE IF EXISTS s_author_id;
CREATE SEQUENCE s_author_id START WITH 1;
CREATE TABLE author (
  id INT NOT NULL,
  first_name VARCHAR(50),
  last_name VARCHAR(50) NOT NULL,
  date_of_birth DATE,
  year_of_birth INT,
  address VARCHAR(50),
  CONSTRAINT pk_t_author PRIMARY KEY (ID)
);
CREATE TABLE book (
  id INT NOT NULL,
  author_id INT NOT NULL,
  co_author_id INT,
  details_id INT,
  title VARCHAR(400) NOT NULL,
  published_in INT,
  language_id INT,
  content_text CLOB,
  content_pdf BLOB,
  rec_version INT,
  rec_timestamp TIMESTAMP,
  CONSTRAINT pk_t_book PRIMARY KEY (id),
  CONSTRAINT fk_t_book_author_id FOREIGN KEY (author_id) REFERENCES author(id),
  CONSTRAINT fk_t_book_co_author_id FOREIGN KEY (co_author_id) REFERENCES author(id)
);
CREATE TABLE book_store (
  name VARCHAR(400) NOT NULL,
  CONSTRAINT uk_t_book_store_name PRIMARY KEY(name)
);
CREATE TABLE book_to_book_store (
  book_store_name VARCHAR(400) NOT NULL,
  book_id INTEGER NOT NULL,
  stock INTEGER,
  CONSTRAINT pk_b2bs PRIMARY KEY(book_store_name, book_id),
  CONSTRAINT fk_b2bs_bs_name FOREIGN KEY (book_store_name)
                             REFERENCES book_store (name)
                             ON DELETE CASCADE,
  CONSTRAINT fk_b2bs_b_id    FOREIGN KEY (book_id)
                             REFERENCES book (id)
                             ON DELETE CASCADE
);

数据文件 data-h2.sql

INSERT INTO author VALUES (next value for s_author_id, 'George', 'Orwell', '1903-06-25', 1903, null);
INSERT INTO author VALUES (next value for s_author_id, 'Paulo', 'Coelho', '1947-08-24', 1947, null);
INSERT INTO book VALUES (1, 1, null, null, '1984', 1948, 1, 'To know and not to know, to be conscious of complete truthfulness while telling carefully constructed lies, to hold simultaneously two opinions which cancelled out, knowing them to be contradictory and believing in both of them, to use logic against logic, to repudiate morality while laying claim to it, to believe that democracy was impossible and that the Party was the guardian of democracy, to forget, whatever it was necessary to forget, then to draw it back into memory again at the moment when it was needed, and then promptly to forget it again, and above all, to apply the same process to the process itself -- that was the ultimate subtlety; consciously to induce unconsciousness, and then, once again, to become unconscious of the act of hypnosis you had just performed. Even to understand the word ''doublethink'' involved the use of doublethink..', null, 1, '2010-01-01 00:00:00');
INSERT INTO book VALUES (2, 1, null, null, 'Animal Farm', 1945, 1, null, null, null, '2010-01-01 00:00:00');
INSERT INTO book VALUES (3, 2, null, null, 'O Alquimista', 1988, 4, null, null, 1, null);
INSERT INTO book VALUES (4, 2, null, null, 'Brida', 1990, 2, null, null, null, null);
INSERT INTO book_store (name) VALUES
	('Orell Fssli'),
	('Ex Libris'),
	('Buchhandlung im Volkshaus');
INSERT INTO book_to_book_store VALUES
	('Orell Fssli', 1, 10),
	('Orell Fssli', 2, 10),
	('Orell Fssli', 3, 10),
	('Ex Libris', 1, 1),
	('Ex Libris', 3, 2),
	('Buchhandlung im Volkshaus', 3, 1);

上面两个文件准备好,接下来咱们如何导入呢?

凭借sql-maven-plugin办法,在 pom 装备文件中,添加下面这段

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>create-database-h2</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>execute</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <driver>org.h2.Driver</driver>
            <url>jdbc:h2:~/h2-db</url>
            <username>test</username>
            <password></password>
            <autocommit>true</autocommit>
            <srcFiles>
                <srcFile>src/main/resources/schema-h2.sql</srcFile>
                <srcFile>src/main/resources/data-h2.sql</srcFile>
            </srcFiles>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.200</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

然后单元测试常用办法如下操作springcloud即可

【SpringBoot DB 系列】h2databse 集成示例 demo

导入成功之后,再到 h2 控制台,就能够查看对应的数据

【SpringBoot DB 系列】h2databse 集成示例 demo

II. 其他

0. 项目

  • 工程:githu缓存bgiti.com/liuyueyi/sp…
  • spring翻译目源码: github.com/liu单元测试集成测试系统测试yueyi/sp…

1. 一灰灰 Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力缓存视频在手机哪里找有限,不免有疏漏和过错之处,如发现 bugiti轮胎g 或许有更好的主张,欢迎批评指正,不吝感谢

下面一灰灰的个人博客,记载一切github中文官网网页学习和工作中的博spring面试题文,欢迎大家前去逛逛

  • 一灰灰 Bl单元测试用例og 个人博客 blog缓存.hhui.top
  • 一灰灰 Blog-Spring 专题博客 spring单元测试是什么.hhui.top