SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫的示例代碼
前言
- H2數(shù)據(jù)庫是一個開源的關(guān)系型數(shù)據(jù)庫。H2采用java語言編寫,不受平臺的限制,同時支持網(wǎng)絡(luò)版和嵌入式版本,有比較好的兼容性,支持相當標準的sql標準
- 提供JDBC、ODBC訪問接口,提供了非常友好的基于web的數(shù)據(jù)庫管理界面
官網(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 "刪除成功";
}實體類
@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ù)庫腳本文件
架構(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ù)庫時發(fā)生錯誤,是否停止 spring.datasource.continue-on-error=true #---------JPA配置------------- #要操作的目標數(shù)據(jù)庫 spring.jpa.database=h2 #控制臺顯示SQL語句 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 #是否在啟動時初始化架構(gòu) spring.jpa.generate-ddl=true #----------H2配置-------------- #http://localhost:8080/h2-console spring.h2.console.path=/h2-console #啟用控制臺 spring.h2.console.enabled=true
啟動項目

訪問H2數(shù)據(jù)庫
訪問:http://localhost:8080/h2-console

查看全部數(shù)據(jù)
由于設(shè)置了數(shù)據(jù)庫腳本,所以SpringBoot項目每次啟動都會運行一遍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ù)庫時發(fā)生錯誤,是否停止 spring.datasource.continue-on-error=true

H2數(shù)據(jù)庫文件
數(shù)據(jù)庫文件位置通過spring.datasource.url來指定
spring.datasource.url=jdbc:h2:file:./data;AUTO_SERVER=TRUE

運行方式
1.在內(nèi)存中運行
數(shù)據(jù)庫只在內(nèi)存中運行,關(guān)閉連接后數(shù)據(jù)庫將被清空,適合測試環(huán)境
連接字符串:
jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
2.嵌入式
數(shù)據(jù)庫持久化存儲為單個文件
連接字符串:
jdbc:h2:file:~/.h2/DBName;AUTO_SERVER=TRUE
3.服務(wù)模式
H2支持三種服務(wù)模式:
- web server:此種運行方式支持使用瀏覽器訪問H2 Console
- TCP server:支持客戶端/服務(wù)器端的連接方式
- PG server:支持PostgreSQL客戶端
啟動tcp服務(wù)連接字符串示例:
jdbc:h2:tcp://localhost/~/test 使用用戶主目錄 jdbc:h2:tcp://localhost//data/test 使用絕對路徑
4.連接字符串參數(shù)
DB_CLOSE_DELAY:要求最后一個正在連接的連接斷開后,不要關(guān)閉數(shù)據(jù)庫MODE=MySQL:兼容模式,H2兼容多種數(shù)據(jù)庫,該值可以為:DB2、Derby、HSQLDB、MSSQLServer、MySQL、Oracle、PostgreSQLAUTO_RECONNECT=TRUE:連接丟失后自動重新連接AUTO_SERVER=TRUE:啟動自動混合模式,允許開啟多個連接,該參數(shù)不支持在內(nèi)存中運行模式TRACE_LEVEL_SYSTEM_OUT、TRACE_LEVEL_FILE:輸出跟蹤日志到控制臺或文件, 取值0為OFF,1為ERROR(默認值),2為INFO,3為DEBUGSET TRACE_MAX_FILE_SIZE mb:設(shè)置跟蹤日志文件的大小,默認為16M
到此這篇關(guān)于SpringBoot+Spring Data JPA整合H2數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)SpringBoot整合H2數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA 筆記 ClassLoader.getResourceAsStream() 與 Class.getResourc
這篇文章主要介紹了JAVA 筆記 ClassLoader.getResourceAsStream() 與 Class.getResourceAsStream()的區(qū)別,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
Java中ThreadLocal避免內(nèi)存泄漏的方法詳解
ThreadLocal是Java中的一個線程本地存儲機制,它允許每個線程擁有一個獨立的本地存儲空間,用于存儲該線程的變量,本文主要介紹了ThreadLocal如何避免內(nèi)存泄漏,需要的朋友可以參考下2023-05-05
如何用匿名內(nèi)部類實現(xiàn) Java 同步回調(diào)
這篇文章主要介紹了如何用匿名內(nèi)部類實現(xiàn) Java 同步回調(diào),幫助大家更好的理解和學習Java,感興趣的朋友可以了解下2020-10-10
Java Swing實現(xiàn)的定制TextField功能示例
這篇文章主要介紹了Java Swing實現(xiàn)的定制TextField功能,結(jié)合實例形式分析了java swing組件TextField相關(guān)屬性功能與設(shè)置操作技巧,需要的朋友可以參考下2018-01-01
JPA如何使用entityManager執(zhí)行SQL并指定返回類型
這篇文章主要介紹了JPA使用entityManager執(zhí)行SQL并指定返回類型的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

