springboot整合H2内存数据库,实现单元测试与数据库无关性

简介: springboot整合H2内存数据库,实现单元测试与数据库无关性 一、新建spring boot工程 新建工程的时候,需要加入JPA,H2依赖 二、工程结构 pom文件依赖如下: [html] view plain copy <?xml version="1.

springboot整合H2内存数据库,实现单元测试与数据库无关性

一、新建spring boot工程

新建工程的时候,需要加入JPA,H2依赖

二、工程结构

pom文件依赖如下:

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <groupId>com.chhliu.springboot.h2</groupId>
  6.     <artifactId>springboot-h2</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>jar</packaging>
  9.     <name>springboot-h2</name>
  10.     <description>Demo project for Spring Boot H2</description>
  11.     <parent>
  12.         <groupId>org.springframework.boot</groupId>
  13.         <artifactId>spring-boot-starter-parent</artifactId>
  14.         <version>1.4.3.RELEASE</version>
  15.         <relativePath/> <!-- lookup parent from repository -->
  16.     </parent>
  17.     <properties>
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20.         <java.version>1.7</java.version>
  21.     </properties>
  22.     <dependencies>
  23.         <dependency>
  24.             <groupId>org.springframework.boot</groupId>
  25.             <artifactId>spring-boot-starter-data-jpa</artifactId>
  26.         </dependency>
  27.         <dependency>
  28.             <groupId>org.springframework.boot</groupId>
  29.             <artifactId>spring-boot-starter-web</artifactId>
  30.         </dependency>
  31.         <dependency>
  32.             <groupId>com.h2database</groupId>
  33.             <artifactId>h2</artifactId>
  34.             <scope>runtime</scope>
  35.         </dependency>
  36.         <dependency>
  37.             <groupId>org.springframework.boot</groupId>
  38.             <artifactId>spring-boot-starter-test</artifactId>
  39.             <scope>test</scope>
  40.         </dependency>
  41.     </dependencies>
  42.     <build>
  43.         <plugins>
  44.             <plugin>
  45.                 <groupId>org.springframework.boot</groupId>
  46.                 <artifactId>spring-boot-maven-plugin</artifactId>
  47.             </plugin>
  48.         </plugins>
  49.     </build>
  50. </project>

三、编写实体类

[java] view plain copy

  1. package com.chhliu.springboot.h2.entity;
  2. import java.math.BigDecimal;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.GenerationType;
  7. import javax.persistence.Id;
  8. @Entity
  9. public class User {
  10.   @Id
  11.   @GeneratedValue(strategy = GenerationType.AUTO)
  12.   private Long id;
  13.   @Column
  14.   private String username;
  15.   @Column
  16.   private String name;
  17.   @Column
  18.   private Short age;
  19.   @Column
  20.   private BigDecimal balance;
  21.   ……省略gettter和setter方法
  22. }

四、编写dao

[java] view plain copy

  1. package com.chhliu.springboot.h2.repository;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. import org.springframework.stereotype.Repository;
  4. import com.chhliu.springboot.h2.entity.User;
  5. @Repository
  6. public interface UserRepository extends JpaRepository<User, Long> {
  7. }

五、编写controller

[java] view plain copy

  1. package com.chhliu.springboot.h2.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.chhliu.springboot.h2.entity.User;
  7. import com.chhliu.springboot.h2.repository.UserRepository;
  8. @RestController
  9. public class UserController {
  10.   @Autowired
  11.   private UserRepository userRepository;
  12.   @GetMapping("/user/{id}")// 注意,此处使用的是GetMapping注解,该注解的作用类似与@RequestMapping(value="/user/{id}" ,method=RequestMethod.GET),@PostMapping注解同理
  13.   public User findById(@PathVariable Long id) {
  14.     return this.userRepository.findOne(id);
  15.   }
  16. }

六、配置文件

[java] view plain copy

  1. # 服务器端口号
  2. server.port=7900
  3. # 是否生成ddl语句
  4. spring.jpa.generate-ddl=false
  5. # 是否打印sql语句
  6. spring.jpa.show-sql=true
  7. # 自动生成ddl,由于指定了具体的ddl,此处设置为none
  8. spring.jpa.hibernate.ddl-auto=none
  9. # 使用H2数据库
  10. spring.datasource.platform=h2
  11. # 指定生成数据库的schema文件位置
  12. spring.datasource.schema=classpath:schema.sql
  13. # 指定插入数据库语句的脚本位置
  14. spring.datasource.data=classpath:data.sql
  15. # 配置日志打印信息
  16. logging.level.root=INFO
  17. logging.level.org.hibernate=INFO
  18. logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
  19. logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
  20. logging.level.com.itmuch=DEBUG

七、启动程序

在浏览器中输入如下URL:

[java] view plain copy

  1. http://localhost:7900/user/4

可以看到测试结果

[java] view plain copy

  1. {"id":4,"username":"user4","name":"马六","age":20,"balance":100.00}

说明,我们的整合是OK的

八、测试dao层

[java] view plain copy

  1. package com.chhliu.springboot.h2;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. import com.chhliu.springboot.h2.entity.User;
  9. import com.chhliu.springboot.h2.repository.UserRepository;
  10. @RunWith(SpringRunner.class)
  11. @SpringBootTest
  12. public class SpringbootH2ApplicationTests {
  13.     @Autowired
  14.     private UserRepository repository;
  15.     @Test
  16.     public void test(){
  17.         User u = repository.findOne(1L);
  18.         Assert.assertEquals("成功的测试用例""张三", u.getName());
  19.     }
  20. }

发现测试是ok的!

九、总结

由于H2是关系内存数据库,当程序启动的时候,会在内存中创建表,并将数据存储在内存中,当重启程序后,会自动删除内存中的数据,从而可以很好的用来做dao层的单元测试和service层的单元测试,使整个程序不会依赖具体的数据库,同时也提高了单元测试的效率。

相关文章
|
1月前
|
NoSQL Java 数据库
【问题篇】springboot项目通过数据库限制实例端口号
【问题篇】springboot项目通过数据库限制实例端口号
19 0
|
17天前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
21 0
|
16天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
17天前
|
Java 测试技术 数据库
SpringBoot启动时设置不加载数据库
SpringBoot启动时设置不加载数据库
10 0
|
1月前
|
SQL Java 数据库连接
springboot解析txt文件顺便加到数据库中(nohup文件)
springboot解析txt文件顺便加到数据库中(nohup文件)
112 1
|
1月前
|
Oracle Java 关系型数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
|
1月前
|
Java 测试技术 数据库
springboot大学生体质测试管理系统
springboot大学生体质测试管理系统
|
1月前
|
前端开发 Java 数据库
基于Springboot的漫画网站22(程序+数据库+论文)可帮忙远程调试
基于Springboot的漫画网站22(程序+数据库+论文)可帮忙远程调试
|
1月前
|
存储 JSON 监控
Higress Controller**不是将配置信息推送到Istio的内存存储里面的**。
【2月更文挑战第30天】Higress Controller**不是将配置信息推送到Istio的内存存储里面的**。
14 1
|
1月前
|
存储 C语言
C语言--------数据在内存中的存储
C语言--------数据在内存中的存储
26 0