温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot怎么整合SQLite数据库

发布时间:2023-03-14 11:11:56 来源:亿速云 阅读:110 作者:iii 栏目:开发技术

这篇“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>

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

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

SpringBoot怎么整合SQLite数据库

3、测试代码

    @Autowired
    private JdbcTemplate jdbcTemplate;
        // 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);

完整测试代码

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);
    }
 
}

以上就是关于“SpringBoot怎么整合SQLite数据库”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI