主要设计思想:以列为单位,将对应关系分离出来,主要关注关系的分离及实现,目的是将关系做成可配置的插件,以实现关系的复用。框架通过xml文件来进行关系的描述。数据移植的目的:处理两个库之间的数据对应。所谓数据对应,就是列的多对多的对应关系。
数据对应的结构见下图:
数据对应的处理见下图:
所谓关系,可以简单的理解为一个方法,方法有三个参数:源列(多个,是用数组来描述)、目的列(多个,是用数组来描述)、传递的参数(用作关系的扩展)。
列是一个对象,如下图:
其中,源列和目的列均为列的一种表现。
对列的操作就是对列这些属性的设置及获取。
框架以池的形式管理列, 提供了列的初始化功能,所谓列的初始化,就是根据配置文件,创建列对象,并初始化列数据,保证配置文件中的列在列池中存在。
对于列的表名属性,所谓表名,只是自定义的一个id,可以但不仅限于数据库中实际的表名。
框架提供了表的概念,表可以是数据库中实际存在的单个表,也可以是自定义的关联表,但每个表必须有一个id属性,并要注意id的唯一性。表的配置如下:
<table id="loan.LG_HTGL" name="loan.LG_HTGL" type="normal">
<condition>WHERE substring(bz,1,1)!='4' and substring(bz,2,1)!='0'</condition>
</table>
数据对应的结构见下图:
数据对应的处理见下图:
所谓关系,可以简单的理解为一个方法,方法有三个参数:源列(多个,是用数组来描述)、目的列(多个,是用数组来描述)、传递的参数(用作关系的扩展)。
列是一个对象,如下图:
其中,源列和目的列均为列的一种表现。
对列的操作就是对列这些属性的设置及获取。
框架以池的形式管理列, 提供了列的初始化功能,所谓列的初始化,就是根据配置文件,创建列对象,并初始化列数据,保证配置文件中的列在列池中存在。
对于列的表名属性,所谓表名,只是自定义的一个id,可以但不仅限于数据库中实际的表名。
框架提供了表的概念,表可以是数据库中实际存在的单个表,也可以是自定义的关联表,但每个表必须有一个id属性,并要注意id的唯一性。表的配置如下:
<table id="loan.LG_HTGL" name="loan.LG_HTGL" type="normal">
<condition>WHERE substring(bz,1,1)!='4' and substring(bz,2,1)!='0'</condition>
</table>
看看使用该框架写出来的部分移植代码吧3.1 程序入口
移植程序的入口,Main方法:
import org.cafecup.common.datatran.constant.Constants;
import org.cafecup.common.datatran.manage.ConfigurationManager;
import org.cafecup.common.datatran.manage.DBManage;
import org.cafecup.common.datatran.manage.TableManager;public static void main(String[] args) { try {
/**
* 根据配置文件进行处理
*/
Constants.COMPAREFILE = "comparedata.xml";
FileInputStream fstream = new FileInputStream("credit_contract_tabledesc.xml");
ConfigurationManager.getInstance().parseAndDeal(fstream);
/**
* 设置为自动填充不满足条件的列 设置文件生成目录
*/
TableManager.setToAutoFill();
Constants.DIRECTORY = "F:/数据移植/data/2006-09-28/";
ConfigurationManager.getInstance().produceFiles(); System.out.println("数据库连接关闭状态:"
+ DBManage.getInstance().freeConnections());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
接下来的:3.2 配置文件
字段对应关系的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database-trans SYSTEM "TranDescription.dtd">
<database-trans>
<definition>
<sourcedb>
<driver>com.sybase.jdbc2.jdbc.SybDriver</driver>
<url>jdbc:sybase:Tds:10.10.10.74:5000/loan?charset=cp936</url>
<user>l</user>
<password>l</password>
<tables>
<!-- 台帐信息 -->
<table id="tzdjb" name="type为custom时无意义,不写都可以" type="custom">
<condition>select tzdj.*,fhxx.lv,fhxx.wjflzt,fhxx.dkzt from sjyz_dtzdjb tzdj,sjyz_tzfhxx fhxx where tzdj.tzbh=fhxx.tzbh</condition>
</table>
<!-- 合同信息 -->
<table id="xdht" name="type为custom时无意义,不写都可以" type="custom">
<condition>select tzdj.*,fhxx.lv,fhxx.wjflzt,fhxx.dkzt from sjyz_dtzdjb tzdj,sjyz_tzfhxx fhxx where tzdj.tzbh=fhxx.tzbh</condition>
</table>
</tables>
</sourcedb>
<targetdb>
<driver>com.sybase.jdbc2.jdbc.SybDriver</driver>
<url>jdbc:sybase:Tds:10.10.10.74:5000/loan?charset=cp936</url>
<user>l</user>
<password>l</password>
<tables>JK_DKZH</tables>
</targetdb>
</definition>
<tran-units>
<!-- unit的type用来定义单元的对应属性,是一个class全路经或者 一个默认的实现 -->
<!-- 用来描述多对多的对应 -->
<unit type="org.cafecup.common.datatran.prop.MoreToMoreComparisonImpl">
<!-- source用来描述源字段 -->
<source>
<!-- 台帐编号 对应 借据号 -->
<column tableid="tzdjb">tzbh</column>
<!-- 借款人编号 对应 客户编号 -->
<column tableid="tzdjb">jkrbh</column>
<!-- 合同编号 对应 合同号 -->
<column tableid="tzdjb">htbh</column>
<!-- 结算账户 对应 贷款帐号 -->
<column tableid="tzdjb">jszh</column>
<!-- 贷款金额 对应 初贷金额 -->
<column tableid="tzdjb">dkje</column>
<!-- 贷款余额 对应 放款余额 -->
<column tableid="tzdjb">dkye</column>
<!-- 机构码 对应 机构代码 -->
<column tableid="tzdjb">jgm</column>
<!-- 利率的对应 -->
<column tableid="tzdjb">lv</column>
<column tableid="tzdjb">lv</column>
</source>
<!-- target用来描述目的数据 -->
<target>
<column tableid="JK_DKZH">jjh</column>
<column tableid="JK_DKZH">khbh</column>
<column tableid="JK_DKZH">hth</column>
<column tableid="JK_DKZH">dkzh</column>
<column tableid="JK_DKZH">cdje</column>
<column tableid="JK_DKZH">fkye</column>
<column tableid="JK_DKZH">jgdm</column>
<column tableid="JK_DKZH">zxlv</column>
<column tableid="JK_DKZH">dqzxlv</column>
</target>
</unit>
<!-- 通过对应关系进行对应 -->
<!-- 业务状态的对应 -->
<unit type="org.cafecup.common.datatran.prop.CompareComparisonImpl">
<params>
<param>dkxtToywzt</param>
</params>
<!-- source用来描述源字段 -->
<source>
<column tableid="tzdjb">dkxt</column>
</source>
<!-- target用来描述目的数据 -->
<target>
<column tableid="JK_DKZH">ywzt</column>
</target>
</unit>
<!-- 业务品种的对应 -->
<unit type="org.cafecup.common.datatran.prop.CompareComparisonImpl">
<params>
<param>dktxToywpz</param>
</params>
<!-- source用来描述源字段 -->
<source>
<column tableid="tzdjb">dktx</column>
</source>
<!-- target用来描述目的数据 -->
<target>
<column tableid="JK_DKZH">ywpz</column>
</target>
</unit>
<!-- 五级分类状态的对应 -->
<unit type="org.cafecup.common.datatran.prop.CompareComparisonImpl">
<params>
<param>wjflztTowjflzt</param>
</params>
<!-- source用来描述源字段 -->
<source>
<column tableid="tzdjb">wjflzt</column>
</source>
<!-- target用来描述目的数据 -->
<target>
<column tableid="JK_DKZH">wjflzt</column>
</target>
</unit>
<!-- 以下是自定义对应关系的实现 -->
<unit type="dg.dkzh.comparisonimpl.DateFormatComparisonImpl">
<source>
<!-- 起贷日期的对应 -->
<column tableid="tzdjb">qdrq</column>
<!-- 止贷日期的对应 -->
<column tableid="tzdjb">zdrq</column>
<!-- 修改日期的对应 -->
<column tableid="tzdjb">xgrq</column>
</source>
<target>
<column tableid="JK_DKZH">dkffr</column>
<column tableid="JK_DKZH">dkdqr</column>
<column tableid="JK_DKZH">bgrq</column>
</target>
</unit>
<!-- 账户状态的处理 -->
<unit type="dg.dkzh.comparisonimpl.ZhztComparisonImpl">
<source>
<!-- 起贷日期的对应 -->
<column tableid="tzdjb">bz</column>
<!-- 止贷日期的对应 -->
<column tableid="tzdjb">dkzt</column>
</source>
<target>
<column tableid="JK_DKZH">zhzt</column>
</target>
</unit>
<!-- 默认的赋值实现 -->
<unit type="org.cafecup.common.datatran.prop.DefaultValueComparisonImpl">
<source>
<!-- column用来描述列名 -->
<column tableid="tzdjb">tzbh</column>
</source>
<target>
<column tableid="JK_DKZH" defaultValue="01">hbzl</column>
</target>
</unit>
</tran-units>
</database-trans>compare.xml文件主要用来描述一种值的对应,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<compare-datas>
<data-unit type="dkxtToywzt">
<description>贷款形态到业务状态的对应</description>
<codes>
<code source="1" target="0101">新贷</code>
<code source="2" target="0101">增贷</code>
<code source="3" target="0106">转贷</code>
<code source="4" target="0107">续贷</code>
<code source="5" target="0104">展期</code>
</codes>
</data-unit>
</compare-datas>
3.3 关系的说明
框架实现了几种对应关系:
一对一对应关系,一对多对应关系,多对多对应关系,通过配置文件的对应关系,进行默认值赋值的对应关系。也可以通过implements Comparison接口来实现自定义的对应关系,如下:
一个自定义对应关系的实现
package impl.comparisonimpl;import org.apache.log4j.Logger;
import org.cafecup.common.datatran.entity.Column;
import org.cafecup.common.datatran.prop.Comparison;/**
* 格式化日期格式为yyyy-mm-dd
*
* @author Administrator
*
*/public class DateFormatComparisonImpl implements Comparison { private Logger logger = Logger.getLogger(DateFormatComparisonImpl.class); public void execute(String[] params, Column[] sources, Column[] targets) {
int size = sources.length;
if (size > targets.length) {
size = targets.length;
}
if (logger.isDebugEnabled()) {
logger.debug("日期类型多对多的关系处理:\r\n");
logger.debug("SOURCES size=[" + sources.length + "]\r\n");
logger.debug("TARGETS size=[" + targets.length + "]\r\n");
} for (int i = 0; i < size; i++) {
if (logger.isDebugEnabled()) {
logger.debug("开始日期类型的处理SOURCE:[" + sources[i].getTableId()
+ "." + sources[i].getColumnName() + "]TARGET:["
+ targets[i].getTableId() + "."
+ targets[i].getColumnName() + "]");
}
for (int j = 0; j < sources[i].getDatalist().size(); j++) {
targets[i].getDatalist()
.add(getDateStr((String) sources[i].getDatalist().get(j)));
}
}
} /**
* 获取合规的日期字符串,格式为"YYYY-MM-DD"
* 传过来的格式可能为yyyymmdd、yyyy/m/d、yyyy/m/dd、yyyy/mm/d或者yyyy/mm/dd
*
* @param datestr
* @return String
*/
private String getDateStr(String datestr) {
if (datestr == null || datestr.length() < 8 || datestr.length() > 10) {
logger.warn("获取的日期参数[" + datestr + "]不合法!");
return null;
}
String[] dateunits = datestr.split("[/]");
if (dateunits.length == 1) {// 格式为yyyymmdd
return datestr.substring(0, 4) + "-" + datestr.substring(4, 6)
+ "-" + datestr.substring(6);
} else {
StringBuffer strb = new StringBuffer("0000-00-00");
strb.replace(0, 4, dateunits[0]);
strb.replace(7 - dateunits[1].length(), 7, dateunits[1]);
strb.replace(10 - dateunits[2].length(), 10, dateunits[2]);
return strb.toString();
}
}}暂时就介绍这些把,由于时间仓促,虽然思路还可以,但是具体实现还存在一些差距,仅仅是一个能用的状态,以后会逐步完善的。
由于时间仓促,并且自己表达能力有限,描述的可能不是很清楚,见谅~