SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫的示例代碼
前言
- H2數(shù)據(jù)庫是一個開源的關系型數(shù)據(jù)庫。H2采用java語言編寫,不受平臺的限制,同時支持網(wǎng)絡版和嵌入式版本,有比較好的兼容性,支持相當標準的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ù)庫腳本文件
架構 (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);
配置文件
#---------服務器配置----------- 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= #架構 (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ù)表結構 spring.jpa.hibernate.ddl-auto=update #物理命名策略的完全限定名稱 spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #是否在啟動時初始化架構 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ù)據(jù)庫腳本,所以SpringBoot項目每次啟動都會運行一遍sql文件
#架構 (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)存中運行,關閉連接后數(shù)據(jù)庫將被清空,適合測試環(huán)境
連接字符串:
jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
2.嵌入式
數(shù)據(jù)庫持久化存儲為單個文件
連接字符串:
jdbc:h2:file:~/.h2/DBName;AUTO_SERVER=TRUE
3.服務模式
H2支持三種服務模式:
- web server:此種運行方式支持使用瀏覽器訪問H2 Console
- TCP server:支持客戶端/服務器端的連接方式
- PG server:支持PostgreSQL客戶端
啟動tcp服務連接字符串示例:
jdbc:h2:tcp://localhost/~/test 使用用戶主目錄 jdbc:h2:tcp://localhost//data/test 使用絕對路徑
4.連接字符串參數(shù)
DB_CLOSE_DELAY
:要求最后一個正在連接的連接斷開后,不要關閉數(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
:設置跟蹤日志文件的大小,默認為16M
到此這篇關于SpringBoot+Spring Data JPA整合H2數(shù)據(jù)庫的文章就介紹到這了,更多相關SpringBoot整合H2數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java注解如何基于Redission實現(xiàn)分布式鎖
這篇文章主要介紹了Java注解如何基于Redission實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01使用Java實現(xiàn)MySQL數(shù)據(jù)鎖定的策略
在并發(fā)環(huán)境下,多個線程同時對MySQL數(shù)據(jù)庫進行讀寫操作可能會導致數(shù)據(jù)沖突和不一致的問題,為了解決這些并發(fā)沖突,我們可以采用數(shù)據(jù)鎖定策略來保證數(shù)據(jù)的一致性和完整性,下面將介紹如何使用Java實現(xiàn)MySQL數(shù)據(jù)鎖定策略,,需要的朋友可以參考下2023-08-08java中用數(shù)組實現(xiàn)環(huán)形隊列的示例代碼
這篇文章主要介紹了java中用數(shù)組實現(xiàn)環(huán)形隊列的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04Matplotlib可視化之自定義顏色繪制精美統(tǒng)計圖
matplotlib提供的所有繪圖都帶有默認樣式.雖然這可以進行快速繪圖,但有時可能需要自定義繪圖的顏色和樣式,以對繪制更加精美、符合審美要求的圖像.matplotlib的設計考慮到了此需求靈活性,很容易調(diào)整matplotlib圖形的樣式,需要的朋友可以參考下2021-06-06快速解決commons-fileupload組件無法處理自定義head信息的bug
問題在于fileupload組件解析完自定義的head節(jié)點后,卻忘記傳遞到FileItemStreamImpl中了,稍作修訂,即可修正該bug2013-08-08