最近项目需要将oracle 数据库转换成mysql数据库,网上百度了下,总结一下:
1.数据库表结构修改
通过工具powerdesign 将导出的oracle 表结构sql 导入,转换成mysql 表结构数据
方法通过power design 工具
1)导出ORACLE表结构
2)File->Reverse Engineer->Database,设置物理模型的名称及所使用数据库类型,选择 Oracle version 11g,然后点击 Using script files 框里的 Add Files 按钮,选择已经导出的 Oracle 表结构 sql 文件
3)改变数据库类型,Database->Change Current DBMS,Current DBMS 显示当前的数据库类型 Oracle version 11g,New DBMS 中选择 MySQL 5.0
4) 导出 sql 文件
Database->Generete Database
或者快捷键:ctrl+g
5)将数据导出,导入到oracle 基本通用,有需要修改函数修改下基本就可以了
2.配置文件相关修改
1)Jdbc.properties 增加
#mysql 数据库
qxgl.jdbc.driverClass=com.mysql.jdbc.Driver
qxgl.jdbc.url=jdbc:mysql://localhost:3306/hotelqxgl?useUnicode=true&characterEncoding=utf8&autoReconnect=true
qxgl.jdbc.user=root
qxgl.jdbc.password=123456
2)修改applicationContext.xml中hibernate 数据库方言设置为mysql
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
prop>
3.函数替换:
Notepad正则匹配表达式:
to_date(\([^\)]*\))替换为:DATE_FORMAT\(NOW\(\),'%Y-%m-%d'\)
to_char(\([^\)]*\))替换为:DATE_FORMAT\(NOW\(\),'%Y-%m-%d'\)
datetime default 'SYSDATE' 替换为:timestamp NULL DEFAULT CURRENT_TIMESTAMP
4.触发器,存储过程相关改造,待整理
参考触发器存储过程:
-- LKWB_INSERT
DROP TRIGGER IF EXISTS `LKWB_INSERT`;
DELIMITER ;;
create trigger LKWB_INSERT
before insert on LKWB
for each row
begin
declare rk int DEFAULT 0;
declare i_rows int DEFAULT 0;
declare r_wb_lg varchar(10);
declare r_id varchar(36);
declare r_zwm varchar(100);
declare r_ywm varchar(100);
declare r_ywx varchar(100);
declare r_csrq varchar(8);
declare r_zjlx varchar(20);
declare r_zjlxm varchar(100);
declare r_zjhm varchar(18);
declare r_gj varchar(20) ;
declare r_gjm varchar(100) ;
declare r_rzsj varchar(12);
declare r_rzrq datetime;
declare r_rzcs int;
declare r_xb varchar(20) ;
declare r_xbm varchar(100);
declare r_xtlg varchar(10);
declare r_lg varchar(10);
declare r_lkbms text;
declare r_lkbm varchar(22);
declare r_lg2 varchar(2000);
declare r_rzcs2 int default 0;
DECLARE code varchar(102) ;
DECLARE msg varchar(102) ;
declare erid varchar(32) ;
declare cwsj datetime;
declare cwlx varchar(20);
declare cwbt varchar(100) ;
declare cwnr text;
declare continue handler for SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
SET erid := replace(uuid(), '-', '') ;
SET cwsj := SYSDATE();
SET cwlx := '1';
SET cwbt := '旅客外宾一天多处登记处理触发器lkwb_insert';
SET cwnr := concat('旅客编码:' , NEW.lkbm , ',错误码:' , code , ',错误内容:',msg );
INSERT INTO ERROR_LOG VALUES (erid,cwsj,cwlx,cwbt,cwnr);
END;
SELECT COUNT(1) into rk from lkwb_rksj where lkbm=NEW.lkbm;
if rk=0 then
SET NEW.etlsj := SYSDATE();
insert into lkwb_rksj (lkbm,rksj) values (NEW.lkbm,NEW.etlsj);
else
SELECT rksj into @NEW.etlsj from lkwb_rksj where lkbm=NEW.lkbm;
end if;
-- 外宾日期可能特殊
begin
declare continue handler for SQLEXCEPTION
BEGIN
SET i_rows := -1;
END;
-- 传送延迟,单位(天)
-- 通过mysql 函数对比相差秒数,转换成天数
set NEW.csyc := TIMESTAMPDIFF(SECOND,NEW.rzrq,STR_TO_DATE(NEW.cssj,'%Y%m%d%H%i'))/(24*3600) ;
end;
if NEW.tfsj is null or NEW.tfsj='' then
SET NEW.sfzz := '1';
else
SET NEW.sfzz := '0';
end if;
-- 查找入住时间为当天,并且证件号码一致的。
select count(*) into i_rows
from dual where exists(select 1 from lkwb a
where a.jdrzsj=NEW.jdrzsj
and a.zjhm = NEW.zjhm);
-- 如果存在一天多处登记
if i_rows>0 then
-- 查找一天多处登记表是否存在多处登记记录
select count(*) into i_rows
from dual where exists(
select 1 from cxwbitdcdj a
where a.rzsj=NEW.jdrzsj
and a.zjhm = NEW.zjhm);
if i_rows=0 then
-- 如果一天多处登记表不存在记录,哪么新增
set r_id := replace(uuid(), '-', '') ;
set r_rzcs := 2;
SELECT a.lkbm,a.lg,a.zwm,a.ywm,a.ywx,a.csrq,a.zjlx,a.zjlxm,a.zjhm,a.gj,a.gjm,a.jdrzsj,a.rzrq,a.xb,a.xbm
INTO r_lkbm,r_wb_lg,r_zwm,r_ywm,r_ywx,r_csrq,r_zjlx,r_zjlxm,r_zjhm,r_gj,r_gjm,r_rzsj,r_rzrq,r_xb,r_xbm
FROM LKWB a WHERE a.jdrzsj=NEW.jdrzsj and a.zjhm = NEW.zjhm limit 1;
-- 判断是否入住同一家旅馆
if r_wb_lg = NEW.lg then
SET r_xtlg := '1'; -- 同一旅馆
SET r_lg := NEW.lg;
else
set r_xtlg := '0'; -- 不同旅馆
-- 记录不同旅馆,根据旅客内宾触发器逻辑新增
set r_rzcs2 := 2;
set r_lg2 := concat(new.lg , ',' , r_lg);
end if;
SET r_lkbms := concat(r_lkbm,',', NEW.lkbm);
insert into cxwbitdcdj values (r_id,r_zwm,r_ywx,r_ywm,r_xb,r_xbm,r_csrq,r_zjlx,r_zjlxm,r_zjhm,r_gj,r_gjm,r_rzsj,r_rzrq,r_rzcs,r_xtlg,r_lkbms,null,null,null);
else
-- 已经存在,哪么修改
select a.lkbms into r_lkbms from cxwbitdcdj a where a.jdrzsj=NEW.jdrzsj and a.zjhm = NEW.zjhm limit 1;
IF instr(r_lkbms,NEW.lkbm)=0 THEN
-- 如果原来记录的旅馆是同一旅馆多处入住,哪么判断原来的旅馆和现在的旅馆
if r_xtlg = '1' then
if r_lg != NEW.lg then
SET r_xtlg := '0';
end if;
end if;
-- 判断是否是同旅馆,如果不是,哪么修改计数器
IF instr(r_lg2,new.lg)=0 THEN
set r_rzcs2 := r_rzcs2 + 1;
set r_lg2 := concat(new.lg , ',' , r_cxnbitdcdj.lg);
END IF;
SET r_lkbms := concat(lkbms ,',', NEW.lkbm);
SET r_rzcs := r_rzcs + 1;
-- 修改旅客编码和入住状态
update cxwbitdcdj set lkbms = r_lkbms,
xtlg = r_xtlg,
rzcs = r_rzcs,
rzcs2 = r_rzcs2,
lg2 = r_lg2
where id=r_id;
END IF;
end if;
end if;
end;
;;
DELIMITER ;
-- lkwb_update 触发器
DROP TRIGGER IF EXISTS `LKWB_UPDATE`;
DELIMITER ;;
create trigger LKWB_UPDATE
before update on LKWB
for each row
begin
declare i_rows int;
DECLARE code varchar(102) ;
DECLARE msg varchar(102) ;
declare erid varchar(32) ;
declare cwsj datetime;
declare cwlx varchar(20);
declare cwbt varchar(100) ;
declare cwnr text;
declare continue handler for SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
set erid := replace(uuid(), '-', '') ;
set cwsj := SYSDATE();
set cwlx := '1';
set cwbt := '旅客外宾一天多处登记处理触发器lkwb_update';
set cwnr := concat('旅客编码:', NEW.lkbm , ',错误码:' , code , ',错误内容:', msg );
INSERT INTO ERROR_LOG VALUES (erid,cwsj,cwlx,cwbt,cwnr);
END;
-- 外宾日期可能特殊
begin
declare continue handler for SQLEXCEPTION
BEGIN
SET i_rows := -1;
END;
set NEW.csyc := TIMESTAMPDIFF(SECOND,NEW.rzrq,STR_TO_DATE(NEW.cssj,'%Y%m%d%H%i'))/(24*3600);
end;
if NEW.tfsj is null or NEW.tfsj='' then
set NEW.sfzz := '1';
else
set NEW.sfzz := '0';
end if;
end;
;;
DELIMITER ;
-- lgfh_del_trigger
DROP TRIGGER IF EXISTS `LGFH_DEL_TRIGGER`;
DELIMITER ;;
CREATE TRIGGER LGFH_DEL_TRIGGER
AFTER DELETE
ON LGFH FOR EACH ROW
insert into LGFH_DEL_RECORD values(old.id);
;;
DELIMITER ;
DROP PROCEDURE IF EXISTS initWbytdcdj;
DELIMITER ;;
create procedure initWbytdcdj ()
begin
declare l_rows INT;
declare s_jdrzsj varchar(10);
declare s_date varchar(12);
declare s_date2 varchar(12);
declare s_zjhm varchar(18);
declare l_count INT;
declare s_now varchar(10);
declare l_num int;
declare r_id varchar(36);
declare r_zwm varchar(30);
declare r_ywm varchar(20);
declare r_ywx varchar(20);
declare r_csrq varchar(8);
declare r_zjlx varchar(20);
declare r_zjlxm varchar(100);
declare r_zjhm varchar(18);
declare r_gj varchar(20) ;
declare r_gjm varchar(100) ;
declare r_rzsj varchar(12);
declare r_rzrq datetime;
declare r_rzcs int;
declare r_xb varchar(20) ;
declare r_xbm varchar(100);
declare r_xtlg varchar(10);
declare c_lg varchar(10);
declare r_lg varchar(10);
declare r_lkbms text;
declare r_jdrzsj varchar(12);
declare r_lkbm varchar(22);
declare r_lg2 varchar(2000);
declare r_rzcs2 int default 0;
DECLARE done int default false;
-- 异常处理
DECLARE code varchar(102) ;
DECLARE msg varchar(102) ;
declare erid varchar(32) ;
declare cwsj datetime;
declare cwlx varchar(20);
declare cwbt varchar(100) ;
declare cwnr text;
declare c cursor for
-- 查询外宾一天多处登记情况
select jdrzsj,zjhm,count(*) x
from lkwb
group by jdrzsj,zjhm having count(*)>1;
declare c2 cursor for
-- 查询外宾一天多处登记情况详情
select a.lkbm,a.lg,a.zwm,a.ywm,a.ywx,a.csrq,a.zjlx,a.zjlxm,a.zjhm,a.gj,a.gjm,a.jdrzsj,a.rzrq,a.xb,a.xbm from lkwb a
where a.rzsj>=s_date and a.rzsj<=s_date2
and a.zjhm=s_zjhm;
-- 游标查询不到数据的情况
DECLARE continue handler for SQLSTATE '02000' set done = true;
declare continue handler for SQLEXCEPTION
ROLLBACK;
BEGIN
GET DIAGNOSTICS CONDITION 1 code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
SET erid := replace(uuid(), '-', '') ;
SET cwsj := SYSDATE();
SET cwlx := '1';
SET cwbt := '旅客外宾一天多处登记初始化过程initWbytdcdj';
SET cwnr := concat('旅客编码:' , NEW.lkbm , ',错误码:' , code , ',错误内容:' , msg);
INSERT INTO ERROR_LOG VALUES (erid,cwsj,cwlx,cwbt,cwnr);
COMMIT;
END;
-- set serveroptput on 才能打印数据
TRUNCATE TABLE cxwbitdcdj;
set s_now := DATE_FORMAT(SYSDATE(),'%Y%m%d');
SELECT '计算外宾登记历史数据...';
OPEN c;
loop_1: loop
fetch c into s_jdrzsj,s_zjhm,l_rows;
if done then
leave loop_1;
end if;
set l_num := l_num + 1;
IF mod(l_num,100)=0 THEN
SELECT CONCAT('...' , l_num);
END IF;
-- 不计算今天的
IF s_jdrzsj!=s_now THEN
set s_date := CONCAT(s_jdrzsj, '0000');
set s_date2 := CONCAT(s_jdrzsj, '2359');
set l_count := 0;
open c2;
loop_2: loop
fetch c2 into r_lkbm,r_lg,r_zwm,r_ywm,r_ywx,r_csrq,r_zjlx,r_zjlxm,r_zjhm,r_gj,r_gjm,r_rzsj,r_rzrq,r_xb,r_xbm;
if done then
leave loop_2;
end if;
set l_count := l_count + 1;
IF l_count=1 THEN
set r_id := replace(uuid(), '-', '');
set c_lg = r_lg;
set r_xtlg := '1'; -- 同一旅馆
set r_lkbms :=r_lkbm; -- 入住流水号
ELSE
-- 流水号相加
set r_lkbms := CONCAT(r_lkbms , ',', r_lkbm);
-- 判断是否是不同旅馆
if r_xtlg='1' AND c_lg != r_lg then
set r_xtlg := '0'; -- 不同旅馆
-- 记录不同旅馆,根据旅客内宾触发器逻辑新增,记录不同旅馆登记次数
set r_rzcs2 := r_rzcs2+1;
set r_lg2 := concat(c_lg , ',' , r_lg);
end if;
END IF;
end loop;
close c2;
set r_rzcs := l_count;
insert into cxwbitdcdj values (r_id,r_zwm,r_ywx,r_ywm,r_xb,r_xbm,r_csrq,r_zjlx,r_zjlxm,r_zjhm,r_gj,r_gjm,r_rzsj,r_rzrq,r_rzcs,r_xtlg,r_lkbms,r_lg,r_rzcs2,r_lg2);
END IF;
END LOOP;
CLOSE c;
COMMIT;
set l_rows := 1;
end;
;;
DELIMITER ;
-- initNbytdcdj 存储过程
DROP PROCEDURE IF EXISTS initNbytdcdj;
DELIMITER ;;
create procedure initNbytdcdj()
begin
declare l_rows INT;
declare s_jdrzsj VARCHAR(12);
declare s_date VARCHAR(12);
declare s_date2 VARCHAR(12);
declare s_zjhm VARCHAR(20);
declare l_count INT;
declare s_now varchar(10);
declare l_num INT;
declare r_id varchar(36);
declare r_xm varchar(100);
declare r_xb varchar(1) ;
declare r_xbm varchar(100);
declare r_csrq varchar(8);
declare r_zjlx varchar(2);
declare r_zjlxm varchar(100);
declare r_zjhm varchar(18);
declare r_ssxq varchar(6) ;
declare r_ssxqm varchar(100);
declare r_rzsj varchar(12);
declare r_rzrq datetime;
declare r_rzcs int;
declare r_xtlg varchar(10);
declare c_lg varchar(10);
declare r_lg varchar(10);
declare r_lkbms text;
declare r_lkbm varchar(22);
declare r_lg2 varchar(2000);
declare r_rzcs2 int default 0;
DECLARE done int default false;
-- 异常处理
DECLARE code varchar(102) ;
DECLARE msg varchar(102) ; declare erid varchar(32) ;
declare cwsj datetime;
declare cwlx varchar(20);
declare cwbt varchar(100) ;
declare cwnr text;
declare c cursor for
-- 查询一天内宾多处登记次数大于1的
select jdrzsj,zjhm,count(*) x
from lknb
group by jdrzsj,zjhm having count(*)>1;
declare c2 cursor for
-- 查询内宾一天多处登记详情信息
select a.lkbm,a.lg,a.xm,a.xb,a.xbm,a.csrq,a.zjlx,a.zjlxm,a.zjhm,a.ssxq,a.ssxqm,a.rzsj,a.rzrq from lknb a
where a.rzsj>=s_date and rzsj<=s_date2
and a.zjhm=s_zjhm;
-- 游标查询不到数据的情况
DECLARE continue handler for SQLSTATE '02000' set done = true;
declare continue handler for SQLEXCEPTION
ROLLBACK;
BEGIN
GET DIAGNOSTICS CONDITION 1 code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
SET erid := replace(uuid(), '-', '') ;
SET cwsj := SYSDATE();
SET cwlx := '1';
SET cwbt := concat('旅客内宾一天多处登记初始化过程initNbytdcdj');
SET cwnr := concat('旅客编码:' , NEW.lkbm , ',错误码:' , code , ',错误内容:' , msg);
INSERT INTO ERROR_LOG VALUES (erid,cwsj,cwlx,cwbt,cwnr);
COMMIT;
END;
-- set serveroptput on 才能打印数据
TRUNCATE TABLE cxnbitdcdj;
SET s_now := DATE_FORMAT(SYSDATE(),'%Y%m%d');
OPEN c;
loop_1:loop
fetch c into s_jdrzsj,s_zjhm,l_rows;
if done then
leave loop_1;
end if;
SET l_num := l_num + 1;
IF mod(l_num,100)=0 THEN
SELECT CONCAT('...' , TO_CHAR(l_num));
END IF;
-- 不计算今天的
IF s_jdrzsj!=s_now THEN
SET s_date := CONCAT(s_jdrzsj , '0000');
SET s_date2 := CONCAT(s_jdrzsj , '2359');
SET l_count := 0;
open c2;
loop_2:loop
fetch c2 into r_lkbm,r_lg,r_xm,r_xb,r_xbm,r_csrq,r_zjlx,r_zjlxm,r_zjhm,r_ssxq,r_ssxqm,r_rzsj,r_rzrq;
if done then
leave loop_2;
end if;
set l_count := l_count + 1;
IF l_count=1 THEN
set r_id := replace(uuid(), '-', '');
set c_lg = r_lg;
set r_xtlg := '1'; -- 同一旅馆
set r_lkbms := r_lkbm; -- 入住流水号
ELSE
-- 流水号相加
set r_lkbms := CONCAT(r_lkbms , ',', r_lkbm);
-- 判断是否是不同旅馆
if r_xtlg='1' AND c_lg != r_lg then
set r_xtlg := '0'; -- 不同旅馆
-- 记录不同旅馆,根据旅客内宾触发器逻辑新增,记录不同旅馆登记次数
set r_rzcs2 := r_rzcs2+1;
set r_lg2 := concat(c_lg , ',' , r_lg);
end if;
END IF;
end loop;
close c2;
set r_rzcs := l_count;
insert into cxnbitdcdj values (r_id,r_xm,r_xb,r_xbm,r_csrq,r_zjlx,r_zjlxm,r_zjhm,r_ssxq,r_ssxqm,r_rzsj,r_rzrq,r_rzcs,r_xtlg,r_lkbms,r_lg,r_rzcs2,r_lg2);
END IF;
END LOOP;
CLOSE c;
COMMIT;
set l_rows := 1;
end;
;;
DELIMITER ;