最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot加載SQLite數(shù)據(jù)源方式

 更新時(shí)間:2025年09月12日 09:07:37   作者:咖啡Beans  
文章介紹了SpringBoot中配置SQLite數(shù)據(jù)源的流程,包括DBeaver連接數(shù)據(jù)庫(kù)、執(zhí)行建表與數(shù)據(jù)插入、引入依賴(lài)、初始化連接、定義實(shí)體類(lèi)與Mapper,以及測(cè)試查詢(xún)操作,同時(shí)提及多數(shù)據(jù)源適配的擴(kuò)展方式

摘要

本文演示SpringBoot中單獨(dú)使用SQLite數(shù)據(jù)源的場(chǎng)景寫(xiě)法。

示例步驟

1)打開(kāi)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)引入依賴(lài)

<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("===開(kāi)始數(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)類(lèi)名
       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          : ===開(kāi)始數(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í)體類(lèi)

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è)試查詢(xún)

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查詢(xún):" + sqliteUserMapper.selectBySearchInSqlite());
       /*
          Sqlite查詢(xún):[SqliteUser(id=1, name=sqlite名字, createTime=2025-01-01 00:00:00)]
        */
    }

}

總結(jié)

以上我們了解了SpringBoot中單獨(dú)使用SQLite數(shù)據(jù)源的場(chǎng)景寫(xiě)法,除此之外還有更簡(jiǎn)便的使用場(chǎng)景,比如多數(shù)據(jù)源適配。

這些僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

广平县| 梅州市| 建湖县| 海安县| 朝阳市| 彭山县| 平安县| 南漳县| 海口市| 陕西省| 东乡族自治县| 同心县| 黑河市| 桓台县| 唐河县| 上思县| 都匀市| 天全县| 米易县| 长寿区| 内乡县| 抚松县| 前郭尔| 长白| 桐柏县| 准格尔旗| 咸宁市| 林州市| 钟山县| 宜兰县| 波密县| 郸城县| 开化县| 林芝县| 甘南县| 华阴市| 蓬安县| 阳城县| 黎川县| 孝义市| 黔江区|