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

相关推荐
AC使者6 小时前
介绍 TensorFlow 的基本概念和使用场景。
开发语言·自然语言处理·sqlite·github
begei9 小时前
在Django中安装、配置、使用CKEditor5,并将CKEditor5录入的文章展现出来,实现一个简单博客网站的功能
数据库·django·sqlite
信阳农夫1 天前
python 3.6.8支持的Django版本是多少?
python·django·sqlite
LCY1332 天前
websocket与django
websocket·django·sqlite
一个处女座的程序猿O(∩_∩)O2 天前
Django+Vue3全栈开发实战:从零搭建博客系统
vue.js·python·django·sqlite
努力的CV战士2 天前
数据库-SQLite
c语言·开发语言·sqlite
神仙别闹2 天前
基于Python+Sqlite实现的选课系统
jvm·python·sqlite
m0_748255413 天前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
wangz765 天前
Android 下用kotlin写一个sqlite
android·sqlite·kotlin·jetpack compose
忧虑的乌龟蛋5 天前
基于Qt 和微信小程序的用户管理系统:WebSocket + SQLite 实现注册与登录
数据库·嵌入式硬件·qt·物联网·websocket·微信小程序·sqlite