为什么在Springboot中用H2DB

用Springboot开发需要连接数据库的程序的时候,使用H2DB作为内存数据库可以很方便的调试程序。

怎么用

1.加入依赖

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

2.创建实体类,并指明表的名称,这个实体类是放在src/main/java 下的,注意,这个实体类是应用程序会用到的,不是为了测试而写的。不用我们先CreateDB,再在DB下create 一个 Table,H2DB根据实体类会帮我们create 好Table 放在H2DB里面。

package com.springboot.h2;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name = "T_CUSTOMER")
public class Customer { @Id
@Column(name = "customer_id")
private Long customerId; @Column(name = "customer_name")
private String customerName; public Long getCustomerId() {
return customerId;
} public void setCustomerId(Long customerId) {
this.customerId = customerId;
} public String getCustomerName() {
return customerName;
} public void setCustomerName(String customerName) {
this.customerName = customerName;
} public String toString() {
return String.format("Customer id: %d ,Customer name: %s", customerId, customerName);
} }

3.在src/test/resource 下面放置 准备测试数据的SQL文件

4.此时,开始写测试类

package com.springboot.h2;

import java.util.List;

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; @RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class H2DBTest { @Autowired
private CustomerRepository repository; @Test
public void getAllCustomerTest() { List<Customer> customers = repository.findAll(); for(Customer c : customers) {
System.out.println(c);
} }
}

当run测试类的H2DB 会首先执行放在 src/test/resource  下面的SQL文件,准备好测试数据。

在 @Test 方法里就可以测试程序的逻辑了。

源码如下,请笑纳 :)

https://github.com/XLuffyStory/springboot-h2DB-test

Springboot-H2DB的更多相关文章

  1. Decoration1:Spring-boot基础实现

    前段时间发布的Traveller项目,花费了不少精力,但是效果并不如意,根源在于瀑布式的开发思想不适合这种独立的学习项目.在项目初始就规划一个全面的web系统,,因为预设了一个前景,在心理上会想尽快看 ...

  2. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  3. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  4. Springboot搭建web项目

    最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...

  5. Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)

    这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...

  6. 解决 SpringBoot 没有主清单属性

    问题:SpringBoot打包成jar后运行提示没有主清单属性 解决:补全maven中的bulid信息 <plugin> <groupId>org.springframewor ...

  7. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  8. springboot 学习资源推荐

    springboot 是什么?对于构建生产就绪的Spring应用程序有一个看法. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.(这是springboot的官方介绍) 我们为什么要学 ...

  9. Springboot框架

    本片文章主要分享一下,Springboot框架为什么那么受欢迎以及如何搭建一个Springboot框架. 我们先了解一下Springboot是个什么东西,它是干什么用的.我是刚开始接触,查了很多资料, ...

  10. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

随机推荐

  1. SVN服务器和客户端使用教程总结

    一.SVN简介 Subversion是什么? 它是一个自由/开源的版本控制系统,一组文件存放在中心版本库,记录每一次文件和目录的修改,Subversion允许把数据恢复到早期版本,或是检查数据修改的历 ...

  2. mybatis源码级别深度剖析

    mybatis 3.x源码深度解析与最佳实践 Mybatis源码解析优秀博文

  3. Java中HashMap扩容机制思考

    1. HashMap在什么条件下扩容 判断HashMap的数组Size大小如果超过loadFactor*capacity,就要扩容. 相关的类属性: capacity:当前数组容量,始终保持 2^n, ...

  4. Zend Framework MVC的结构

    The Zend Framework MVC Architecture 一.概述: In this chapter, we will cover the following topics:1. Zen ...

  5. Python数据基础类型-新建列表

    1,遍历列表 遍历列表的所有元素,对每个元素执行相同的操作.可使用for循环 magicians = ['alice','david','carolina'] for magician in magi ...

  6. 【已解决】Error running 'xxx项目' Command line is too long(idea版)

    [错误] Error running 'xxx项目': Command line is too long. Shorten command line for xxx or also for Sprin ...

  7. 坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求

    本文链接:https://blog.csdn.net/qq_26909801/article/details/96966372数值型坐标轴刻度计算算法前言算法描述上代码代码运行效果结语前言因实习的公司 ...

  8. spark 在启动的时候出现JAVA_HOME not set

    解决方法:在sbin目录下的spark-config.sh 中添加对应的jdk 路径,然后使用scp -r 命令复制到各个worker节点

  9. JS中对数组元素进行增、删、改、查的方法,以及其他方法

    前言 昨天联调一个页面,看着就一个页面,接口倒是不少. 热点问题配置测试联调完成(同步异步接口共11个) 1.配置新增 2.配置编辑 3.配置删除 4.热点问题新增 5.热点问题编辑 6.热点问题删除 ...

  10. javaweb新手学习之Tomcat

    一.Tomcat服务器常见启动问题: (1).Java_home环境变量,由于tomcat服务器的bin目录中的一些jar文件必须使用到java类库,所以必须先配置Java_home环境变量. (2) ...