SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實踐
目標
在SpringBoot中集成內(nèi)存數(shù)據(jù)庫hsqldb.
為什么
像H2、hsqldb、derby、sqlite這樣的內(nèi)存數(shù)據(jù)庫,小巧可愛,做小型服務(wù)端演示程序,非常好用。最大特點就是不需要你另外安裝一個數(shù)據(jù)庫。
操作步驟
修改pom.xml文件
<dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> </dependency>
修改項目配置文件application.yml
spring: datasource: username: hsp password: 123456 url: jdbc:hsqldb:mem://localhost/blogdb;shutdown=true driver-class-name: org.hsqldb.jdbcDriver schema: classpath:schema.sql data: classpath:data.sql initialization-mode: always continue-on-error: true
添加初始化數(shù)據(jù)文件
建表腳本:schema.sql
CREATE TABLE blog ( id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY, title varchar(255) DEFAULT NULL, );
導(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即可
效果
總結(jié)
1.當前的hsqldb在配置的時候一定要在url中添加shutdown=true;否者會出現(xiàn)錯誤:
org.hsqldb.HsqlException: user lacks privilege or object not found: USER(表不存在的錯誤)
2.當前的hsqldb中的實體類只需要@Entity注解,@Id和@GeneratedValue
3.dao層需要繼承jpa的或者reposity即可
完整源代碼
https://gitee.com/hspbc/springboot_memdb
到此這篇關(guān)于SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實踐的文章就介紹到這了,更多相關(guān)SpringBoot集成hsqldb內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
這篇文章主要介紹了Spring Cloud Gateway Hystrix fallback獲取異常信息的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07實例解析Json反序列化之ObjectMapper(自定義實現(xiàn)反序列化方法)
這篇文章主要介紹了實例解析Json反序列化之ObjectMapper,json自定義序列化的方法,需要的朋友可以了解下。2017-09-09java 學(xué)習筆記(入門篇)_多選擇結(jié)構(gòu)switch語句
在java中為多路分支選擇流程專門提供了switch語句,switch語句根據(jù)一個表達式的值,選擇運行多個操作中的一個,感興趣的朋友可以了解下2013-01-01Java實現(xiàn)圖片上傳至服務(wù)器功能(FTP協(xié)議)
這篇文章主要為大家詳細介紹了Java實現(xiàn)圖片上傳至服務(wù)器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06