数据库交接项目随笔

项目用的纯spring框架,主要实现的是将一个数据库的数据(几张表)提存到另一数据库(一张表),并设置了实时任务。

spring 里的配置:applicationContext-src.xml

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="byName" default-lazy-init="false">

    <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
	<context:component-scan base-package="com.**.task"/>
	
   	<bean id="run" class="com.wonders.qlyg.engine.task.Run">
		<property name="taskMap" ref="taskMap" />
	</bean>
	
	<bean id="taskMap" class="java.util.LinkedHashMap">
		<constructor-arg>  
			<map>  
				<entry key="yzData" value-ref="yzData" />
			</map>
		</constructor-arg>
	</bean>
    
</beans>
根据bean 的ref 找到指定的bean

yangzhong-task.xml

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="byName" default-lazy-init="false">

    <!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
	<context:component-scan base-package="com.**.task"/>

    <bean id="yzData" class="com.wonders.qlyg.engine.task.ExchangeScheduler">
    	<constructor-arg index="0" value="扬中" />
		<constructor-arg index="1" value="2011-08-28 11:57" />  <!--启动时间 -->
		<constructor-arg index="2" value="1" />  <!--间隔时间 -->
		<constructor-arg index="3" value="day" />  <!--间隔时间单位 -->
		<constructor-arg index="4" value="src/config/exchange/myexchange_yangzhong.xml" />   <!--数据交换xml -->
		<constructor-arg index="5"> <!-- 交换用的Manager -->
			<ref bean="yZDirDatabaseManager" />
		</constructor-arg>
		<constructor-arg index="6"> <!-- 交换用的Manager -->
			<ref bean="baseSrcManager" />
		</constructor-arg>
    </bean>
     
</beans>
其他配置

applicationContext-src.xml

<?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:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:util="http://www.springframework.org/schema/util"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" 
	   default-autowire="byName" default-lazy-init="false">

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="location" value="classpath:config/spring/jdbc.properties"/>
    </bean>
    <!-- 
	<bean id="srcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${src.jdbc.driver}"/>
        <property name="url" value="${src.jdbc.url}"/>
        <property name="username" value="${src.jdbc.username}"/>
        <property name="password" value="${src.jdbc.password}"/>
    </bean>
 -->
    <bean id="srcDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName"  value="${src.jdbc.driver}"  />
	    <property name="url" value="${src.jdbc.url}"  />
	    <property name="username" value="${src.jdbc.username}"  />
	    <property name="password" value="${src.jdbc.password}"  />
	    <property name="maxActive" value="3" />  
		<property name="maxWait"  value="20000"  />
	</bean>

	<bean id="srcTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="srcDataSource"/>
    </bean>
    
	<!-- 以AspectJ方式 定义 AOP -->
	<aop:config proxy-target-class="true">
		<aop:advisor pointcut="execution(* com.*..*.service.*Manager.*(..))" advice-ref="srcTxAdvice" />
	</aop:config>
	<!-- 基本事务定义,使用transactionManager作事务管理,默认get* find*方法的事务为readonly,其余方法按默认设置. 
		默认的设置请参考Spring文档事务一章. -->
	<tx:advice id="srcTxAdvice" transaction-manager="srcTxManager">
		<tx:attributes>
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="*" read-only="false" />
		</tx:attributes>
	</tx:advice>
	
	<tx:annotation-driven transaction-manager="srcTxManager" proxy-target-class="true"/>
	
	<!-- 时间转换格式 sping注册 -->
	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean class="com.wonders.qlyg.engine.util.DatePropertyEditor">
						<property name="format">
							<value>yyyy-MM-dd hh:mm</value>
						</property>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>
启动类

public class YangzhongRun
{
  public static void main(String[] args)
  {
    ApplicationContext context = new ClassPathXmlApplicationContext("config/spring/*.xml");//扫描spring
    Run run = (Run)context.getBean("run");//获取id为‘run’的bean
    for (Scheduler scheduler : run.getTaskMap().values())
      scheduler.scheduleTask();
  }
}

这就是数数据库交接的项目的spring的配置,至于具体的项目中用到SQL数据在具体的交换xml 中记录

我们为什么需要写这样的一个Java项目呢?有时候你可能会觉得没有必要,当你操作几十张表和几十万条数据的时候,就会明白这种提取数据的高效性,这个效果作用与存储过程有点类似。


将文件打成jar包,然后安装java虚拟机运行

//这个里面编辑的命令可以是你允许Java虚拟机在cmd里面的编辑命令
d:    //进入d盘
cd   javaxuniji //进入javaxuniji文件夹
java -jar sjkjj.jar //执行jar文件

最后补充一点批处理文件的知识,我们可以将运行java的命令写成一个批处理文件(cmd或者bat结尾的),以后你就可以双击那个文件执行,这就有点类似于一个桌面软件的双击运行了。

新建文本文档-》修改后缀.bat--》然后在里面编辑以上的命令

//这个里面编辑的命令可以是你允许Java虚拟机在cmd里面的编辑命令
d:    //进入d盘
cd   javaxuniji //进入javaxuniji文件夹
java -jar sjkjj.jar //执行jar文件



下面是博主遇到的错误写的提示方式:

for (Map paramMap : srcList) {
        try {
          this.qlygManager.generate(paramMap, dr);
        } catch (Exception e) {
          log.error(">>>>", e);//打印出错误日志
          log.error(e.getMessage());
        }
      }
//打印出插入日志
log.info(">>>>>>>>>>>>>"+dataReference.getInsertSql());
log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + paramMap.get("BUSINESSID"));//打印插入的数据主键ID






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值