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

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

