乾貨:springboot與h2資料庫快速測試

前言:最近學習 springboot 時,編寫的 demo 由於依賴了 MySQL,上傳 git,換個地方繼續時,就得安裝 MySQL,很麻煩,特別有些案例只需要些樣本資料就好(當然完全不要ORM 又不像話)。

乾貨:springboot與h2資料庫快速測試

所以學習下使用 H2,加快練習步伐,又不影響各個層級角色。

開發工具

JDK 1。8

sts 4

乾貨:springboot與h2資料庫快速測試

sts版本

Maven 3。6。1

快速構建springboot專案

sts 新建 starter 專案:

乾貨:springboot與h2資料庫快速測試

next,勾選所需的 starter 依賴模組,這裡只需要勾選的以下幾項就OK:

乾貨:springboot與h2資料庫快速測試

(DevTools 是為方便熱部署,小改程式碼時不用重啟應用;JDBC 則是後面使用JdbcTemplate 代替 mybatis 快速測試 H2資料庫 連線用的。PS:這些

不勾

後續再追加進 Maven 依賴配置也是可以的。)

finish,構建完成,專案結構和 Maven 依賴如下:

乾貨:springboot與h2資料庫快速測試

專案結構圖↑ pom檔案首行報錯還不知道為啥。。不影響功能

org。springframework。boot spring-boot-starter-jdbc org。springframework。boot spring-boot-starter-web org。mybatis。spring。boot mybatis-spring-boot-starter 2。0。1 org。springframework。boot spring-boot-devtools runtime com。h2database h2 runtime org。springframework。boot spring-boot-starter-test test

簡單配置下 application。properties 檔案:

server。port=8080server。servlet。context-path=/demo

快速執行驗證下新專案效果。

執行 DemoH2Application。java 入口類,瀏覽器輸入 http://localhost:8080/demo/ 看到下面介面,就代表應用啟動OK,只是沒新增 index 頁面罷了。

乾貨:springboot與h2資料庫快速測試

開發與測試

1)配置 h2 資料來源

server。port=8080server。servlet。context-path=/demo#配置 h2 資料來源spring。datasource。url=jdbc:h2:mem:h2testspring。datasource。driver-class-name=org。h2。Driverspring。datasource。username=rootspring。datasource。password=123456spring。datasource。schema=classpath:h2sql/init。sqlspring。datasource。data=classpath:h2sql/data。sql

2)建立上述配置的簡單的指令碼,應用啟動時會執行。

>>h2sql/init。sql

drop table if exists tb_fake;create table tb_fake ( id serial , name varchar(50))

>>h2sql/data。sql

insert into tb_fake(name) values(‘菠蘿包’);insert into tb_fake(name) values(‘皮卡丘’);insert into tb_fake(name) values(‘悟空’);insert into tb_fake(name) values(‘克林’);insert into tb_fake(name) values(‘弗利沙’);

3)編寫簡單的Dao類,組合 spring 的 JdbcTemplate。

乾貨:springboot與h2資料庫快速測試

4)開始單元測試 src/test/java >> XXXApplicationTests.java

至此專案包結構如下:

乾貨:springboot與h2資料庫快速測試

首先簡單測試下h2資料來源的連線資訊。

@RunWith(SpringRunner。class)//@SpringBootTest@SpringBootTest(webEnvironment = SpringBootTest。WebEnvironment。NONE) //不載入web環境,更快public class DemoH2ApplicationTests { @Autowired private FakeDao fakeDao; @Test public void testH2DbConnect() throws SQLException { DataSource dataSource = fakeDao。getJdbcTemlate()。getDataSource(); Connection conn = dataSource。getConnection(); System。out。println(“>>” + conn。toString()); //連線資訊,看是否與配置一致 }

Juni 執行 @Test 方法,輸出如下:

乾貨:springboot與h2資料庫快速測試

說明 h2 配置和連線,以及 bean 的注入使用都沒問題。

繼續測試 Dao 的兩個簡單方法,注意對比初始化指令碼資料。

@Test public void testDaoCount() { int cnt = fakeDao。selectCountAll(); //預期為5個 System。out。println(“>> fake count: ” + cnt); } @Test public void testDaoSelectNameById() { //按指令碼插入順序, name=‘皮卡丘’ 的 id是2 String name = fakeDao。selectNameById(2); System。out。println(“>> fake name: ” + name); }

執行結果:

乾貨:springboot與h2資料庫快速測試

乾貨:springboot與h2資料庫快速測試

測試結束,打完手工。(有時間補上mybatis的簡潔整合。。。)

補:H2資料型別

列下我常用的 Java實體-H2資料型別對應。

Java <> H2

Integer <> int、tinyint、smallint

Long <> bigint、identity(自增)

Float <> real

Double <> double

BigDecimal <> bigdecimal

Boolean <> boolean

Date <> date、timestamp(沒有 datetime。。。)

String <> char、varchar

文章參考:https://my。oschina。net/u/913265/blog/892904

官方參考:http://www。h2database。com/html/datatypes。html

第一發文章,歡迎踩點吐槽,收藏轉發!

乾貨:springboot與h2資料庫快速測試