2014-10-16 39 views
1

我對spring jdbc完全陌生,遇到了需要將spring jdbc配置文件中使用的sql轉換爲plain db2格式的情況。 例如,下面是在spring conf文件中使用的查詢(內部值標記),我想更改爲針對db2運行,我瀏覽了許多spring文檔,但沒有找到任何相關信息,有人能指向我的鏈接或解決這個SQL格式化將Spring jdbc中使用的查詢轉換爲db2

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

    <!-- JNDI DataSource for J2EE environment. --> 
    <bean id="mi.conv.batch.dataSource" class="org.springframework.jdbc.datasource.WebSphereDataSourceAdapter"> 
     <property name="targetDataSource"> 
      <jee:jndi-lookup id="dataSourceInternal" jndi-name="java:comp/env/jdbc/Database" /> 
     </property> 
    </bean> 

    <!-- Transaction manager that delegates to JTA (use it when running within a container) -->  
    <bean id="mi.conv.batch.TransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/> 


    <!-- Transactional proxy for data access facade. --> 
    <bean id="mi.conv.batch.transactionProxyParent" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> 
     <property name="transactionManager" ref="x.y.z.TransactionManager"/> 
     <property name="transactionAttributes"> 
      <props> 
       <prop key="add*">PROPAGATION_REQUIRED</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="statementsDao" parent="x.y.z.transactionProxyParent"> 
     <property name="target"> 
      <bean class="abc.test.TestDaoImpl"> 
       <property name="dataSource" ref="a.b.c.dataSource"/> 
       <property name="insertNotificationPreferenceSql"> 
        <value> 
         select 
          NOTIFICATIONKY 
         from new table (
          insert into 
           NOTIFICATION (
            ID, 
            PERSONKY, 
            INSTANCEKY 
           ) 
           **select 
            (substr(ba.CODE, 1, 2) || '1111' || RIGHT(ba.CODE, 4) || 
             (case substr(ba.CODE, 1, 2) 
              when 'XY' then '' 
              else '2222' 
             end) 
            || '0000' || ba.ACCTID) as ID, 
            cp.PERSONKY, 
            ba.INSTANCEKY 
           from 
            BCSACCT ba 
            join 
            PERSON cp on (1=1) 
           where cp.PERSONKY = :personId 
            and ba.INSTANCEKY = :prodinstId** 
         )      
        </value>     
       </property>           
      </bean> 
     </property> 
    </bean> 


</beans> 
+0

值標記中的查詢是DB2 SQL。 – 2014-10-16 16:18:12

+0

您對DB2以外的數據庫使用了相同的SQL嗎?如果你需要一個SQL語句用於DB2,而另一個用於另一個數據庫,則必須單獨複製Spring XML配置文件並維護這些語句,除非您使用的是支持配置文件的較新的Spring,在這種情況下,您可以應用每個「」的配置文件名稱需要某些特定於DB2的內容。 – ngreen 2014-10-16 20:19:04

+0

沒有它的單個數據庫,謝謝澄清它澄清了我所有的事情 – Rakesh 2014-10-17 05:57:01

回答

1

我認爲普通的DB2 formated sql將是您的「值」引號內的字符串實體。我嘗試通過連接到數據庫的TEST副本的SQL客戶端運行完全相同的查詢,並查看是否獲得了您正在查找的結果。

select NOTIFICATIONKY 
from new table (
    insert into NOTIFICATION (
     ID, 
     PERSONKY, 
     INSTANCEKY 
    ) 
    **select (substr(ba.CODE, 1, 2) || 
       '1111'     || 
       RIGHT(ba.CODE, 4)  || 
       (case substr(ba.CODE, 1, 2) when 'XY' then '' else '2222' end) || 
       '0000'     || 
       ba.ACCTID) as ID, 
       cp.PERSONKY, 
       ba.INSTANCEKY 
    from BCSACCT ba 
    join PERSON cp on (1=1) 
    where cp.PERSONKY = :personId 
    and ba.INSTANCEKY = :prodinstId** 
)      

您可能會遇到SQL語句問題;我試圖清理一下壓痕以理解它。這個語句將行插入一個名爲「NOTIFICATION」的表中,其中ID字段是從BCSACCT.CODE或BCSACCT.ACCTID的一堆ORs中弄出來的,PERSONKY和INSTANCEKY非常簡單。

快速說明: Spring Bean是使用Spring Inversion of Control Container進行管理的java類。 xml文件中的信息定義了Spring在初始化類時相關Java Bean使用的幾個屬性。在您的XML文件中,類似代碼中的Java bean引用屬性「insertNotificationPreferenceSql」。 JDBCTemplate類實際上運行SQL,但需要調查Java Bean代碼以查看該屬性實際上是如何使用的。