SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實踐
目標(biāo)
在SpringBoot中集成內(nèi)存數(shù)據(jù)庫H2.
為什么
像H2、hsqldb、derby、sqlite這樣的內(nèi)存數(shù)據(jù)庫,小巧可愛,做小型服務(wù)端演示程序,非常好用。最大特點就是不需要你另外安裝一個數(shù)據(jù)庫。
操作步驟
修改pom.xml文件
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
修改項目配置文件application.yml
spring: datasource: username: hsp password: 123456 url: jdbc:h2:file:./blogDB driver-class-name: org.h2.Driver schema: classpath:schema.sql data: classpath:data.sql initialization-mode: always continue-on-error: true h2: console: enabled: true path: /h2
添加初始化數(shù)據(jù)文件
建表腳本:schema.sql
CREATE TABLE `blog` ( `id` int AUTO_INCREMENT NOT NULL, `title` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) );
導(dǎo)入數(shù)據(jù)腳本:data.sql
insert into blog(id,title) values(1,'花生皮編程博客');
啟動類:HspApplication
@MapperScan({"cn.hsp.blog"}) @SpringBootApplication public class HspApplication { public static void main(String[] args) { SpringApplication.run(HspApplication.class, args); } }
Controller類:BlogController
@RestController @RequestMapping("/blog") public class BlogController { @Autowired private BlogMapper blogMapper; @GetMapping(value="/query") public List<Blog> query() { return blogMapper.query(); } }
Mapper類:BlogMapper
@Repository public interface BlogMapper { @Select(value = "select * from blog") List<Blog> query(); }
數(shù)據(jù)bean:Blog
@Data public class Blog { private int id; private String title; }
工程截圖
運行
運行HspApplication即可
效果
完整源代碼
https://gitee.com/hspbc/springboot_memdb
到此這篇關(guān)于SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實踐的文章就介紹到這了,更多相關(guān)SpringBoot集成H2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 構(gòu)建maven多模塊工程項目(詳細(xì)多圖)
這篇文章主要介紹了IntelliJ IDEA 構(gòu)建maven多模塊工程項目(詳細(xì)多圖),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06踩坑批量更新sql報錯,實際sql能夠正常執(zhí)行的問題
在項目工程遷移過程中,遇到了一個批量更新接口在新工程中報錯的問題,通過分析,排除了代碼錯誤的可能,最終發(fā)現(xiàn)是由于數(shù)據(jù)庫連接配置不當(dāng)導(dǎo)致的,在jdbc連接字符串中加入allowMultiQueries=true參數(shù)后,問題得以解決,這個參數(shù)的作用是允許SQL批量執(zhí)行2022-12-12SpringBoot實現(xiàn)網(wǎng)站的登陸注冊邏輯記錄
登陸注冊功能是我們?nèi)粘i_發(fā)中經(jīng)常遇到的一個功能,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)網(wǎng)站的登陸注冊邏輯的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10Java實現(xiàn)將列表數(shù)據(jù)導(dǎo)出為PDF文件并添加水印
這篇文章主要為大家詳細(xì)介紹了如何使用Java實現(xiàn)把列表數(shù)據(jù)導(dǎo)出為PDF文件,同時加上PDF水印,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02Springboot Retry組件@Recover失效問題解決方法
在使用springboot的retry模塊時,你是否出現(xiàn)過@Recover注解失效的問題呢?不用擔(dān)心,這篇文章就來告訴你解決@Recover失效的辦法,需要的小伙伴可以參考一下2021-11-11