SpringBoot整合SQLite詳細(xì)過程
一、SQLite是什么
SQLite是一個不需要服務(wù)、不需要配置、不需要外部依賴的開源SQL輕量級數(shù)據(jù)庫。
- 不需要服務(wù)器:如MySQL安裝后,會在操作系統(tǒng)中創(chuàng)建一個進(jìn)程mysqld.exe,而SQLite不需要創(chuàng)建。
- 不需要配置:如MySQL安裝后,需要配置端口、用戶名、密碼等,而SQLite不需要,它是存儲在磁盤上的文件,不需要安裝。
- 不需要外部依賴:SQLite是自給自足的,不需要任何外部的依賴。
二、SQLite優(yōu)點
sqlite支持MySQL擁有的大多數(shù)功能。
允許多個進(jìn)程\線程安全訪問,支持事務(wù)機(jī)制。
允許多門開發(fā)語言調(diào)用,支持JDBC。
支持Windows、Linux等多個操作系統(tǒng)上運行。
三、SpringBoot整合SQLite
準(zhǔn)備工作
1.創(chuàng)建一個文件(后綴名為.db)
【建議】將這個文件夾放到項目所在的目錄。
這個文件的相當(dāng)于MySQL中創(chuàng)建一個庫。
以下是我創(chuàng)建的文件,

2.使用IDEA連接



顯示這個就是連接成功了。
3.創(chuàng)建表


整合
1.導(dǎo)入依賴
<!-- SQLite JDBC Driver -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.34.0</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql JDBC Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>2.配置文件
spring:
datasource:
druid:
login-timeout: 30000
dynamic:
primary: sqlite
datasource:
mysql:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC
username:
password:
sqlite:
url: jdbc:sqlite:D:\xxx\user.db
driver-class-name: org.sqlite.JDBC
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: true
map-underscore-to-camel-case: false
global-config:
db-config:
logic-delete-field: isDeleted # 全局邏輯刪除的實體字段名(since 3.3.0,配置后可以忽略不配置步驟2)
logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1)
logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0)解釋

3.代碼
我剛才創(chuàng)建的表有三個字段,id、name、age
實體類
@TableName(value ="user")
@Data
public class User implements Serializable {
/**
*
*/
private Integer id;
/**
*
*/
private String name;
/**
*
*/
private Integer age;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}mapper層
@Mapper
public interface UserMapper extends BaseMapper<User> {
}Service層
public interface UserService extends IService<User> {
}實現(xiàn)類impl
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
implements UserService {
}控制層
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/getAll")
public List<User> getAll() {
return userService.list();
}
}展示結(jié)果

以上我們就整合完了,寫了一個小小的demo。希望能夠幫助到你。
到此這篇關(guān)于SpringBoot整合SQLite(詳細(xì)講解)的文章就介紹到這了,更多相關(guān)SpringBoot整合SQLite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringBoot整合ElasticSearch遇到的連接問題
這篇文章主要介紹了解決SpringBoot整合ElasticSearch遇到的連接問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Windows下安裝Maven的詳細(xì)教程(含鏡像與本地倉庫配置)
Maven?是?Java?生態(tài)系統(tǒng)中不可或缺的項目管理和構(gòu)建自動化工具,本文詳細(xì)介紹了在Windows系統(tǒng)上安裝和配置Maven的完整流程,希望對大家有所幫助2026-04-04
SpringBoot可視化接口開發(fā)工具magic-api的簡單使用教程
作為Java后端開發(fā),平時開發(fā)API接口的時候經(jīng)常需要定義Controller、Service、Dao、Mapper、XML、VO等Java對象。有沒有什么辦法可以讓我們不寫這些代碼,直接操作數(shù)據(jù)庫生成API接口呢?今天給大家推薦一款工具magic-api,來幫我們實現(xiàn)這個小目標(biāo)!2021-06-06
java循環(huán)刪除List元素報錯的原因分析與解決
大家在工作中應(yīng)該都會遇到從List集合中刪除某一個或多個元素的業(yè)務(wù)場景,相信大家都會避開在循環(huán)里面刪除元素,使用其他方式處理,這是為什么呢,下面小編就來和大家詳細(xì)聊聊2023-11-11

