金仓数据库是基于POSTGRE_SQL
参考资料
国产数据库人大金仓踩坑记录和函数适配_金仓数据库关系不存在-CSDN博客
Springboot工程 适配人大金仓 kingbase V8R3
引入驱动包和方言包
hibernate-5.2.17.Finaldialect.jar
kingbase8-8.2.0.jar
application.yml文件
driver-class-name: com.kingbase8.Driver
url: jdbc:kingbase8://10.7.215.249:54321/ta
username: root
password: e022f87539fd81f3
dialect: org.hibernate.dialect.Kingbase8Dialect
如果项目中使用了mybatis分页插件,需要加以下配置类,负责工程无法启动
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor pageHelper=new PaginationInterceptor();
/**
*分页插件会根据配置的数据库连接地址获取对应的方言,但不支持kingbase,kingbase的需要设置成 MYSQL
*/
pageHelper.setDialectType(DbType.POSTGRE_SQL.getDb());
return pageHelper;
}
}
遇到的问题:
1.KingbaseV8R6无法设置大小写敏感,查询时,表名或字段名不能用“`”符号,表字段名或别名需要用双引号括起来,否则全部会转为小写,sql语句中的出现的字符串需要用单引号标识。影响范围较大
故采用低版本的V8R3,查询使用别名时,最好遵循 字段 as 别名 这种写法。
2.mysql的group_concat函数需要替换成string_agg(字段名,分隔符),写法如下
SELECT
string_agg(t.resource_id,',')
FROM
sim_group t
3. 使用group by时 查询的字段和order by的字段都必须出现在group by后面。解决方案:
在kingbase的安装目录下KingbaseV8R3/data找到kingbase.conf文件
加入下面一行配置,重启数据库
exclude_reserved_words='owner' // 屏蔽关键字 多个用逗号隔开
sql_mode='' // 兼容mysql group by命令
4. mysql中 一列拆成多行
SELECT
substring_index(substring_index( a.rn,',',b.help_topic_id + 1 ),',' ,- 1) AS rn
FROM
(select '1,2,3,4' as rn) a
JOIN mysql.help_topic b ON b.help_topic_id <
(length(a.rn) - length( replace(a.rn, ',', '') ) + 1)
kingbase中的写法:
select regexp_split_to_table('a,b,c,d,e,f',',') as rn;
5. timestampdiff函数需要自己创建, 其中第一个参数为text类型,函数创建语句如下
create or replace function timestampdiff(para1 text,para2 timestamp,para3 timestamp) return bigint
as
declare
diff interval day to second;
diffs bigint;
diffm bigint;
diffh bigint;
diffd bigint;
nyy bigint;
ny bigint;
nm bigint;
begin
diff:=para3-para2;
diffs:=extract(second from diff);
diffm:=extract(minute from diff);
diffh:=extract(hour from diff);
diffd:=extract(day from diff);
ny:=extract(year from age(para3,para2));
nm:=extract(month from age(para3,para2));
nyy:=extract(year from para3)-extract(year from para2);
if para1='second' then
return ((diffd*24+diffh)*60+diffm)*60+diffs;
elseif para1='minute' then
return (diffd*24+diffh)*60+diffm;
elseif para1='hour' then
return diffd*24+diffh;
elseif para1='day' then
return diffd;
elseif para1='month' then
return ny*12+nm;
elseif para1='year' then
return nyy;
else
raise warning 'para1 not support "%"!',para1;
return null;
end if;
end;
用法示例:
SELECT
ip,
safe_state safeState,
update_state_date updateStateDate
FROM
asset_search
WHERE
updated = false
AND TimeStampDiff( 'hour', update_state_date, now()) > 24
6.获取当前时间的前7天
mysql中:
select date_add(SYSDATE(), interval -7 day)
kingbase中:
select sysdate-7
7. kingbase兼容date_formate函数
CREATE OR REPLACE FUNCTION date_format(indate anyelement, intext text)
RETURNS text
LANGUAGE plsql
AS
BEGIN
IF upper(inText) = upper('%Y%m%d_%H%i') THEN
return to_char(inDate,'YYYYMMDD_HH24MI');
END IF;
IF upper(inText) = upper('%Y%m%d%H%i%s') THEN
return to_char(inDate,'YYYYMMDDHH24MISS');
END IF;
IF upper(inText) = upper('%Y-%m-%d %H') THEN
return to_char(inDate,'YYYY-MM-DD HH24');
END IF;
IF upper(inText) = upper('%Y-%m-%d') THEN
return to_char(inDate,'YYYY-MM-DD');
END IF;
IF upper(inText) = upper('%Y-%m') THEN
return to_char(inDate,'YYYY-MM');
end if;
IF upper(inText) = upper('%m%d') THEN
return to_char(inDate,'MMDD');
END IF;
return '';
END;
8.创建主键id自增的表,字段类型 SERIAL 表示自增,它会在库里创建一个序列
CREATE TABLE test (
id SERIAL NOT NULL ,
ip character varying(200 char),
alarm_num integer,
source_attack_num integer,
get_attack_num integer,
by_visit_num integer,
create_date date,
visit_num integer,
event_amount integer,
PRIMARY KEY ("id")
)
9.使用hibernate查询时,如果是以下写法,则会报错 Can't use query methods that take a query string on a PreparedStatement.
修改方式为:
PreparedStatement st = con.prepareStatement(sql);
ResultSet rs = st.executeQuery()
10.初始化数据时,如果表的主键是自增,则insert语句中不要对id列赋值,否则后续新增数据时,会报错主键冲突,原因是,手动给id赋值没有触发自增id的序列值更新。
解决方法:
把表对应的序列也初始化一下。
SELECT sys_catalog.setval('"PUBLIC"."clxt_compliance_config_id_SEQ"', 2, true);
关键字:
user、enable、label、type...
更多问题请参考金仓数据库知识库:金仓数据库知识库 - 金仓社区