SpringBoot集成内存数据库Sqlite的实践

目录

  • 目标
  • 为什么
  • 操作步骤
  • 工程截图
  • 运行
  • 效果
  • 完整源代码

目标
在SpringBoot中集成内存数据库Sqlite.


为什么
像H2、hsqldb、derby、sqlite这样的内存数据库,小巧可爱,做小型服务端演示程序,非常好用。最大特点就是不需要你另外安装一个数据库。


操作步骤 1、修改pom.xml文件
org.xerialsqlite-jdbc3.36.0.3

2、修改项目配置文件application.yml
spring:datasource:username: hsppassword: 123456url: jdbc:derby:blogDb; create=truedriver-class-name: org.apache.derby.jdbc.EmbeddedDriverschema: classpath:schema.sqldata: classpath:data.sqlinitialization-mode: alwayscontinue-on-error: true

3、添加初始化数据文件
建表脚本:schema.sql
CREATE TABLE `blog` (`id` int AUTO_INCREMENT NOT NULL,`title` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`));

导入数据脚本:data.sql
insert into blog(id,title) values(1,'花生皮编程博客');

4、启动类:HspApplication
@MapperScan({"cn.hsp.blog"})@SpringBootApplicationpublic class HspApplication { public static void main(String[] args) {SpringApplication.run(HspApplication.class, args); }}

5、Controller类:BlogController
@RestController@RequestMapping("/blog")public class BlogController {@Autowiredprivate BlogMapper blogMapper; @GetMapping(value="https://www.it610.com/query")public List query(){return blogMapper.query(); }}

6、Mapper类:BlogMapper
@Repositorypublic interface BlogMapper {@Select(value = "https://www.it610.com/article/select * from blog")List query(); }

7、数据bean:Blog
@Datapublic class Blog {private int id; private String title; }


工程截图 SpringBoot集成内存数据库Sqlite的实践
文章图片


运行
运行HspApplication即可


效果 SpringBoot集成内存数据库Sqlite的实践
文章图片


完整源代码
https://gitee.com/hspbc/springboot_memdb
【SpringBoot集成内存数据库Sqlite的实践】到此这篇关于SpringBoot集成内存数据库Sqlite的实践的文章就介绍到这了,更多相关SpringBoot集成Sqlite内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读