springboot整合H2内存数据库,实现单元测试与数据库无关性
一、新建spring boot工程
新建工程的时候,需要加入JPA,H2依赖
二、工程结构
pom文件依赖如下:
<?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>4.0.0</modelVersion>
<groupId>com.chhliu.springboot.h2</groupId>
<artifactId>springboot-h2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-h2</name>
<description>Demo project for Spring Boot H2</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、编写实体类
package com.chhliu.springboot.h2.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Short age;
@Column
private BigDecimal balance;
……省略gettter和setter方法
}
四、编写dao
package com.chhliu.springboot.h2.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.chhliu.springboot.h2.entity.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
五、编写controller
package com.chhliu.springboot.h2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.chhliu.springboot.h2.entity.User;
import com.chhliu.springboot.h2.repository.UserRepository;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/user/{id}")// 注意,此处使用的是GetMapping注解,该注解的作用类似与@RequestMapping(value="/user/{id}" ,method=RequestMethod.GET),@PostMapping注解同理
public User findById(@PathVariable Long id) {
return this.userRepository.findOne(id);
}
}
六、配置文件
# 服务器端口号
server.port=7900
# 是否生成ddl语句
spring.jpa.generate-ddl=false
# 是否打印sql语句
spring.jpa.show-sql=true
# 自动生成ddl,由于指定了具体的ddl,此处设置为none
spring.jpa.hibernate.ddl-auto=none
# 使用H2数据库
spring.datasource.platform=h2
# 指定生成数据库的schema文件位置
spring.datasource.schema=classpath:schema.sql
# 指定插入数据库语句的脚本位置
spring.datasource.data=classpath:data.sql
# 配置日志打印信息
logging.level.root=INFO
logging.level.org.hibernate=INFO
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
logging.level.com.itmuch=DEBUG
七、启动程序
在浏览器中输入如下URL:
http://localhost:7900/user/4
可以看到测试结果
{"id":4,"username":"user4","name":"马六","age":20,"balance":100.00}
说明,我们的整合是OK的
八、测试dao层
package com.chhliu.springboot.h2;
import org.junit.Assert;
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 com.chhliu.springboot.h2.entity.User;
import com.chhliu.springboot.h2.repository.UserRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootH2ApplicationTests {
@Autowired
private UserRepository repository;
@Test
public void test(){
User u = repository.findOne(1L);
Assert.assertEquals("成功的测试用例", "张三", u.getName());
}
}
发现测试是ok的!
九、总结
由于H2是关系内存数据库,当程序启动的时候,会在内存中创建表,并将数据存储在内存中,当重启程序后,会自动删除内存中的数据,从而可以很好的用来做dao层的单元测试和service层的单元测试,使整个程序不会依赖具体的数据库,同时也提高了单元测试的效率。
springboot整合H2内存数据库,实现单元测试与数据库无关性的更多相关文章
- SpringBoot整合Quartz定时任务(持久化到数据库)
背景 最近在做项目,项目中有个需求:需要使用定时任务,这个定时任务需要即时生效.查看Quartz官网之后发现:Quartz提供两种基本作业存储类型: RAMJobStore :RAM也就是内存,默认情 ...
- Springboot整合Mybatis,连接多个数据库(Mysql+Oracle)
maven依赖,需要注意的是mysql使用的版本 1 <dependencies> 2 <dependency> 3 <groupId>com.oracle.dat ...
- SpringBoot整合系列-整合H2
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959855.html SpringBoot整合H2内存数据库 一般我们在测试的时候习惯于 ...
- Spring Boot学习笔记:整合H2数据库
H2数据库:java语言编写的嵌入式sql数据库.可以和应用一起打包发布. H2有三种连接模式(Connection Modes): Embedded mode (local connections ...
- springboot整合mybatis(SSM开发环境搭建)
0.项目结构: ---------------------方法一:使用mybatis官方提供的Spring Boot整合包实现--------------------- 1.application.p ...
- Springboot整合shardingsphere和druid进行读写分离
最近在使用springboot整合shardingsphere和druid实现mysql数据库读写分离时遇到了一些问题,特此记录一下. 依赖版本 Springboot 2.1.6.RElEASE sh ...
- spring security关闭http验证 和 springboot 使用h2数据库
spring security关闭http验证 最近在跑demo的过程中,访问swagger页面的时候需要验证登录,记得在之前写的代码中是关闭了security验证,无需登录成功访问,直接在appli ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- springboot整合mybatis连接mysql数据库出现SQLException异常
在springboot整合mybatis连接数据库的时候,项目中遇到一个SQLException,我检查了properties配置文件,看数据源有没有配错,检查有没有打错字,在数据库中把sql语句查询 ...
随机推荐
- reorder-list——链表、快慢指针、逆转链表、链表合并
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- UI标签库专题二:JEECG智能开发平台Column(列) 子标签
UI标签库专题二:JEECG智能开发平台Column(列) 子标签 1.1. Column(列) 子标签 1.1.1. 演示样例 <t:dgCol title="年龄" ...
- winform中使用ReportViewer的时候,找不到报表数据面板.
最近在做winform的报表项目,看人家教程里都有报表数据面板,可我的怎么都找不到,我的是VS2012,最后终于找到,首先必须在报表设计TAB上(选中它),然后选择视图->报表数据
- RAC改动归档文件夹
逐个节点改动 关闭全部节点,启动单节点(rac1)到mount状态 SQL> startup mount; 改动server參数配置 SQL> alter system set clust ...
- DASH----Desktop and mobile Architecture for System Hardware----桌面和移动系统硬件架构(DASH)计划
http://baike.baidu.com/subview/813787/11301142.htm http://sites.amd.com/cn/business/it-solutions/man ...
- android-auto-scroll-view-pager
https://github.com/eltld/android-auto-scroll-view-pager
- Filter注入对象
由于没有在web.xml文件中加上<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter ...
- linux进程间通信消息队列:msgsnd: Invalid argument
今天写了个消息队列的小测试程序结果send端程序总是出现:msgsnd: Invalid argument,搞了半个小时也没搞明白,后来查资料发现我将(st_msg_buf.msg_type = 0; ...
- Oracle核心技术 笔记(该书读得不细致,须要找时间再细读~~)
Oracle核心技术 跳转至: 导航. 搜索 文件夹 1 開始 2 redo和undo 3 事务与一致性 4 锁与闩 5 缓存和复制 6 写入和恢复 7 解析与优化 8 RAC及'缺陷' 9 附录A ...
- vmware 自动挂起
用VMware跑虚拟机,经常会出现客户操作系统自己挂起的现象,怀疑是主机自己休眠的设置.设置之后,无效. 后来才发现不是主机休眠设置,还是应该设置客户操作系统中的休眠设置. 在客户机,控制面板 电源 ...