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

相关推荐
q***72561 小时前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
HillVue10 小时前
重估百度,也是在重估 AI 的未来
大数据·人工智能·sqlite
q***23921 天前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
6极地诈唬1 天前
【C#-sqlSugar-sqlite】在Windows从源码编译构建System.Data.SQLite.dll的方法
windows·sqlite·c#
humors2212 天前
前端开发案例(不定期更新)
前端·vue.js·elementui·ruoyi·若依
傻啦嘿哟2 天前
爬取数据存入SQLite:轻量级数据库实战指南
数据库·sqlite
aerror2 天前
将sqlite3的表转成excel表
java·sqlite·excel
q***46523 天前
在Django中安装、配置、使用CKEditor5,并将CKEditor5录入的文章展现出来,实现一个简单博客网站的功能
数据库·django·sqlite
EelBarb3 天前
sqlite数据库迁移至mysql
数据库·mysql·sqlite
不惑_3 天前
[特殊字符] 在 Linux 上设置 SQLite
linux·jvm·sqlite