欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合H2內存數(shù)據庫實現(xiàn)單元測試與數(shù)據庫無關性

 更新時間:2022年02月17日 16:11:51   作者:牛奮lch  
本篇文章主要介紹了springboot整合H2內存數(shù)據庫實現(xiàn)單元測試與數(shù)據庫無關性,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、新建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數(shù)據庫 
spring.datasource.platform=h2 
# 指定生成數(shù)據庫的schema文件位置 
spring.datasource.schema=classpath:schema.sql 
# 指定插入數(shù)據庫語句的腳本位置 
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()); 
 } 
} 

發(fā)現(xiàn)測試是ok的!

九、總結

由于H2是關系內存數(shù)據庫,當程序啟動的時候,會在內存中創(chuàng)建表,并將數(shù)據存儲在內存中,當重啟程序后,會自動刪除內存中的數(shù)據,從而可以很好的用來做dao層的單元測試和service層的單元測試,使整個程序不會依賴具體的數(shù)據庫,同時也提高了單元測試的效率。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java?Web中ServletContext對象詳解與應用

    Java?Web中ServletContext對象詳解與應用

    ServletContext是一個容器,可以用來存放變量,供一個web項目中多個Servlet共享,下面這篇文章主要給大家介紹了關于Java?Web中ServletContext對象詳解與應用的相關資料,需要的朋友可以參考下
    2023-04-04
  • 詳解Spring Boot實戰(zhàn)之單元測試

    詳解Spring Boot實戰(zhàn)之單元測試

    本篇文章主要介紹了詳解Spring Boot實戰(zhàn)之單元測試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot異步線程父子線程數(shù)據傳遞的5種方式

    SpringBoot異步線程父子線程數(shù)據傳遞的5種方式

    這篇文章主要介紹了SpringBoot異步線程父子線程數(shù)據傳遞的5種方式,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • Intellij IDEA集成JProfiler性能分析工具

    Intellij IDEA集成JProfiler性能分析工具

    作為Java程序員,性能分析是我們必須掌握的技能之一,在性能分析中,JProfiler是一款非常強大的工具,本文就來介紹一下Intellij IDEA集成JProfiler性能分析工具,就有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 如何用Springboot Admin監(jiān)控你的微服務應用

    如何用Springboot Admin監(jiān)控你的微服務應用

    這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務應用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。
    2021-01-01
  • shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)

    shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)

    這篇文章主要介紹了shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)的相關資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學習吧
    2016-08-08
  • Spring的編程式事務TransactionTemplate的用法詳解

    Spring的編程式事務TransactionTemplate的用法詳解

    TransactionTemplate提供了一種在代碼中進行編程式事務管理的方式,使開發(fā)人員能夠在方法級別定義事務的開始和結束點,本文介紹了Spring框架中TransactionTemplate的用法,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • java 文件和byte互轉的實例

    java 文件和byte互轉的實例

    下面小編就為大家分享一篇java 文件和byte互轉的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • Java代碼實現(xiàn)微信頁面滾動防露底(核心代碼)

    Java代碼實現(xiàn)微信頁面滾動防露底(核心代碼)

    這篇文章主要介紹了Java代碼實現(xiàn)微信頁面滾動防露底的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Springboot實現(xiàn)定時任務的4種方式舉例詳解

    Springboot實現(xiàn)定時任務的4種方式舉例詳解

    在我們開發(fā)項目過程中經常需要定時任務來幫助我們來做一些內容,下面這篇文章主要給大家介紹了關于Springboot實現(xiàn)定時任務的4種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-01-01

最新評論