当前位置:数据库 > Oracle >>

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

使用ibatis将数据库从oracle迁移到mysql的几个修改点
 
  我们项目在公司的大战略下需要从oracle迁移到mysql,我们的项目使用的是ibatis,在ibatis层上需要的一些修改点如下:单元测试框架我们用的是jtester。
  
[java] 
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类型。  
 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,