SpringBoot整合SQLite的超詳細(xì)講解
一、SQLite是什么
SQLite是一個(gè)不需要服務(wù)、不需要配置、不需要外部依賴的開源SQL輕量級(jí)數(shù)據(jù)庫(kù)。
- 不需要服務(wù)器:如MySQL安裝后,會(huì)在操作系統(tǒng)中創(chuàng)建一個(gè)進(jìn)程mysqld.exe,而SQLite不需要?jiǎng)?chuàng)建。
- 不需要配置:如MySQL安裝后,需要配置端口、用戶名、密碼等,而SQLite不需要,它是存儲(chǔ)在磁盤上的文件,不需要安裝。
- 不需要外部依賴:SQLite是自給自足的,不需要任何外部的依賴。
二、SQLite優(yōu)點(diǎn)
sqlite支持MySQL擁有的大多數(shù)功能。
允許多個(gè)進(jìn)程\線程安全訪問(wèn),支持事務(wù)機(jī)制。
允許多門開發(fā)語(yǔ)言調(diào)用,支持JDBC。
支持Windows、Linux等多個(gè)操作系統(tǒng)上運(yùn)行。
三、SpringBoot整合SQLite
準(zhǔn)備工作
1.創(chuàng)建一個(gè)文件(后綴名為.db)
【建議】將這個(gè)文件夾放到項(xiàng)目所在的目錄。
這個(gè)文件的相當(dāng)于MySQL中創(chuàng)建一個(gè)庫(kù)。
以下是我創(chuàng)建的文件,
2.使用IDEA連接
顯示這個(gè)就是連接成功了。
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 # 全局邏輯刪除的實(shí)體字段名(since 3.3.0,配置后可以忽略不配置步驟2) logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1) logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0)
解釋
3.代碼
我剛才創(chuàng)建的表有三個(gè)字段,id、name、age
實(shí)體類
@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> { }
實(shí)現(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é)果
以上我們就整合完了,寫了一個(gè)小小的demo。
總結(jié)
到此這篇關(guān)于SpringBoot整合SQLite的文章就介紹到這了,更多相關(guān)SpringBoot整合SQLite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用
這篇文章主要介紹了Java的命名方法和變量的值及其引用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09Idea設(shè)置全局highlighting?level為Syntax問(wèn)題
這篇文章主要介紹了Idea設(shè)置全局highlighting?level為Syntax問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Collections工具類_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Collections工具類提供了大量針對(duì)Collection/Map的操作。這篇文章主要介紹了Collections工具類_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-04-04Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費(fèi)消息的實(shí)例代碼
本文介紹了在SpringBoot中如何使用RabbitMQ進(jìn)行消息的生產(chǎn)和消費(fèi),詳細(xì)闡述了RabbitMQ中交換機(jī)的作用和類型,包括直連交換機(jī)、主題交換機(jī)、扇出交換機(jī)和頭交換機(jī),并解釋了各自的消息路由機(jī)制,感興趣的朋友一起看看吧2024-10-10SpringBoot做junit測(cè)試的時(shí)候獲取不到bean的解決
這篇文章主要介紹了SpringBoot做junit測(cè)試的時(shí)候獲取不到bean的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot+MyBatisPlus中樂(lè)觀鎖的實(shí)現(xiàn)示例
樂(lè)觀鎖是一種用于解決并發(fā)沖突的機(jī)制,在數(shù)據(jù)庫(kù)中用于保護(hù)數(shù)據(jù)的一致性,本文主要介紹了SpringBoot+MyBatisPlus中樂(lè)觀鎖的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08Java設(shè)計(jì)模式之狀態(tài)模式(State模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之狀態(tài)模式(State模式)介紹,本文講解了何時(shí)使用狀態(tài)模式、如何使用狀態(tài)模式等內(nèi)容,需要的朋友可以參考下2015-03-03