溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot怎么整合SQLite數(shù)據(jù)庫

發(fā)布時間:2023-03-14 11:11:56 來源:億速云 閱讀:152 作者:iii 欄目:開發(fā)技術(shù)

這篇“SpringBoot怎么整合SQLite數(shù)據(jù)庫”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringBoot怎么整合SQLite數(shù)據(jù)庫”文章吧。

前言

SQLite是一個進程內(nèi)的庫,實現(xiàn)了自給自足的、無服務(wù)器的、零配置的、事務(wù)性的 SQL 數(shù)據(jù)庫引擎。它是一個零配置的數(shù)據(jù)庫,這意味著與其他數(shù)據(jù)庫不一樣,您不需要在系統(tǒng)中配置。

就像其他數(shù)據(jù)庫,SQLite 引擎不是一個獨立的進程,可以按應(yīng)用程序需求進行靜態(tài)或動態(tài)連接。SQLite 直接訪問其存儲文件。

功能特性

  1. ACID事務(wù)

  2. 零配置 – 無需安裝和管理配置

  3. 儲存在單一磁盤文件中的一個完整的數(shù)據(jù)庫

  4. 數(shù)據(jù)庫文件可以在不同字節(jié)順序的機器間自由的共享

  5. 支持數(shù)據(jù)庫大小至2TB

  6. 足夠小, 大致13萬行C代碼, 4.43M

  7. 比一些流行的數(shù)據(jù)庫在大部分普通數(shù)據(jù)庫操作要快

  8. 簡單, 輕松的API

  9. 包含TCL綁定, 同時通過Wrapper支持其他語言的綁定

  10. 良好注釋的源代碼, 并且有著90%以上的測試覆蓋率

  11. 獨立: 沒有額外依賴

  12. 源碼完全的開源, 你可以用于任何用途, 包括出售它

  13. 支持多種開發(fā)語言,C, C++, PHP, Perl, Java, C#,Python, Ruby等

1、pom.xml

    <dependencies>
        <!--web應(yīng)用基本環(huán)境配置 -->
        <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只需要關(guān)聯(lián)一個.db文件,就能實現(xiàn)數(shù)據(jù)庫的連接操作。  

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

在如下位置,手動創(chuàng)建一個 test.db 空文件

SpringBoot怎么整合SQLite數(shù)據(jù)庫

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、插入數(shù)據(jù)
        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、插入數(shù)據(jù)
        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);
    }
 
}

以上就是關(guān)于“SpringBoot怎么整合SQLite數(shù)據(jù)庫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI