Home > Database > Mysql Tutorial > body text

采用Hibernate框架的研发平台如何能够真正兼容Oracle和sqlServer

WBOY
Release: 2016-06-07 15:21:03
Original
1073 people have browsed it

都说Hibernate框架的使用可以很容易的让你的研发平台支持多种不同类型的数据库,但实践表明,这里的容易,是相对的。 想让研发平台支持多种数据库,并不是一件简单的事,也可以这么说:并不是只要使用了Hibernate框架就能实现的。 下面记录一下我做这件事情

  都说Hibernate框架的使用可以很容易的让你的研发平台支持多种不同类型的数据库,但实践表明,这里的“容易”,是相对的。

  想让研发平台支持多种数据库,并不是一件简单的事,也可以这么说:并不是只要使用了Hibernate框架就能实现的。

  下面记录一下我做这件事情的过程和一些感悟。

  当我接到该任务时,我先大致的理了一下思路:

  要完成迁移,总体上有2大块工作要做,分别是:数据库层面的迁移  和  平台底层代码的改造

  一、数据库层面的迁移过程:

  1、通过sqlServer Studio2008 工具将数据从Oracle导入到SqlServer数据库

  从SSMS2008开始才支持此功能,具体操作步骤(右键点击数据库-选择导入-点下一步-选择 Oracle Provider for OLE DB 数据源-点击属性-填写数据源,格式为 IP:端口/实例名),后面的步骤根据向导一步步的操作即可。需要注意的是在 选择源表和源视图的步骤中:

  (1)、要把【目标】列中的默认前缀去掉,这样导入的表才会默认关联到dbo下,否则你每次查询表都要带上schema前缀,导致你之前的应用程序中的sql无法执行,因为你之前写的那些sql肯定不会带这种前缀。

  (2)、先勾选你要导入的源,然后双击每一行记录,在弹出的对话框中检查是否所有的类型都正确绑定好了,我在检查的时候就遇到了oracle中是varchar2类型的,在该对话框显示的表结构中变成了130,只能手动的去将所有130改成varchar类型(sqlserver里没有varchar2类型)。还有原来是clob类型的,现在变成了varchar,要手动改成text类型(因为clob类型的字段比较少,所以可以通过在oracle中执行“select * from user_tab_columns c where c.data_type='CLOB';”来查看哪些表中用到了CLOB类型的字段)。

  2、增加to_date、to_char、to_number、concat等常用的函数

说明:我在编写to_date函数的时候,只提供了一种格式“yyyy-mm-dd HH:mi:ss”,这是因为在sqlserver中是没有和to_date函数的类似的函数的,只能使用convert函数实现,但是convert函数不支持传入格式化字符串,只能传入格式字符对应的整型数字,而120对应的正是之前提到的“yyyy-mm-dd HH:mi:ss”格式;另外此次是迁移到Sqlserver2005,该版本是没有内嵌concat函数的,根据官方文档的说法,是从sqlServer2012开始才有concat函数的,所以这里我要自己编写一个concat函数。

<span>--</span><span>----------------------------------------------------------------concat函数</span>
<span>USE</span> <span>[</span><span>skyplatform</span><span>]</span>
<span>GO</span>
<span>/*</span><span>***** Object:  UserDefinedFunction [dbo].[concat]    Script Date: 03/10/2015 17:11:31 *****</span><span>*/</span>
<span>SET</span> ANSI_NULLS <span>ON</span>
<span>GO</span>
<span>SET</span> QUOTED_IDENTIFIER <span>ON</span>
<span>GO</span>
<span>CREATE</span> <span>FUNCTION</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>concat</span><span>]</span><span>
(
</span><span>@param1</span> <span>varchar</span>(<span>500</span><span>),
</span><span>@param2</span> <span>varchar</span>(<span>500</span><span>)
)
</span><span>returns</span> <span>varchar</span>(<span>1000</span><span>)
</span><span>as</span>
<span>begin</span>
<span>DECLARE</span> <span>@returntext</span> <span>varchar</span>(<span>1000</span><span>)
    </span><span>if</span> (<span>@param1</span> <span>is</span> <span>null</span><span>)
    </span><span>SELECT</span> <span>@returntext</span><span>=</span> <span>@param2</span><span>;
    </span><span>else</span> <span>if</span> (<span>@param2</span> <span>is</span> <span>null</span><span>) 
     </span><span>SELECT</span> <span>@returntext</span><span>=</span> <span>@param1</span><span>;
    </span><span>else</span>
     <span>SELECT</span> <span>@returntext</span><span>=</span> <span>@param1</span> <span>+</span> <span>@param2</span><span>;
   </span><span>return</span> <span>@returntext</span><span>;
</span><span>end</span>

