数据库同步,配置两个数据源(mysql)

spring的applicationContext.xml的配置信息:

    <!-- 数据源1 -->
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="defaultAutoCommit" value="true" />
        <property name="defaultTransactionIsolation" value="4" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="validationQuery" value="/* ping */ select 1" />
        <property name="testWhileIdle" value="true" />
    </bean>

    <!-- 事务管理器 -->
    <bean id="serviceTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- Mybatis配置 -->  
    <bean id="serviceSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations">
            <array>
                <value>classpath*:sql/**/*.xml</value>
            </array>
        </property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:conf/service-mybatis.xml"></property>
    </bean>
    <mybaits-spring:scan base-package="com.zntz.web.admin.dao"
        factory-ref="serviceSessionFactory" />

    <!-- 数据源2 -->
    <bean id="dataSource2" class="org.apache.tomcat.jdbc.pool.DataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${jdbc2.url}"/>
        <property name="username" value="${jdbc2.username}"/>
        <property name="password" value="${jdbc2.password}"/>
        <property name="defaultAutoCommit" value="true" />
        <property name="defaultTransactionIsolation" value="4" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="validationQuery" value="/* ping */ select 1" />
        <property name="testWhileIdle" value="true" />
    </bean>

    <!-- 事务管理器 -->
    <bean id="serviceTransactionManager2"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource2"></property>
    </bean>

    <!-- Mybatis配置 -->  
    <bean id="serviceSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations">
            <array>
                <value>classpath*:sql2/**/*.xml</value>
            </array>
        </property>
        <property name="dataSource" ref="dataSource2"></property>
        <property name="configLocation" value="classpath:conf/service-mybatis.xml"></property>
    </bean>
    <mybaits-spring:scan base-package="com.zntz.web.admin.dao2"
        factory-ref="serviceSessionFactory2" />

controller类:

package com.zntz.web.admin.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zntz.web.admin.model.base.ResultModel;
import com.zntz.web.admin.model.vo.system.EstCityInfoVo;
import com.zntz.web.admin.service.system.EstCityInfoService;

@Controller
@RequestMapping("/syn")
public class SynController {

    @Autowired
    EstCityInfoService estCityInfoService;

    @RequestMapping(value = "/syn", method = RequestMethod.GET)
        public String syn() {
            return "syn/estCityInfo_syn";
        }

    /**
     * 去同步
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/synEstCityInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultModel syn(EstCityInfoVo vo) {
        ResultModel result = new ResultModel();

            try {
                List<EstCityInfoVo> list = estCityInfoService.getAll();
                for (int i = 0; i < list.size(); i++) {

                vo.setCity(list.get(i).getCity());
                vo.setLevel(list.get(i).getLevel());
                vo.setSecondIncrement(list.get(i).getSecondIncrement());
                vo.setThirdIncrement(list.get(i).getThirdIncrement());
                vo.setConstructCost(list.get(i).getConstructCost());
                vo.setUpdateTime(list.get(i).getUpdateTime());
                estCityInfoService.insertEstCityInfoVo(vo);

                }
            } catch (Exception e) {
                result.setCode("1");
            }

        return result;
    }

}

配置两套持久层以及mapper类。

1 与数据库A建立连接
2 从A取得数据
3 处理这些数据
4 与数据库B建立连接
5 将处理后的数据插入B中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值