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

2.使用IDEA連接



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


整合
1.導入依賴
<!-- 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 # 邏輯已刪除值(默認為 1)
logic-not-delete-value: 0 # 邏輯未刪除值(默認為 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。
總結(jié)
到此這篇關(guān)于SpringBoot整合SQLite的文章就介紹到這了,更多相關(guān)SpringBoot整合SQLite內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用
這篇文章主要介紹了Java的命名方法和變量的值及其引用,是Java入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
Idea設(shè)置全局highlighting?level為Syntax問題
這篇文章主要介紹了Idea設(shè)置全局highlighting?level為Syntax問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
Collections工具類_動力節(jié)點Java學院整理
Collections工具類提供了大量針對Collection/Map的操作。這篇文章主要介紹了Collections工具類_動力節(jié)點Java學院整理,需要的朋友可以參考下2017-04-04
Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費消息的實例代碼
本文介紹了在SpringBoot中如何使用RabbitMQ進行消息的生產(chǎn)和消費,詳細闡述了RabbitMQ中交換機的作用和類型,包括直連交換機、主題交換機、扇出交換機和頭交換機,并解釋了各自的消息路由機制,感興趣的朋友一起看看吧2024-10-10
SpringBoot做junit測試的時候獲取不到bean的解決
這篇文章主要介紹了SpringBoot做junit測試的時候獲取不到bean的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot+MyBatisPlus中樂觀鎖的實現(xiàn)示例
樂觀鎖是一種用于解決并發(fā)沖突的機制,在數(shù)據(jù)庫中用于保護數(shù)據(jù)的一致性,本文主要介紹了SpringBoot+MyBatisPlus中樂觀鎖的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2023-08-08
Java設(shè)計模式之狀態(tài)模式(State模式)介紹
這篇文章主要介紹了Java設(shè)計模式之狀態(tài)模式(State模式)介紹,本文講解了何時使用狀態(tài)模式、如何使用狀態(tài)模式等內(nèi)容,需要的朋友可以參考下2015-03-03