<span>--</span><span>------------------------------------------------------------------to_char函数</span>
<span>USE</span> <span>[</span><span>skyplatform</span><span>]</span>
<span>GO</span>
<span>/*</span><span>***** Object:  UserDefinedFunction [dbo].[to_char]    Script Date: 03/10/2015 17:12:09 *****</span><span>*/</span>
<span>SET</span> ANSI_NULLS <span>ON</span>
<span>GO</span>
<span>SET</span> QUOTED_IDENTIFIER <span>ON</span>
<span>GO</span>
<span>CREATE</span> <span>FUNCTION</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>to_char</span><span>]</span><span>
(
</span><span>@param1</span> <span>datetime</span><span>,
</span><span>@param2</span> <span>varchar</span>(<span>20</span><span>)
)
</span><span>returns</span> <span>varchar</span>(<span>20</span><span>)
</span><span>as</span>
<span>begin</span>

<span>return</span> <span>convert</span>(<span>varchar</span>(<span>20</span>),<span>@param1</span>,<span>120</span><span>)
    
</span><span>end</span>

<span>--</span><span>------------------------------------------------------------------to_date函数</span>
<span>USE</span> <span>[</span><span>skyplatform</span><span>]</span>
<span>GO</span>
<span>/*</span><span>***** Object:  UserDefinedFunction [dbo].[to_date]    Script Date: 03/10/2015 17:12:58 *****</span><span>*/</span>
<span>SET</span> ANSI_NULLS <span>ON</span>
<span>GO</span>
<span>SET</span> QUOTED_IDENTIFIER <span>ON</span>
<span>GO</span>
<span>CREATE</span> <span>FUNCTION</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>to_date</span><span>]</span><span>
(
</span><span>@param1</span> <span>varchar</span>(<span>20</span><span>),
</span><span>@param2</span> <span>varchar</span>(<span>20</span><span>)
)
</span><span>returns</span> <span>datetime</span>
<span>as</span>
<span>begin</span>

<span>return</span> <span>convert</span>(<span>datetime</span>,<span>@param1</span>,<span>120</span>)<span>--</span><span>120 means that yyyy-mm-dd hh:mi:ss(24h)</span>
    
<span>end</span>
<span>--</span><span>------------------------------------------------------------------to_number函数</span>
<span>USE</span> <span>[</span><span>skyplatform</span><span>]</span>
<span>GO</span>
<span>/*</span><span>***** Object:  UserDefinedFunction [dbo].[to_number]    Script Date: 03/10/2015 17:13:09 *****</span><span>*/</span>
<span>SET</span> ANSI_NULLS <span>ON</span>
<span>GO</span>
<span>SET</span> QUOTED_IDENTIFIER <span>ON</span>
<span>GO</span>
<span>CREATE</span> <span>FUNCTION</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>to_number</span><span>]</span><span>
(
</span><span>@param1</span> <span>varchar</span><span>
)
</span><span>returns</span><span> numeric
</span><span>as</span>
<span>begin</span>

<span>return</span> <span>convert</span>(numeric,<span>@param1</span><span>)
    
</span><span>end</span>
Copy after login

  二、平台底层代码的改造

  1、引入SqlServer的jar包:sqljdbc4-4.0.jar

<span><span>groupId</span><span>></span>com.microsoft.sqlserver<span></span><span>groupId</span><span>></span>
<span><span>artifactId</span><span>></span>sqljdbc4<span></span><span>artifactId</span><span>></span>
<span><span>version</span><span>></span>4.0<span></span><span>version</span><span>></span></span></span></span>
Copy after login

  2、修改db.properties中关于数据库连接信息的配置

<span>jdbc.dialect=org.hibernate.dialect.SQLServerDialect
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=xxx
jdbc.default_schema=dbo
jdbc.username=xxx
jdbc.password=xxx</span>
Copy after login

  3、修改平台中使用的一些非sql标准的语法

  在使用delete insert update这些dml语句的时候,切记不要使用别名,因为在oracle和sqlserver中,这些dml语句使用别名的语法是不一样的。

  4、各实体类主键策略的改造

  最好都使用string类型的主键,但是因为之前的代码中都用的sequence做主键策略,现在改成string类型工作量势必很大,所以决定使用table策略来兼容各种数据库。

  5、dao层对sql的处理

  由于sqlserver中调用自定义标量值函数,必须在函数名前加上dbo.的前缀,但是这样写势必会导致不能兼容其它的关系型数据库,所以只能从dao实现层,对sql进行统一的处理,处理规则就是:如果当前数据库是sqlserver,并且sql中出现了concat、to_date、to_char、to_number等函数,就为这些函数名加上dbo.的前缀。

  以上做完,基本就可以让平台在sqlserver数据库上跑了,同时也可以通过改配置文件切换到Oracle数据库。

  以上的做法可能并不是最优的方式,如果有更好的方案,希望各位大牛能给予指点。

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template