开发喵星球

若依更改数据库为SqlLite(177)

前提:在你的项目上已经集成mybatis-plus,还没有集成的可以看这篇文章:若依分离版集成Mybatis-Plus(六十二)

1、添加依赖

位置:ruoyi-admin/pom.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.配置驱动

位置:

# 数据源配置
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错误
具体错误信息如下:

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';
 nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sysConfigServiceImpl': Invocation of init 
method failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: 
Error attempting to get column 'create_time' from result set.  Cause: java.sql.SQLException: Error parsing time stamp
; uncategorized SQLException; SQL state [null]; error code [0]; Error parsing time stamp; nested exception is java.sql.SQLException: Error parsing time stamp
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
    ······
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1359)
    at com.ruoyi.RuoYiApplication.main(RuoYiApplication.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

2.1、修改初始化时校验是否连接成功查询

# 配置检测连接是否有效
    validationQuery: SELECT 1 FROM sys_config

3、MybatisPlusConfig

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

4、修改因为mapper中使用sysdate() 报错问题

4.1.BaseEntity

    /** 当前时间 */
    @Setter
    @TableField(exist = false)
    private Date nowDate;

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

4.2修改mapper

SysLogininforMapper.xml
<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}, getdate())

SysUserMapper.xml
<update  parameterType="SysUser">
    <if test="remark != null">remark = #{remark},</if>
    update_time = getdate()
</set>
where user_id = #{userId}

5、yml配置

打包jar完成后,数据库db所在文件夹“DB”,yml配置数据库路径可配置为:

url: jdbc:sqlite:.//DB//sql.db?date_string_format=yyyy-MM-dd HH:mm:ss
   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2024-04-13 00:39:38 阅读量:407
<<   >>


powered by kaifamiao