小于博客 小于博客
首页
  • Java学习笔记
  • Docker专区
  • 实战教程
  • Shell
  • 内存数据库
  • Vue学习笔记
  • Nginx
  • Php
  • CentOS
  • Docker
  • Gitlab
  • GitHub
  • MySql
  • MongoDB
  • OpenVPN
  • 配置文件详解
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 电影音乐
  • 效率工具
  • 博客相关
  • 最佳实践
  • 迎刃而解
  • 学习周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

小于博客

行者常至,为者常成
首页
  • Java学习笔记
  • Docker专区
  • 实战教程
  • Shell
  • 内存数据库
  • Vue学习笔记
  • Nginx
  • Php
  • CentOS
  • Docker
  • Gitlab
  • GitHub
  • MySql
  • MongoDB
  • OpenVPN
  • 配置文件详解
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 电影音乐
  • 效率工具
  • 博客相关
  • 最佳实践
  • 迎刃而解
  • 学习周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • Java学习笔记

  • Docker专区

  • Shell编程

  • 实战教程

  • 内存数据库

    • h2数据库全面解析
    • SpringBoot配置使用H2数据库的简单教程
    • SpringBoot整合SQLite
      • 1.pom.xml
      • 2.application.properties
      • 3.测试代码
  • Vue学习笔记

  • 编程世界
  • 内存数据库
小于博客
2024-03-07
目录

SpringBoot整合SQLite

SpringBoot整合SQLite

SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库不一样,您不需要在系统中配置。

就像其他数据库,SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。

功能特性

  • 1.ACID事务
  • 2.零配置 – 无需安装和管理配置
  • 3.储存在单一磁盘文件中的一个完整的数据库
  • 4.数据库文件可以在不同字节顺序的机器间自由的共享
  • 5.支持数据库大小至2TB
  • 6.足够小, 大致13万行C代码, 4.43M
  • 7.比一些流行的数据库在大部分普通数据库操作要快
  • 8.简单, 轻松的API
  • 9.包含TCL绑定, 同时通过Wrapper支持其他语言的绑定
  • 10.良好注释的源代码, 并且有着90%以上的测试覆盖率
  • 11.独立: 没有额外依赖
  • 12.源码完全的开源, 你可以用于任何用途, 包括出售它
  • 13.支持多种开发语言,C, C++, PHP, Perl, Java, C#,Python, Ruby等

# 1.pom.xml

<dependencies>
        <!--web应用基本环境配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--sqlite-->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 2.application.properties

SQLite只需要关联一个.db文件,就能实现数据库的连接操作。

    spring.datasource.driver-class-name=org.sqlite.JDBC
    #绝对位置配置方式
    #spring.datasource.url=jdbc:sqlite:E:/db/test.db
    #相对位置配置方式
    spring.datasource.url=jdbc:sqlite::resource:db/test.db
    #我用的这种,在项目根目录建立db/test.db即可
    spring.datasource.url=jdbc:sqlite:db/test.db
1
2
3
4
5
6
7

在如下位置,手动创建一个 test.db 空文件

# 3.测试代码

    @Autowired
    private JdbcTemplate jdbcTemplate;
1
2
            // 1、建表 DDL
        String createUser = "create table user(" +
                "id integer primary key autoincrement," +
                "name varchar(20)," +
                "age integer" +
                ")";
                jdbcTemplate.update(createUser);
                // 2、插入数据
                String insertUserData = "insert into user(name,age) values ('张三',18),('李四',20)";
                jdbcTemplate.update(insertUserData);
                // 3、查询语句
                String selectUserData = "select * from user";
                List<Map<String, Object>> list = jdbcTemplate.queryForList(selectUserData);
        for (Map<String, Object> map : list) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.println(entry.getKey() + "=" + entry.getValue());
        }
        }
        // 5、删除整张表
        String dropTable = "drop table user";
        jdbcTemplate.update(dropTable);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

完整测试代码

package com.study.myweb;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
 
import java.util.List;
import java.util.Map;
 
 
@SpringBootApplication
public class MyWebApplication implements CommandLineRunner {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    public static void main(String[] args) {
        SpringApplication.run(MyWebApplication.class, args);
    }
 
    @Override
    public void run(String... args) throws Exception {
        // 1、建表 DDL
        String createUser = "create table user(" +
                "id integer primary key autoincrement," +
                "name varchar(20)," +
                "age integer" +
                ")";
        jdbcTemplate.update(createUser);
        // 2、插入数据
        String insertUserData = "insert into user(name,age) values ('张三',18),('李四',20)";
        jdbcTemplate.update(insertUserData);
        // 3、查询语句
        String selectUserData = "select * from user";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(selectUserData);
        for (Map<String, Object> map : list) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
            }
        }
        // 4、删除整张表
        String dropTable = "drop table user";
        jdbcTemplate.update(dropTable);
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

← SpringBoot配置使用H2数据库的简单教程 初识Vue→

最近更新
01
SpringBoot 快速实现 api 加密!
03-21
02
SpringBoot配置使用H2数据库的简单教程
02-21
03
h2数据库全面解析
02-21
更多文章>
Theme by Vdoing | Copyright © 2017-2024 | 点击查看十年之约 | 豫ICP备2022014539号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式