SpringBoot集成內(nèi)存數(shù)據(jù)庫Sqlite的實(shí)踐
目標(biāo)
在SpringBoot中集成內(nèi)存數(shù)據(jù)庫Sqlite.
為什么
像H2、hsqldb、derby、sqlite這樣的內(nèi)存數(shù)據(jù)庫,小巧可愛,做小型服務(wù)端演示程序,非常好用。最大特點(diǎn)就是不需要你另外安裝一個數(shù)據(jù)庫。
操作步驟
1、修改pom.xml文件
<dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.36.0.3</version> </dependency>
2、修改項(xiàng)目配置文件application.yml
spring: datasource: username: hsp password: 123456 url: jdbc:derby:blogDb;create=true driver-class-name: org.apache.derby.jdbc.EmbeddedDriver schema: classpath:schema.sql data: classpath:data.sql initialization-mode: always continue-on-error: true
3、添加初始化數(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,'花生皮編程博客');
4、啟動類:HspApplication
@MapperScan({"cn.hsp.blog"}) @SpringBootApplication public class HspApplication { public static void main(String[] args) { SpringApplication.run(HspApplication.class, args); } }
5、Controller類:BlogController
@RestController @RequestMapping("/blog") public class BlogController { @Autowired private BlogMapper blogMapper; @GetMapping(value="/query") public List<Blog> query() { return blogMapper.query(); } }
6、Mapper類:BlogMapper
@Repository public interface BlogMapper { @Select(value = "select * from blog") List<Blog> query(); }
7、數(shù)據(jù)bean:Blog
@Data public class Blog { private int id; private String title; }
工程截圖
運(yùn)行
運(yùn)行HspApplication即可
效果
完整源代碼
https://gitee.com/hspbc/springboot_memdb
到此這篇關(guān)于SpringBoot集成內(nèi)存數(shù)據(jù)庫Sqlite的實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot集成Sqlite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaSE中compare、compareTo的區(qū)別
本文主要介紹了JavaSE中compare、compareTo的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05springboot2.3之后hibernate-validator依賴缺失【踩坑】
這篇文章主要介紹了springboot2.3之后hibernate-validator依賴缺失【踩坑】,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Springboot FeignClient調(diào)用Method has too m
本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12logback-spring.xml的配置及示例詳解(直接復(fù)制粘貼可用)
在使用logback作為日志框架時,可以創(chuàng)建一個名為logback-spring.xml的配置文件來自定義日志輸出的格式和方式,下面這篇文章主要給大家介紹了關(guān)于logback-spring.xml的配置及示例詳解的相關(guān)資料,文中的代碼直接復(fù)制粘貼可用,需要的朋友可以參考下2024-01-01java實(shí)現(xiàn)簡單網(wǎng)絡(luò)象棋游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單網(wǎng)絡(luò)象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12@TransactionalEventListener的使用和實(shí)現(xiàn)原理分析
這篇文章主要介紹了@TransactionalEventListener的使用和實(shí)現(xiàn)原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java實(shí)現(xiàn)統(tǒng)計字符串出現(xiàn)的次數(shù)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)統(tǒng)計字符串出現(xiàn)的次數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10