SpringBoot整合SQLite數(shù)據(jù)庫全過程
前言
SQLite是一個進(jìn)程內(nèi)的庫,實現(xiàn)了自給自足的、無服務(wù)器的、零配置的、事務(wù)性的 SQL 數(shù)據(jù)庫引擎。它是一個零配置的數(shù)據(jù)庫,這意味著與其他數(shù)據(jù)庫不一樣,您不需要在系統(tǒng)中配置。
就像其他數(shù)據(jù)庫,SQLite 引擎不是一個獨立的進(jìn)程,可以按應(yīng)用程序需求進(jìn)行靜態(tài)或動態(tài)連接。SQLite 直接訪問其存儲文件。
功能特性
- ACID事務(wù)
- 零配置 – 無需安裝和管理配置
- 儲存在單一磁盤文件中的一個完整的數(shù)據(jù)庫
- 數(shù)據(jù)庫文件可以在不同字節(jié)順序的機器間自由的共享
- 支持?jǐn)?shù)據(jù)庫大小至2TB
- 足夠小, 大致13萬行C代碼, 4.43M
- 比一些流行的數(shù)據(jù)庫在大部分普通數(shù)據(jù)庫操作要快
- 簡單, 輕松的API
- 包含TCL綁定, 同時通過Wrapper支持其他語言的綁定
- 良好注釋的源代碼, 并且有著90%以上的測試覆蓋率
- 獨立: 沒有額外依賴
- 源碼完全的開源, 你可以用于任何用途, 包括出售它
- 支持多種開發(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 空文件

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);
}
}總結(jié)
到此這篇關(guān)于SpringBoot整合SQLite數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)SpringBoot整合SQLite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea自帶database連接mysql失敗問題的解決辦法
在IDEA?帶的數(shù)據(jù)庫連接?具中,可以連接MySQL數(shù)據(jù)庫,但是有的時候連接出現(xiàn)錯誤,連接不上數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于idea自帶database連接mysql失敗問題的解決辦法,需要的朋友可以參考下2023-06-06
springboot多環(huán)境配置文件及自定義配置文件路徑詳解
這篇文章主要介紹了springboot多環(huán)境配置文件及自定義配置文件路徑,文中給大家介紹了classpath的基本概念講解及自定義springboot配置文件路徑的相關(guān)知識,需要的朋友可以參考下2023-02-02
使用spring boot開發(fā)時java對象和Json對象轉(zhuǎn)換的問題
這篇文章主要介紹了使用spring boot開發(fā)時java對象和Json對象轉(zhuǎn)換的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
JavaCV實戰(zhàn)之調(diào)用攝像頭基礎(chǔ)詳解
這篇文章主要介紹了使用JavaCV框架對攝像頭進(jìn)行各種處理的基礎(chǔ)理論詳解,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)JavaCV有一定的幫助,需要的可以了解一下2022-01-01
Spring中的spring.factories文件用法(Spring如何加載第三方Bean)
這篇文章主要介紹了Spring中的spring.factories文件用法(Spring如何加載第三方Bean),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot+Netty實現(xiàn)簡單聊天室的示例代碼
這篇文章主要介紹了如何利用SpringBoot Netty實現(xiàn)簡單聊天室,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)SpringBoot有一定幫助,感興趣的同學(xué)可以了解一下2022-02-02

