若依将数据库更改为SQLite

文章目录

    1. 添加依赖项
    1. 更新配置文件 application-druid.yml
    • 2.1. 配置数据源
    • 2.2. 配置连接验证
    1. 更新 MybatisPlusConfig
    1. 解决 mapper 中使用 sysdate() 的问题
    • 4.1. 修改 BaseEntity
    • 4.2. 修改 Mapper
    1. 更新 YML 配置

正文开始:

前提条件:在您的项目中已经集成了 MyBatis-Plus。

1. 添加依赖项

位置:ruoyi-admin/pom.xml

xml 复制代码
<!-- SQLite3驱动 -->
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.36.0.3</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.1</version>
</dependency>

2. 更新配置文件 application-druid.yml

2.1. 配置数据源

yaml 复制代码
# 数据源配置
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: org.sqlite.JDBC
        druid:
            # 主库数据源配置
            master:
                url: jdbc:sqlite:db\\db.sqlite3?date_string_format=yyyy-MM-dd HH:mm:ss
                username:
                password:

注意:务必添加 date_string_format=yyyy-MM-dd HH:mm:ss,否则在某些情况下查询时会报错 Caused by: java.sql.SQLException: Error parsing time stamp。具体错误信息示例:

log 复制代码
13:53:38.671 [restartedMain] ERROR o.s.b.SpringApplication - [reportFailure,870] - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'captchaController': Unsatisfied dependency expressed through field 'configService';
...
Error parsing time stamp; nested exception is java.sql.SQLException: Error parsing time stamp

2.2. 配置连接验证

yaml 复制代码
# 配置检测连接有效性
validationQuery: SELECT 1 FROM sys_config

3. 更新 MybatisPlusConfig

在 MybatisPlusConfig 类中,配置分页插件以支持 SQLite 数据库:

java 复制代码
/**
 * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
 */
public PaginationInnerInterceptor paginationInnerInterceptor() {
    PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
    // 设置数据库类型为SQLite
    paginationInnerInterceptor.setDbType(DbType.SQLITE);
    // 设置最大单页限制数量,默认500条,-1表示不限制
    paginationInnerInterceptor.setMaxLimit(-1L);
    return paginationInnerInterceptor;
}

4. 解决 mapper 中使用 sysdate() 的问题

4.1. 修改 BaseEntity

java 复制代码
/** 当前时间 */
@Setter
@TableField(exist = false)
private Date nowDate;

public Date getNowDate() {
    return DateUtils.getNowDate();
}

4.2. 修改 Mapper

在 SysLogininforMapper.xml 中,替换为以下内容:

sql 复制代码
<insert parameterType="SysLogininfor">
    INSERT INTO sys_logininfor (user_name, status, ipaddr, login_location, browser, os, msg, login_time) 
    VALUES (#{userName}, #{status}, #{ipaddr}, #{loginLocation}, #{browser}, #{os}, #{msg}, CURRENT_TIMESTAMP)
</insert>

在 SysUserMapper.xml 中,修改更新操作:

sql 复制代码
<update parameterType="SysUser">
    <if test="remark != null">remark = #{remark},</if>
    update_time = CURRENT_TIMESTAMP
    WHERE user_id = #{userId}
</update>

5. 更新 YML 配置

在打包为 jar 后,您可以通过配置数据库的路径来灵活设置数据库位置。示例配置如下:

url 复制代码
url: jdbc:sqlite:.//DB//sql.db?date_string_format=yyyy-MM-dd HH:mm:ss

通过以上步骤,您便成功将项目的数据库更改为 SQLite,并配置相应的环境以确保顺利运行。确保在每一步都仔细核对配置,以避免潜在的问题。

相关推荐
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
kangzerun4 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
用户5757303346244 天前
AIGC 时代数据库革命:告别手写 SQL,用自然语言驾驭 SQLite
sqlite
GDAL5 天前
better-sqlite3 深度教程:为什么它比 node‑sqlite3 更好、怎么用、什么时候不该用
sqlite·better-sqlite3
GDAL5 天前
SQLite 与 MySQL 性能深度对比:场景决定最优解
数据库·mysql·sqlite
zihan03216 天前
若依(RuoYi)框架升级适配 JDK 21 和 SpringBoot 3.5.10
java·spring boot·spring·若依·若依升级jdk21
钱彬 (Qian Bin)6 天前
FastAPI的Alembic踩坑记录:缺失历史迁移脚本如何保留数据重建版本控制
sqlite·fastapi·数据库迁移·alembic
qq_454245037 天前
GraphMindStudio 数据操作层解析:基于 SQLite 的封装与自动化存储
sqlite·c#
喵手7 天前
Python爬虫实战:Boss直聘职位数据采集实战 - Playwright + 结构化解析完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·sqlite·爬虫实战·playwright·boss直聘职位数据采集·结构化解析
GDAL8 天前
SQLite 的适用场景与选型指南:它不是轻量 MySQL,而是「文件的升级版」
数据库·mysql·sqlite