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

作者:花生皮编程 时间:2024-01-15 10:24:11 

目录
  • 目标

  • 为什么

  • 操作步骤

  • 工程截图

  • 运行

  • 效果

  • 完整源代码

目标

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

为什么

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

操作步骤

1、修改pom.xml文件


<dependency>
  <groupId>org.xerial</groupId>
  <artifactId>sqlite-jdbc</artifactId>
  <version>3.36.0.3</version>
</dependency>

2、修改项目配置文件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、添加初始化数据文件

建表脚本: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"})
@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、数据bean:Blog


@Data
public class Blog {
   private int id;
   private String title;
}

工程截图

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

运行

运行HspApplication即可

效果

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

完整源代码

https://gitee.com/hspbc/springboot_memdb

来源:https://juejin.cn/post/7004793101520748575

标签:SpringBoot,Sqlite
0
投稿

猜你喜欢

  • python中数组和列表的简单实例

    2021-04-15 20:04:42
  • DWCS3-CSS布局之二CSS规则定义

    2008-06-16 13:36:00
  • python中reload重载实例用法

    2021-05-12 07:02:09
  • Python二维列表的创建、转换以及访问详解

    2022-08-09 16:03:15
  • SqlServer常用函数及时间处理小结

    2024-01-16 14:32:02
  • numpy.random.shuffle打乱顺序函数的实现

    2021-02-04 07:19:34
  • Python求算数平方根和约数的方法汇总

    2021-12-08 18:03:39
  • Python Pandas学习之基本数据操作详解

    2021-11-07 17:46:56
  • python 使用cycle构造无限循环迭代器

    2022-08-19 13:09:10
  • vue实现表单录入小案例

    2024-05-09 15:11:07
  • 如何应对SQL Server数据库崩溃

    2008-11-24 17:25:00
  • matplotlib subplot绘制多个子图的方法示例

    2021-12-18 14:49:50
  • python使用xauth方式登录饭否网然后发消息

    2021-04-18 08:11:54
  • 用Dreamweaver MX制作导航下拉菜单

    2009-05-29 18:37:00
  • Python+requests+unittest执行接口自动化测试详情

    2023-07-30 15:08:37
  • Python实现K-means聚类算法并可视化生成动图步骤详解

    2021-06-20 23:10:40
  • python实现截取屏幕保存文件,删除N天前截图的例子

    2021-09-19 18:13:49
  • 解决IIS出现Active Server Pages错误“ASP 0201”

    2009-05-25 18:04:00
  • sqlserver 触发器学习(实现自动编号)

    2024-01-24 23:03:33
  • python实现12306火车票查询器

    2021-04-07 16:05:58
  • asp之家 网络编程 m.aspxhome.com