135-1821-9792

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

一、新建spring boot工程

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

在高淳等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、网站建设 网站设计制作定制网站开发,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销推广,外贸网站建设,高淳网站建设费用合理。

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

二、工程结构

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

pom文件依赖如下:

<?xml version="1.0" encoding="UTF-8"?> 
 
 4.0.0 
 
 com.chhliu.springboot.h3 
 springboot-h3 
 0.0.1-SNAPSHOT 
 jar 
 
 springboot-h3 
 Demo project for Spring Boot H2 
 
  
  org.springframework.boot 
  spring-boot-starter-parent 
  1.4.3.RELEASE 
    
  
 
  
  UTF-8 
  UTF-8 
  1.7 
  
 
  
   
   org.springframework.boot 
   spring-boot-starter-data-jpa 
   
   
   org.springframework.boot 
   spring-boot-starter-web 
   
 
   
   com.h3database 
   h3 
   runtime 
   
   
   org.springframework.boot 
   spring-boot-starter-test 
   test 
   
  
 
  
   
    
    org.springframework.boot 
    spring-boot-maven-plugin 
    
   
  
 

三、编写实体类

package com.chhliu.springboot.h3.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.h3.repository; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 
import com.chhliu.springboot.h3.entity.User; 
@Repository 
public interface UserRepository extends JpaRepository { 
 
} 

五、编写controller

package com.chhliu.springboot.h3.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.h3.entity.User; 
import com.chhliu.springboot.h3.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=h3 
# 指定生成数据库的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.h3; 
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.h3.entity.User; 
import com.chhliu.springboot.h3.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内存数据库实现单元测试与数据库无关性
文章URL:http://kswsj.com/article/ghgdcc.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号