SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫(kù)的示例代碼
前言
- H2數(shù)據(jù)庫(kù)是一個(gè)開源的關(guān)系型數(shù)據(jù)庫(kù)。H2采用java語(yǔ)言編寫,不受平臺(tái)的限制,同時(shí)支持網(wǎng)絡(luò)版和嵌入式版本,有比較好的兼容性,支持相當(dāng)標(biāo)準(zhǔn)的sql標(biāo)準(zhǔn)
- 提供JDBC、ODBC訪問(wèn)接口,提供了非常友好的基于web的數(shù)據(jù)庫(kù)管理界面
官網(wǎng):http://www.h2database.com/
Maven依賴
<!--jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--h2--> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
Conroller
@RestController public class UserController { @Autowired UserRepository userRepository; @RequestMapping("/list") public List<User> findAll(){ List<User> userList = userRepository.findAll(); return userList; } @RequestMapping("/save") public String save(User user){ userRepository.save(user); return "保存成功"; @RequestMapping("/update") public String update(User user){ return "更新成功"; @RequestMapping("/delete") public String delete(Integer id){ userRepository.deleteById(id); return "刪除成功"; }
實(shí)體類
@AllArgsConstructor @NoArgsConstructor @Entity @Data @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private Integer age; private Integer gender; }
Repository
@Repository public interface UserRepository extends JpaRepository<User,Integer> { }
數(shù)據(jù)庫(kù)腳本文件
架構(gòu) (DDL) 腳本資源引用schema.sql
drop table if exists user; create table user( `id` int primary key auto_increment, `name` varchar(255) not null, `age` int not null, `gender` int not null );
數(shù)據(jù) (DML) 腳本資源引用
insert into user (id,name,age,gender) values (null, '張三',18,1); insert into user (id,name,age,gender) values (null, '李四',19,1); insert into user (id,name,age,gender) values (null, '王五',20,1); insert into user (id,name,age,gender) values (null, '李六',21,1);
配置文件
#---------服務(wù)器配置----------- server.port=8080 #---------數(shù)據(jù)源配置----------- spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:file:./data;AUTO_SERVER=TRUE spring.datasource.username=sa spring.datasource.password= #架構(gòu) (DDL) 腳本資源引用 spring.datasource.schema=classpath:db/schema.sql #數(shù)據(jù) (DML) 腳本資源引用 spring.datasource.data=classpath:db/data.sql #SQL腳本編碼 spring.datasource.sql-script-encoding=UTF-8 #初始化模式 spring.datasource.initialization-mode=ALWAYS #如果在初始化數(shù)據(jù)庫(kù)時(shí)發(fā)生錯(cuò)誤,是否停止 spring.datasource.continue-on-error=true #---------JPA配置------------- #要操作的目標(biāo)數(shù)據(jù)庫(kù) spring.jpa.database=h2 #控制臺(tái)顯示SQL語(yǔ)句 spring.jpa.show-sql=true #更新或者創(chuàng)建數(shù)據(jù)表結(jié)構(gòu) spring.jpa.hibernate.ddl-auto=update #物理命名策略的完全限定名稱 spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #是否在啟動(dòng)時(shí)初始化架構(gòu) spring.jpa.generate-ddl=true #----------H2配置-------------- #http://localhost:8080/h2-console spring.h2.console.path=/h2-console #啟用控制臺(tái) spring.h2.console.enabled=true
啟動(dòng)項(xiàng)目
訪問(wèn)H2數(shù)據(jù)庫(kù)
訪問(wèn):http://localhost:8080/h2-console
查看全部數(shù)據(jù)
由于設(shè)置了數(shù)據(jù)庫(kù)腳本,所以SpringBoot項(xiàng)目每次啟動(dòng)都會(huì)運(yùn)行一遍sql文件
#架構(gòu) (DDL) 腳本資源引用 spring.datasource.schema=classpath:db/schema.sql #數(shù)據(jù) (DML) 腳本資源引用 spring.datasource.data=classpath:db/data.sql #SQL腳本編碼 spring.datasource.sql-script-encoding=UTF-8 #初始化模式 spring.datasource.initialization-mode=ALWAYS #如果在初始化數(shù)據(jù)庫(kù)時(shí)發(fā)生錯(cuò)誤,是否停止 spring.datasource.continue-on-error=true
H2數(shù)據(jù)庫(kù)文件
數(shù)據(jù)庫(kù)文件位置通過(guò)spring.datasource.url
來(lái)指定
spring.datasource.url=jdbc:h2:file:./data;AUTO_SERVER=TRUE
運(yùn)行方式
1.在內(nèi)存中運(yùn)行
數(shù)據(jù)庫(kù)只在內(nèi)存中運(yùn)行,關(guān)閉連接后數(shù)據(jù)庫(kù)將被清空,適合測(cè)試環(huán)境
連接字符串:
jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
2.嵌入式
數(shù)據(jù)庫(kù)持久化存儲(chǔ)為單個(gè)文件
連接字符串:
jdbc:h2:file:~/.h2/DBName;AUTO_SERVER=TRUE
3.服務(wù)模式
H2支持三種服務(wù)模式:
- web server:此種運(yùn)行方式支持使用瀏覽器訪問(wèn)H2 Console
- TCP server:支持客戶端/服務(wù)器端的連接方式
- PG server:支持PostgreSQL客戶端
啟動(dòng)tcp服務(wù)連接字符串示例:
jdbc:h2:tcp://localhost/~/test 使用用戶主目錄 jdbc:h2:tcp://localhost//data/test 使用絕對(duì)路徑
4.連接字符串參數(shù)
DB_CLOSE_DELAY
:要求最后一個(gè)正在連接的連接斷開后,不要關(guān)閉數(shù)據(jù)庫(kù)MODE=MySQL
:兼容模式,H2兼容多種數(shù)據(jù)庫(kù),該值可以為:DB2、Derby、HSQLDB、MSSQLServer、MySQL、Oracle、PostgreSQLAUTO_RECONNECT=TRUE
:連接丟失后自動(dòng)重新連接AUTO_SERVER=TRUE
:?jiǎn)?dòng)自動(dòng)混合模式,允許開啟多個(gè)連接,該參數(shù)不支持在內(nèi)存中運(yùn)行模式TRACE_LEVEL_SYSTEM_OUT、TRACE_LEVEL_FILE
:輸出跟蹤日志到控制臺(tái)或文件, 取值0為OFF,1為ERROR(默認(rèn)值),2為INFO,3為DEBUGSET TRACE_MAX_FILE_SIZE mb
:設(shè)置跟蹤日志文件的大小,默認(rèn)為16M
到此這篇關(guān)于SpringBoot+Spring Data JPA整合H2數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)SpringBoot整合H2數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java注解如何基于Redission實(shí)現(xiàn)分布式鎖
這篇文章主要介紹了Java注解如何基于Redission實(shí)現(xiàn)分布式鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01使用Java實(shí)現(xiàn)MySQL數(shù)據(jù)鎖定的策略
在并發(fā)環(huán)境下,多個(gè)線程同時(shí)對(duì)MySQL數(shù)據(jù)庫(kù)進(jìn)行讀寫操作可能會(huì)導(dǎo)致數(shù)據(jù)沖突和不一致的問(wèn)題,為了解決這些并發(fā)沖突,我們可以采用數(shù)據(jù)鎖定策略來(lái)保證數(shù)據(jù)的一致性和完整性,下面將介紹如何使用Java實(shí)現(xiàn)MySQL數(shù)據(jù)鎖定策略,,需要的朋友可以參考下2023-08-08java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼
這篇文章主要介紹了java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(6)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07Java CountDownLatch應(yīng)用場(chǎng)景代碼實(shí)例
這篇文章主要介紹了Java CountDownLatch應(yīng)用場(chǎng)景代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Matplotlib可視化之自定義顏色繪制精美統(tǒng)計(jì)圖
matplotlib提供的所有繪圖都帶有默認(rèn)樣式.雖然這可以進(jìn)行快速繪圖,但有時(shí)可能需要自定義繪圖的顏色和樣式,以對(duì)繪制更加精美、符合審美要求的圖像.matplotlib的設(shè)計(jì)考慮到了此需求靈活性,很容易調(diào)整matplotlib圖形的樣式,需要的朋友可以參考下2021-06-06快速解決commons-fileupload組件無(wú)法處理自定義head信息的bug
問(wèn)題在于fileupload組件解析完自定義的head節(jié)點(diǎn)后,卻忘記傳遞到FileItemStreamImpl中了,稍作修訂,即可修正該bug2013-08-08