查詢A庫數據插入至B庫中

***********任務需求*********************************
從A系統中查詢出信息插入到B系統數據庫中

知識點1---------不在同一個數據庫操作
  1.因爲不止在一個數據庫中操作,所以要配置多個數據源.
<context:property-placeholder location="classpath:jdbc.properties"/>    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"  
    p:removeAbandoned="${jdbc.removeAbandoned}" p:removeAbandonedTimeout="${jdbc.removeAbandonedTimeout}"
    p:maxWait="${jdbc.maxWait}"  
    p:username="${jdbc.username}" p:password="${jdbc.password}"/>
一開始的想法是再複製一個,如下
<context:property-placeholder location="classpath:jdbc1.properties"/>    
    <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"  
    p:removeAbandoned="${jdbc.removeAbandoned}" p:removeAbandonedTimeout="${jdbc.removeAbandonedTimeout}"
    p:maxWait="${jdbc.maxWait}"  
    p:username="${jdbc.username}" p:password="${jdbc.password}"/>
但是
    Spring容器僅允許最多定義一個PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其餘的會被Spring忽略掉
於是成了這樣
<context:property-placeholder location="classpath:jdbc*.properties"/>    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"  
    p:removeAbandoned="${jdbc.removeAbandoned}" p:removeAbandonedTimeout="${jdbc.removeAbandonedTimeout}"
    p:maxWait="${jdbc.maxWait}"  
    p:username="${jdbc.username}" p:password="${jdbc.password}"/>
<!-- 第二個數據源  -->    
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc1.driverClassName}" p:url="${jdbc1.url}"  
    p:removeAbandoned="${jdbc1.removeAbandoned}" p:removeAbandonedTimeout="${jdbc1.removeAbandonedTimeout}"
    p:maxWait="${jdbc1.maxWait}"  
    p:username="${jdbc1.username}" p:password="${jdbc1.password}"/>
數據源的配置文件格式大致如下(當然兩份文件配置不一樣):
    jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
    jdbc.url=jdbc:oracle:thin:@***.***.***.100:1521:databaseName
    jdbc.username=user
    jdbc.password=pass
    jdbc.maxWait=60000
    jdbc.removeAbandoned=true
    jdbc.removeAbandonedTimeout=180
實際中可能***.***.***.100是一個數據地址,***.***.***.120是一個數據庫地址,

因爲使用JdbcTemplate,給其配置一個數據源,剛開始是這樣寫的:
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource1">
        <ref local="dataSource1" />
    </property>
</bean>

而name="dataSource"中dataSource是預定義的,這樣是不行的,然後成了這樣
<!-- 第二個數據源的jdbcTemplate配置的數據源 -->
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">  <!--dataSource是預定義的,不能改爲dataSource1 -->
        <ref local="dataSource1" />
    </property>
</bean>

<!-- 第二個數據源jdbcTemplate1 -->
<bean id="accInfoToBfjServiceImpl"
    class="cn.com.sandpay.amms.manage.dataSyn.service.impl.AccInfoToBfjServiceImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate1" />  <!jdbcTemplate,不能改爲dataSource1 -->
</bean>    
<!-- 第二個數據源事務管理 -->
<bean id="txManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1" />
</bean>

insert插入,update修改,delete刪除需要進行事務管理。!切記!切記!切記!切記!切記!切記
數據源的事務管理:
<!-- <tx:annotation-driven transaction-manager="txManager"/>
這句話的作用是註冊事務註解處理器,我這裏沒有在這裏配置,是用註解配置的
@Transactional(value="txManager", propagation=Propagation.REQUIRES_NEW)
public void funName() {}
-->

知識點2-----------------JdbcTemplate的使用
這個因爲在前面已經配置好了,這裏的使用很簡單
方法1
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
        this.jdbcTemplate = jdbcTemplate;  
    }
方法2
    @Autowired private JdbcTemplate jdbcTemplate;  
調用
    jdbcTemplate.update(insertSql,params)
參數
    private static String insertSql = "insert into tab_Name values("?,?,?");
    Object[] params = dataList.get(i);

知識點3:-----------一個關於事務管理的,但是不懂,先不寫了









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章