Springboot-H2DB
为什么在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的更多相关文章
- Decoration1:Spring-boot基础实现
前段时间发布的Traveller项目,花费了不少精力,但是效果并不如意,根源在于瀑布式的开发思想不适合这种独立的学习项目.在项目初始就规划一个全面的web系统,,因为预设了一个前景,在心理上会想尽快看 ...
- 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用
问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- Springboot搭建web项目
最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...
- Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)
这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...
- 解决 SpringBoot 没有主清单属性
问题:SpringBoot打包成jar后运行提示没有主清单属性 解决:补全maven中的bulid信息 <plugin> <groupId>org.springframewor ...
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- springboot 学习资源推荐
springboot 是什么?对于构建生产就绪的Spring应用程序有一个看法. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.(这是springboot的官方介绍) 我们为什么要学 ...
- Springboot框架
本片文章主要分享一下,Springboot框架为什么那么受欢迎以及如何搭建一个Springboot框架. 我们先了解一下Springboot是个什么东西,它是干什么用的.我是刚开始接触,查了很多资料, ...
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...
随机推荐
- git自动上传脚本及基本代码
git_auto.bat git add . git add -A git add -u git commit -m "text" git pull --rebase origin ...
- 【监控实践】【4.2】perfmon监控性能计数器(基于typeperf命令)
关键词:typeperf typeperf 命令 使用示例: 案例1:#使用typeperf收集windows cpu.内存.硬盘性能 #使用typeperf收集windows cpu.内存.硬盘性能 ...
- T-聊天止于呵呵
(现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵”,即对话的最后一句话包含单词 hehe 或者它的变形. 具体来说,我们首先提取出对话的最后一句 ...
- oracle数据库启动报错,不能启动ASM实例
数据库rac启动时报错,日志例如以下,后来使用 Sat Jun 7 06:02:11 2014 GATHER_STATS_JOB encountered errors. Check the tra ...
- 在SQL中存储过程的一般语法
一般分为十种情况,每种语法各不相同: 1. 创建语法 1 2 3 4 5 6 7 create proc | procedure pro_name [{@参数数据类型} [=默认值] [outp ...
- 神经网络之:S型神经元
1.S 型神经元和感知器类似,但是被修改为权重和偏置的微小改动只引起输出的微小变化 2.S型神经元结构: S型函数: 带有x1,x2,........,权重w1,w2.....,和偏置b的S型神经元的 ...
- Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- AlphaStar: Mastering the Real-Time Strategy Game StarCraft II 博客要点
original blog: https://deepmind.com/blog/alphastar-mastering-real-time-strategy-game-starcraft-ii S ...
- Tensorflow 多gpu训练
Tensorflow可在训练时制定占用那几个gpu,但如果想真正的使用多gpu训练,则需要手动去实现. 不知道tf2会不会改善一下. 具体参考:https://wizardforcel.gitbook ...
- CPU指令重排序与MESI缓存一致性
一.重排序场景 class ResortDemo { int a = 0; boolean flag = false; public void writer() { a = 1; //1 flag = ...