若依将数据库更改为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,并配置相应的环境以确保顺利运行。确保在每一步都仔细核对配置,以避免潜在的问题。

相关推荐
代码游侠6 小时前
应用——智能配电箱监控系统
linux·服务器·数据库·笔记·算法·sqlite
七夜zippoe10 小时前
领域驱动设计在Python中的实现:从理论到生产级实践
数据库·python·sqlite·ddd·pydantic
凤希AI伴侣13 小时前
告别文件存储的混乱:我用SQLite重构了AI对话记录管理
人工智能·重构·sqlite·凤希ai伴侣
X***07881 天前
理解 MySQL 的索引设计逻辑:从数据结构到实际查询性能的系统分析
数据库·mysql·sqlite
xj7573065332 天前
《精通Django》 第三章 Django模板
数据库·django·sqlite
. . . . .2 天前
SQLite 技术总结:轻量级数据库的本地存储利器
数据库·sqlite
xj7573065333 天前
精通django 第二章 视图和URL
数据库·django·sqlite
曲幽3 天前
FastAPI + SQLite:从基础CRUD到安全并发的实战指南
python·sqlite·fastapi·web·jwt·form·sqlalchemy·oauth2
xj7573065334 天前
《精通Django》第一章 入门
数据库·django·sqlite
bjzhang754 天前
Dorisoy.AMS--一款采用C# WinForm框架+SQLite数据库的企业/机构资产管理解决方案
sqlite·c#·资产管理