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)文章
Spring?Boot?集成并開發(fā)?Sa-token示例詳解
Sa-token是一款高可用的權(quán)限認(rèn)證框架,他帶我們用最簡化的配置完成用?spring?security?需要進(jìn)行大量配置的才能完成的工作,這篇文章主要介紹了Spring?Boot?集成并開發(fā)?Sa-token,需要的朋友可以參考下2023-06-06springboot 定時任務(wù)@Scheduled實現(xiàn)解析
這篇文章主要介紹了springboot 定時任務(wù)@Scheduled實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09如何將復(fù)雜SQL轉(zhuǎn)換成Java對象的實例講解
轉(zhuǎn)換復(fù)雜SQL到Java代碼,我們需要確定數(shù)據(jù)庫連接方式和工具,使用JDBC的API來連接數(shù)據(jù)庫、執(zhí)行SQL語句,復(fù)雜SQL語句可以被拆分為多個步驟,每個步驟執(zhí)行一個特定的操作,通過將SQL語句拆分為多個步驟,我們可以更好地理解復(fù)雜SQL的邏輯,并且更容易將其轉(zhuǎn)換為Java代碼2024-05-05Mybatis高級映射、動態(tài)SQL及獲得自增主鍵的解析
MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis。這篇文章主要介紹了Mybatis高級映射、動態(tài)SQL及獲得自增主鍵的相關(guān)資料,需要的朋友可以參考下2016-11-11Java中JSON字符串進(jìn)行各種轉(zhuǎn)換的方法小結(jié)
Gson和Hutool的JSONUtil都是常用的用于處理JSON數(shù)據(jù)的工具庫,它們提供了簡單易用的API來進(jìn)行JSON字符串的解析、轉(zhuǎn)換和操作,下面就跟隨小編一起學(xué)習(xí)一下如果使用他們實現(xiàn)JSON字符串的各種轉(zhuǎn)換吧2024-01-01