Project transformation details of Mysql migration to kingbase / PostGreSQL database

(1) Background

  • The company recently requested localization transformation, and the mysql data needs to be migrated to kingbase;
  • Since the project architecture was designed by people who worked on C language before, many table fields are directly named in camel case instead of the underscore form we are familiar with. This part also needs to be modified;
  • At present, I have encountered these pitfalls, sorted them out and sent them out, hoping to provide reference value for those who need them in the future;
  • Will continue to update in the future;

(2) Application and version

  • MybatisPlus(3.3.1)
  • kingbaseES(KingbaseES V008R006C007B0024 on aarch64-unknown-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (NeoKylin 4.8.5-36), 64-bit)

( Kingbase 是基于 PostGreSQL 开发的一款数据库,所以本篇文章也同样适用于 PostGre 改造 )

(3) Create and update logs


【2023-05-16 创建】

1. Modify the @TableName annotation to uniformly modify the table name to lowercase. If there is a table name with a camel case, it is best to change it to an underlined form, and modify the corresponding database table name together;

-- 已知表命名为 t_device 的情况下,以下为错误实例:
1@TableName("T_DEVICE")
2@TableName("tDevice")
3@TableName("T_device")

2. Check whether the fields in the entity class have non-camel case names. The camel case names do not need any modification. The most important thing is that they need to correspond to the fields in the database. 即带下划线的数据库表字段对应驼峰命名的实体类字段;

--修改前的建表语句:
CREATE TABLE "public"."t_device" (
	"manualPath" varchar(64) COLLATE "pg_catalog"."default"
);
-- 修改前对应的实体类字段申明:
private String manualPath ;

-- 修改后的建表语句:
CREATE TABLE "public"."t_device" (
	"manualPath" varchar(64) COLLATE "pg_catalog"."default"
);

Principle: (MybatisPlus will automatically convert underlined fields in the database to camel case)

3. Check the fields and functions (group by) in the Mapper file, type conversion, and modify by yourself;

4. Retrieve project Wrapper to modify the field name of the condition to correspond to the table field Wrapper中使用的字段,必须要和数据库保持一致,否则会报字段找不到的错误;

5. The bit type of the database is uniformly changed to int4

6. The alias operation cannot be used after update

修改前:update t_device t set t.band_flag = #{bandFlag} where t.id = #{id} ;
修改后:update t_device t set t.band_flag = #{bandFlag} where t.id = #{id} ;

7. For the table self-incrementing id, annotations should be added: @TableId(type = IdType.AUTO), 不要使用 @TableId(type = IdType.NONE) 注解,否则 id 不会自增长;

8. There is sql using the date_format function,kingbase 没有这个函数,需要自定义,以下为参考,具体还需要结合业务看是否适配,可自行增减

-- 创建 date_format 函数,并支持若干日期格式
CREATE OR REPLACE FUNCTION "public"."date_format"("indate" anyelement, "intext" text)
    RETURNS "pg_catalog"."text" AS $BODY$
	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('%Y') THEN
		return to_char(inDate,'YYYY');
		end if;
		IF upper(inText) = upper('%m%d') THEN
		return to_char(inDate,'MMDD');
		END IF;
		IF upper(inText) = upper('%k') THEN
		return to_char(inDate,'HH24');
		END IF;
		IF upper(inText) = upper('%c') THEN
		return to_char(inDate,'MM');
		END IF;
		return '';
	END;
	$BODY$
LANGUAGE plpgsql VOLATILE
COST 100

9. There are inconsistencies in the types of the associated table fields, and you need to use :: varchar for type conversion.因为 mysql 的语法对字符串和数值类型做了兼容处理,而 kingbase 是严格区分字符串和数值,所以在遇到一些类型不一致的情况,需要类型转换

-- 已知
SELECT * FROM t_alarm_history h 
	LEFT JOIN t_gis_config c on c.device_id=d.id or c.device_id :: varchar = CONCAT('cam_',d.id);

10. If the return type of this method is resultMap, you need to modify the field name in the column mapped by the resultMap according to the situation;

11. If the username field in the entity class is not named in humpcase, you need to add the annotation @TableField("user_name") to the field. Other fields are similar

12. Whenever you encounter the limit keyword, you need to change it again according to the postgre grammar

-- 对已知表查询前五条数据
修改前:limit 1,5
修改后:limit 0 offset 5

13. If the database table field is of char type and the length is greater than 1, if the stored data is less than the char type length, it is recommended to change it to varchar


【2023-08-08 更新】

1. Where the @MapKey annotation is used, it is necessary to unify the key into lowercase. The Jincang database query ignores the case, and the unification will be automatically converted to lowercase

sql is as follows:

--【修改前】
SQL语句:select name as deviceName , id as deviceId from t_device 
java代码:Map deviceMap = (Map)deviceMap.get("deviceName");

--【修改后】
SQL语句:select name as devicename , id as deviceid from t_device 
java代码:Map deviceMap = (Map)deviceMap.get("devicename");

Guess you like

Origin blog.csdn.net/qq_23845083/article/details/130708295