SpringBoot加載SQLite數(shù)據(jù)源方式
摘要
本文演示SpringBoot中單獨(dú)使用SQLite數(shù)據(jù)源的場(chǎng)景寫法。
示例步驟
1)打開dbeaver選擇sqlite連接

- 選擇自行創(chuàng)建的db文件地址

2)執(zhí)行建表語(yǔ)句,并新增數(shù)據(jù)
-- 用工具連接sqlite庫(kù),執(zhí)行建表DDL初始化數(shù)據(jù)庫(kù) -- 文件存放在src/main/resources/sqlite/my_sqlite.db CREATE TABLE IF NOT EXISTS user ( id INTEGER primary key autoincrement, name VARCHAR, create_time VARCHAR ); INSERT INTO "user" (id, name, create_time) VALUES(1, 'sqlite名字', '2025-01-01 00:00:00');
3)引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
</dependencies>
4)初始化sqlite數(shù)據(jù)源連接配置
package org.coffeebeans.config;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.File;
/**
* <li>ClassName: SqliteDbConfig </li>
* <li>Author: OakWang </li>
* 初始化數(shù)據(jù)源
*/
@Slf4j
@Configuration
public class SqliteDbConfig {
@Autowired
private DataSource dataSource;
@Autowired
private HikariDataSourceCreator dataSourceCreator; //需要顯式指定HikariDataSourceCreator
@PostConstruct
public void initDatasource() {
log.info("===開始數(shù)據(jù)源初始化===");
File file = FileUtil.file(ResourceUtil.getResource("sqlite/my_sqlite.db")); // 獲取sqlite數(shù)據(jù)庫(kù)文件
DataSourceProperty dataSourceProperty = new DataSourceProperty(); // 創(chuàng)建數(shù)據(jù)源屬性對(duì)象
dataSourceProperty.setUrl("jdbc:sqlite:" + file.getAbsolutePath()); // 設(shè)置數(shù)據(jù)庫(kù)連接URL
dataSourceProperty.setDriverClassName("org.sqlite.JDBC"); // 設(shè)置數(shù)據(jù)庫(kù)驅(qū)動(dòng)類名
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; // 獲取動(dòng)態(tài)數(shù)據(jù)源實(shí)例
DataSource masterDataSource = dataSourceCreator.createDataSource(dataSourceProperty); // 根據(jù)屬性創(chuàng)建數(shù)據(jù)源
ds.addDataSource("master", masterDataSource); // 添加數(shù)據(jù)源到動(dòng)態(tài)數(shù)據(jù)源中,命名為master
log.info("===數(shù)據(jù)源初始化完畢===");
}
/*
啟動(dòng)輸出日志:
c.b.d.d.DynamicRoutingDataSource : dynamic-datasource initial loaded [0] datasource,Please add your primary datasource or check your configuration
org.coffeebeans.config.SqliteDbConfig : ===開始數(shù)據(jù)源初始化===
c.b.d.d.DynamicRoutingDataSource : dynamic-datasource - add a datasource named [master] success
org.coffeebeans.config.SqliteDbConfig : ===數(shù)據(jù)源初始化完畢===
org.coffeebeans.SqliteTest : Started SqliteTest in 2.668 seconds (JVM running for 3.577)
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
*/
}
5)定義實(shí)體類
package org.coffeebeans.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
/**
* <li>ClassName: SqliteUser </li>
* <li>Author: OakWang </li>
*/
@Data
@TableName("user")
publicclass SqliteUser implements Serializable {
private static final long serialVersionUID = 6133167221453780808L;
@TableId(type = IdType.AUTO)
private Long id;
@TableField("name")
private String name;
@TableField("create_time")
private String createTime;
}
6)定義Mapper
package org.coffeebeans.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.coffeebeans.entity.SqliteUser;
import java.util.List;
@Mapper
public interface SqliteUserMapper {
@Select("select id,name,create_time as createTime from user where id = 1")
List<SqliteUser> selectBySearchInSqlite();
}
7)測(cè)試查詢
package org.coffeebeans;
import lombok.extern.slf4j.Slf4j;
import org.coffeebeans.mapper.SqliteUserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* <li>ClassName: org.org.org.coffeebeans.SqliteTest </li>
* <li>Author: OakWang </li>
*/
@Slf4j
@SpringBootTest
public class SqliteTest {
@Autowired
private SqliteUserMapper sqliteUserMapper;
@Test
void test() {
log.info("Sqlite查詢:" + sqliteUserMapper.selectBySearchInSqlite());
/*
Sqlite查詢:[SqliteUser(id=1, name=sqlite名字, createTime=2025-01-01 00:00:00)]
*/
}
}
總結(jié)
以上我們了解了SpringBoot中單獨(dú)使用SQLite數(shù)據(jù)源的場(chǎng)景寫法,除此之外還有更簡(jiǎn)便的使用場(chǎng)景,比如多數(shù)據(jù)源適配。
這些僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot+mybatis-plus實(shí)現(xiàn)多數(shù)據(jù)源配置的詳細(xì)步驟
- 在SpringBoot中配置和使用多個(gè)數(shù)據(jù)源方式
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源的示例代碼
- SpringBoot動(dòng)態(tài)配置數(shù)據(jù)源的三種實(shí)現(xiàn)方式
- SpringBoot + Druid + Dynamic Datasource 多數(shù)據(jù)源配置方案
- 在SpringBoot項(xiàng)目中動(dòng)態(tài)切換數(shù)據(jù)源和數(shù)據(jù)庫(kù)的詳細(xì)步驟
- SpringBoot項(xiàng)目中多數(shù)據(jù)源配置方法與使用場(chǎng)景
相關(guān)文章
如何使用java給局域網(wǎng)的電腦發(fā)送開機(jī)數(shù)據(jù)包
這篇文章主要為大家詳細(xì)介紹了如何使用java給局域網(wǎng)的電腦發(fā)送開機(jī)數(shù)據(jù)包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-09-09
SpringMVC數(shù)據(jù)輸出相關(guān)知識(shí)總結(jié)
今天帶大家學(xué)習(xí)SpringMVC的相關(guān)知識(shí),文中對(duì)SpringMVC數(shù)據(jù)輸出作了非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
LRU算法及Apache?LRUMap源碼實(shí)例解析
這篇文章主要給大家介紹了關(guān)于LRU算法及Apache?LRUMap源碼解析的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11
Java 滑動(dòng)窗口最大值的實(shí)現(xiàn)
這篇文章主要介紹了Java 滑動(dòng)窗口最大值,給定一個(gè)數(shù)組 nums,有一個(gè)大小為 k 的滑動(dòng)窗口從數(shù)組的最左側(cè)移動(dòng)到數(shù)組的最右側(cè)。感興趣的可以了解一下2021-05-05
解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問題
下面小編就為大家?guī)?lái)一篇解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-10-10
Java遞歸運(yùn)行的機(jī)制:遞歸的微觀解讀圖文分析
這篇文章主要介紹了Java遞歸運(yùn)行的機(jī)制:遞歸的微觀解讀,結(jié)合圖文形式詳細(xì)分析了java遞歸運(yùn)行的原理、機(jī)制與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-03-03
Spring 中的 @PathVariable 注解及應(yīng)用場(chǎng)景分析
@PathVariable注解是 Spring 框架中一個(gè)非常實(shí)用的注解,它可以幫助我們輕松地從 URL 中提取參數(shù),從而實(shí)現(xiàn) RESTful API 的開發(fā),通過本文的介紹,我們了解了@PathVariable注解的基本使用方法和高級(jí)用法,以及它的應(yīng)用場(chǎng)景,感興趣的朋友跟隨小編一起看看吧2025-05-05

