若依框架数据源切换为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:
# 主库数据源
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()
来判断数据库是否正常连接
PageHelper分页插件换成pgsql的
pagehelper:
helperDialect: postgresql
supportMethodsArguments: true
params: count=countSql
在mapper.xml里全局搜索sysdate()
,换为now()
在mapper.xml里全局搜索ifnull
(字段,‘’) 函数,换成 COALESCE
(字段,‘’)
char类型的值,mysql可以不加引号,但是pg必须加单引号
mapper.xml文件里,搜索
status = 0
,改为status = '0'
搜索`query` ,换为query
全局搜索database()
,换为CURRENT_SCHEMA()
xml里搜索sysdate()
函数,换为now()
三 加自增序列
凡是有自增主键的表,都要加自增序列
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');
查询序列
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_flag、status字段,设置默认值,注意,要加引号。sys_menu、sys_post、sys_notice、sys_loggininfo、sys_job_log、sys_dict_type、sys_dict_data表的status字段,设置默认值0,加引号。
五 改代码
SysRole.java里的menuCheckStrictly、deptCheckStrictly改为int类型,index.vue里的menuCheckStrictly、deptCheckStrictly,由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>