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

相关推荐
bigcarp10 小时前
Roundcube Webmail + sqlite
数据库·sqlite
AI成长日志1 天前
【实用工具教程】数据库基础操作实战:SQLite/MySQL连接、CRUD操作与查询优化
数据库·mysql·sqlite
iMingzhen2 天前
不想引入 Redis,我用一张 SQLite 表实现了消息队列
数据库·redis·ai·sqlite
白藏y2 天前
【数据库】SQLite的基础使用
数据库·sqlite
搜佛说3 天前
RocksDB, SQLite, TDengine Edge, LiteDB与sfsDb选型
物联网·edge·sqlite·边缘计算·时序数据库·iot·tdengine
竹林8183 天前
从数据混乱到丝滑管理:我在Python项目中重构SQLite数据库的实战记录
python·sqlite
Yungoal5 天前
C++链接并操作嵌入式数据库SQLite
数据库·c++·sqlite
umeelove356 天前
使用 Qt 插件和 SQLCipher 实现 SQLite 数据库加密与解密
数据库·qt·sqlite
真智AI7 天前
FastAPI+SQLite任务API:从零到可测上线
数据库·sqlite·fastapi
XiaoLeisj7 天前
Android 文件与数据存储实战:SharedPreferences、SQLite 与 Room 的渐进式实现
android·java·数据库·ui·sqlite·room·sp