簡單介紹
關(guān)于數(shù)據(jù)層的三大組件,數(shù)據(jù)源,持久化技術(shù),數(shù)據(jù)庫。前兩種都已經(jīng)介紹過了SpringBoot的內(nèi)置的解決方案,還有最后一個數(shù)據(jù)庫,在SpringBoot中,內(nèi)置了三款數(shù)據(jù)庫。分別是:
- H2
- HSQL
- Derby
這三種數(shù)據(jù)庫有幾個共同點:
- 都是由Java語言編寫,可以作為Java類被注入到Spring容器中
- 輕量級,足夠輕巧,可以在內(nèi)存中運行
第一個特點可以讓他們被內(nèi)置在Spring中,第二個特點可以讓他們在程序運行的時候避免安裝直接被使用。這都是SpringBoot能內(nèi)置這三款數(shù)據(jù)庫的主要原因,并且足夠輕巧的特點也可以方便我們在測試階段做測試使用。
環(huán)境介紹
我們可以繼續(xù)使用之前的環(huán)境,但是我們需要對pom文件中的坐標做一些修改。將之前的MySQL的坐標注釋,然后添加H2數(shù)據(jù)庫的相關(guān)依賴,以及Web的相關(guān)依賴:
<!--H2數(shù)據(jù)庫必須的兩個坐標-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<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>
然后,我們將之前配置文件中的東西全部注釋,然后對H2數(shù)據(jù)庫和Web環(huán)境做一些配置:
server.port=80
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.url=jdbc:h2:~/test
最上面的是關(guān)于Web的相關(guān)配置,中間的是H2的相關(guān)配置,第一行表示開啟H2的控制臺,第二行表示H2Web端的控制臺路徑,第三部分是關(guān)于H2數(shù)據(jù)庫的數(shù)據(jù)源相關(guān)配置,這部分的配置僅在第一次連接H2的時候有用,當?shù)谝淮芜B接成功之后就可以刪除這段配置。
然后啟動SpringBoot的引導(dǎo)類:?
可以看到,控制臺輸出了很多我們之前沒有見過的東西?,紅色框中的部分就是H2相關(guān)的日志,然后我們打開瀏覽器,輸入H2Web控制臺的網(wǎng)址:
?如果你在你的瀏覽器上看到這個,就說明你的H2數(shù)據(jù)庫已經(jīng)啟動了,然后輸入默認的密碼123456,點擊【Connect】:
這個界面就是操作H2數(shù)據(jù)庫的Web端控制臺,我們可以在右側(cè)的輸入框中輸入SQL語句,然后在下面的狀態(tài)欄中看到結(jié)果:?
由于H2是運行在內(nèi)存中的,所以他的操作非常的快,但是存儲的數(shù)據(jù)量也不是很多,一般常用與我們在測試中。并且H2作為一個SQL數(shù)據(jù)庫,基本上MySQL中的語法也都支持,一些基本的增刪改查的操作都是一樣的。下面我們就用H2搭配Druid和MyBatis做一個完成的Dao層。
首先是我們要用到的所有的依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
?配置文件:
spring:
h2:
console:
enabled: true
path: /h2
datasource:
url: "jdbc:h2:~/test"
username: "sa"
password: "123456"
driver-class-name: "org.h2.Driver"
H2數(shù)據(jù)庫SQL語句:
create table tb_user (id int,name varchar)
insert into tn_user values(1,'張三'),(2,'李四'),(3,'王五'),(4,'趙六')
select * from tb_user
然后就是去編寫POJO和Mapper,這些我們之前都已經(jīng)看過了,所以我這里直接展示測試類中的測試方法:
@Test
void contextLoads(@Autowired userMapper userMapper) {
book user = userMapper.selectUserById(1);
System.out.println(user);
}
?方法還是之前的方法,參數(shù)都是一樣的,SpringBoot主打的就是一個只要是你能集成的東西,那么基本代碼不用變,無非就是導(dǎo)入一下坐標,然后更改一下配置文件而已。
在控制臺上也打印出了我們這里與H2相關(guān)的日志,這就基本完成了H2的集成。?
總結(jié)
到目前為止,我們關(guān)于數(shù)據(jù)層有關(guān)的三大組成部分,數(shù)據(jù)源,持久化技術(shù),數(shù)據(jù)庫,在SpringBoot中的默認解決方案就已經(jīng)介紹完了。這里就只是說了一下在SpringBoot中有,并且自動維護了這么一種技術(shù),具體的數(shù)據(jù)選型還是要根據(jù)當時的情況來決定,并且這集中技術(shù)的好壞優(yōu)勢等等都是要自己的深入學(xué)習(xí)之后,再根據(jù)情景去判斷。文章來源:http://www.zghlxwxcb.cn/news/detail-612380.html
除了介紹這幾種技術(shù),最主要的是這幾種技術(shù)可以互相的組合使用。比如我們一直在使用的MyBatis和Druid,MySQL的組合,在測試的時候,我們可以將MySQL換成速度更快的H2,或者將Druid換成配置更少的hikari,注意在使用的時候要導(dǎo)入對應(yīng)的依賴既可。文章來源地址http://www.zghlxwxcb.cn/news/detail-612380.html
到了這里,關(guān)于SpringBoot——內(nèi)置數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!