使用ibatis将数据库从oracle迁移到mysql的几个修改点

  

  我们项目在公司的大战略下需要从oracle迁移到mysql,我们的项目使用的是ibatis,在ibatis层上需要的一些修改点如下:单元测试框架我们用的是jtester。

  

1.	插入主键生成
Oracle insert时主键id是使用sequence方式:
<insert id="MS-BRANDMEMBER-INSERT" parameterClass="TA-brandMember">
<selectKey resultClass="long" keyProperty="id">
      SELECT seq_industry_brand_member.nextval FROM DUAL
</selectKey>
     insert into industry_brand_member (id, gmt_create, gmt_modified, member_id, bu, active, brand_id, cat_ids, auth_date_from, auth_date_to, auth_level, area,      status, operator, original) values (#id#, sysdate, sysdate, #memberId#, #mallId#, #active#, #brandId#, #catIds#, #authDateFrom#, #authDateTo#, #authLevel#, #area#, #status#, #operator#, #original#)
</insert>
        mysql需要修改为:
<insert id="MS-BRANDMEMBER-INSERT" parameterClass="TA-brandMember">
insert into industry_brand_member (gmt_create, gmt_modified, member_id, mall_id, active, brand_id, cat_ids, auth_date_from, auth_date_to, auth_level, area, status, operator, original)
values (now(), now(), #memberId#, #mallId#, #active#, #brandId#, #catIds#, #authDateFrom#, #authDateTo#, #authLevel#, #area#, #status#, #operator#, #original#)
<selectKey resultClass="java.lang.Long" keyProperty="id">
      SELECT LAST_INSERT_ID() AS ID
</selectKey>
</insert>
2.	分页
Oracle采用三层嵌套的分页方式,用的是偏移量的最小值和最大值获取分页内容:
<select id="SELECT-BRANDIDLIST-BY-MALLID" resultClass="long" parameterClass="java.util.Map">
select brandId from(select brandId,rownum as no from(select distinct(brand_id) as brandId from industry_brand_member where 
<isNotNull property="mallId">
     bu = #mallId:VARCHAR# AND
</isNotNull>
<isNotNull property="status">
    status=#status:VARCHAR# AND
</isNotNull>
brand_id is not null order by brand_id asc))
<![CDATA[
   where no>=#start# and no<#end#
]]>
</select>
          Mysql分页采用limit,offset语法,使用偏移量和每页条数
<select id="SELECT-BRANDIDLIST-BY-MALLID" resultClass="long" parameterClass="java.util.Map">
select distinct(brand_id) as brandId from industry_brand_member where 
<isNotNull property="mallId">
    mall_id = #mallId:VARCHAR# AND
</isNotNull>
<isNotNull property="status">
    status=#status:VARCHAR# AND
</isNotNull>
brand_id is not null 
order by brand_id asc
limit #start#,#pageSize# 
</select>

3.	日期函数
Oracle与mysql的日期API还是有很大差异的,对于日期的操作需要全部检查一遍。我们只用到下面三个日期的方法:
Oracle	Mysql	功能
to_char(last_offer_paytime,'yyyy/MM/dd')	date_format(last_offer_paytime,'%Y/%m/%d')	日期转为字符串
to_date(#searchDate#, 'yyyy/MM/dd')	str_to_date(#searchDate#, '%Y/%m/%d')	字符串转为日期
to_date(#searchDate#, 'yyyy/MM/dd') + 1	date_add(str_to_date(#searchDate#, '%Y/%m/%d'),interval 1 day)	日期相加
  
4.	质量保证
可能大部分sql都会做或多或少的修改,如何保证质量,我们这边对每个sql都做了单元测试,之前可能一些没做的也全部补起来了,辛苦袁飞,程达做了很充分的单元测试,极大的保证了迁移质量,甚至发现了原来隐藏的一些问题。对于迁移来说,单元测试是最好的质量保证方法。
使用jtester封装的DbFit来做DAO的单元测试
@Test
@DbFit(when="wiki/sampleshow/BuyerDAO.insert.when.wiki", then="wiki/sampleshow/BuyerDAO.insert.then.wiki")
public void TestInsertSampleShowBuyer() {
    SampleShowBuyer SampleShowBuyer = new SampleShowBuyer();
    SampleShowBuyer.setMemberId("yanhandle");
    SampleShowBuyer.setPhoneNumber("13512345678");
    Long id = sampleShowBuyerDAO.insertSampleShowBuyer(SampleShowBuyer);
    want.object(id).notNull();
    want.number(id).greaterThan(0l);
}
       单元测试的工作量占了去O工作量的2/3,平均每个表需要0.8人日,当然这个还要看sql的数量和复杂度。建议还是需要做单元测试来保证质量。

5.	数据类型的差异,建表通过idb就可以完成。不过brand有一个配置字段用到了Oracle的CLOB类型,mysql则需要使用text类型。
 

转载于:https://www.cnblogs.com/zhwj184/archive/2013/04/29/3119608.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值