springboot整合H2內(nèi)存數(shù)據(jù)庫實現(xiàn)單元測試與數(shù)據(jù)庫無關(guān)性
一、新建spring boot工程
新建工程的時候,需要加入JPA,H2依賴

二、工程結(jié)構(gòu)

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);
}
}
六、配置文件
# 服務(wù)器端口號 server.port=7900 # 是否生成ddl語句 spring.jpa.generate-ddl=false # 是否打印sql語句 spring.jpa.show-sql=true # 自動生成ddl,由于指定了具體的ddl,此處設(shè)置為none spring.jpa.hibernate.ddl-auto=none # 使用H2數(shù)據(jù)庫 spring.datasource.platform=h2 # 指定生成數(shù)據(jù)庫的schema文件位置 spring.datasource.schema=classpath:schema.sql # 指定插入數(shù)據(jù)庫語句的腳本位置 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
可以看到測試結(jié)果
{"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());
}
}
發(fā)現(xiàn)測試是ok的!
九、總結(jié)
由于H2是關(guān)系內(nèi)存數(shù)據(jù)庫,當程序啟動的時候,會在內(nèi)存中創(chuàng)建表,并將數(shù)據(jù)存儲在內(nèi)存中,當重啟程序后,會自動刪除內(nèi)存中的數(shù)據(jù),從而可以很好的用來做dao層的單元測試和service層的單元測試,使整個程序不會依賴具體的數(shù)據(jù)庫,同時也提高了單元測試的效率。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java?Web中ServletContext對象詳解與應(yīng)用
ServletContext是一個容器,可以用來存放變量,供一個web項目中多個Servlet共享,下面這篇文章主要給大家介紹了關(guān)于Java?Web中ServletContext對象詳解與應(yīng)用的相關(guān)資料,需要的朋友可以參考下2023-04-04
SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式
這篇文章主要介紹了SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
Intellij IDEA集成JProfiler性能分析工具
作為Java程序員,性能分析是我們必須掌握的技能之一,在性能分析中,JProfiler是一款非常強大的工具,本文就來介紹一下Intellij IDEA集成JProfiler性能分析工具,就有一定的參考價值,感興趣的可以了解一下2023-12-12
如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。2021-01-01
shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)
這篇文章主要介紹了shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08
Spring的編程式事務(wù)TransactionTemplate的用法詳解
TransactionTemplate提供了一種在代碼中進行編程式事務(wù)管理的方式,使開發(fā)人員能夠在方法級別定義事務(wù)的開始和結(jié)束點,本文介紹了Spring框架中TransactionTemplate的用法,感興趣的朋友跟隨小編一起看看吧2023-07-07
Springboot實現(xiàn)定時任務(wù)的4種方式舉例詳解
在我們開發(fā)項目過程中經(jīng)常需要定時任務(wù)來幫助我們來做一些內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Springboot實現(xiàn)定時任務(wù)的4種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01

