开发喵星球

若依框架数据源切换为pg库(165)

若依管理系统,默认是mysql数据库,这里将数据源切换为pg

一 切换数据源

ruoyi-admin项目里引入pg数据库驱动

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.18</version>
</dependency>

修改配置文件里的数据源为pg

spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: org.postgresql.Driver
        druid:
            # 主库数据源改为pg库
            master:
                url: jdbc:postgresql://192.168.119.128:5432/FDS?stringtype=unspecified
                username: postgres
                password: ts123456

二 切换函数以及语法

因为pg库里没有dual表,所以把配置文件里validationQuery的值,从SELECT 1 FROM DUAL换为:select version()来判断数据库是否正常连接

application.yml里的PageHelper分页插件换成pgsql

pagehelper:
  helperDialect: postgresql
  supportMethodsArguments: true
  params: count=countSql

三 加自增序列

凡是有自增主键的表,都要加自增序列

CREATE SEQUENCE sys_user_id_seq
    START WITH 3
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_user alter column user_id set default nextval('sys_user_id_seq');

-- 从当前最大id依次递增
--select setval('sys_user_id_seq',(select max(user_id) from sys_user));

CREATE SEQUENCE sys_oper_log_id_seq
    START WITH 3
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_oper_log alter column oper_id set default nextval('sys_oper_log_id_seq');

-- 从当前最大id依次递增
--select setval('sys_oper_log_id_seq',(select max(oper_id) from sys_oper_log));
CREATE SEQUENCE sys_role_id_seq
    START WITH 3
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_role alter column role_id set default nextval('sys_role_id_seq');
CREATE SEQUENCE sys_notice_id_seq
    START WITH 3
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_notice alter column notice_id set default nextval('sys_notice_id_seq');
CREATE SEQUENCE sys_dict_type_id_seq
    START WITH 11
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_dict_type alter column dict_id set default nextval('sys_dict_type_id_seq');
CREATE SEQUENCE sys_dept_id_seq
    START WITH 11
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_dept alter column dept_id set default nextval('sys_dept_id_seq');

SELECT 'CREATE SEQUENCE ' || sequence_name || ' START ' ||  start_value || ';' from information_schema.sequences;
CREATE SEQUENCE gen_table_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table gen_table alter column table_id set default nextval('gen_table_id_seq');
CREATE SEQUENCE gen_table_column_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table gen_table_column alter column column_id set default nextval('gen_table_column_id_seq');
CREATE SEQUENCE sys_post_id_seq
    START WITH 5
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_post alter column post_id set default nextval('sys_post_id_seq');
CREATE SEQUENCE sys_dict_data_code_seq
    START WITH 30
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_dict_data alter column dict_code set default nextval('sys_dict_data_code_seq');
CREATE SEQUENCE sys_job_seq
    START WITH 4
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_job alter column job_id set default nextval('sys_job_seq');
CREATE SEQUENCE sys_job_log_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE 
    CACHE 1;

-- 设置表某个字段自增
alter table sys_job_log alter column job_log_id set default nextval('sys_job_log_id_seq');

查询序列语法

SELECT 'CREATE SEQUENCE ' || sequence_name || ' START ' ||  start_value || ';' from information_schema.sequences;

删除序列语法

# 如果新建的序列有问题,可以使用下边这个语句删除后,再重新新建
DROP SEQUENCE sys_oper_log_id_seq

四 加默认值、加引号

凡是之前是char类型的字段,并且有默认值的,都需要在navicat里添加默认值,如sys_user表、sys_role表、sys_dept表的del_flagstatus字段,设置默认值,注意,要加引号。sys_menusys_postsys_noticesys_loggininfosys_job_logsys_dict_typesys_dict_data表的status字段,设置默认值0,加引号。

五 改代码

SysRole.java里的menuCheckStrictlydeptCheckStrictly改为int类型,index.vue里的menuCheckStrictlydeptCheckStrictly,由true改为1,搜索vue文件里的“父子联动”,注释掉。

GenTableMapper.xml里的id=“selectDbTableList”,修改为

 <select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">
        select * from information_schema.tables
        where table_schema = (select CURRENT_SCHEMA())
        AND table_name NOT LIKE 'qrtz_%' AND table_name NOT LIKE 'gen_%'
        AND table_name NOT IN (select table_name from gen_table)
        <if test="tableName != null and tableName != ''">
            AND lower(table_name) like lower(concat('%', #{tableName}, '%'))
        </if>
        <!--<if test="tableComment != null and tableComment != ''">
            AND lower(table_comment) like lower(concat('%', #{tableComment}, '%'))
        </if>
        <if test="params.beginTime != null and params.beginTime != ''"><!– 开始时间检索 –>
            AND date_format(create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
        </if>
        <if test="params.endTime != null and params.endTime != ''"><!– 结束时间检索 –>
            AND date_format(create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
        </if>
        order by create_time desc-->
</select>

六 去掉数据库的校验

如果不想使用数据库,只使用若依提供的封装工具类,那么把下边这三个类的init构造函数去掉即可:

SysConfigServiceImpl.java:
位置:com/ruoyi/system/service/impl/SysConfigServiceImpl.java

SysDictTypeServiceImpl.java:
位置:com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java:

SysJobServiceImpl.java:
位置:com/ruoyi/quartz/service/impl/SysJobServiceImpl.java

   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2024-04-01 15:02:27 阅读量:90
<<   >>


powered by kaifamiao