思路如下:
1、修改SqlMapClientBuilder,增加一个新的方法buildSqlMapClient(String locationPath)
2、分析此locationPath,获取到n个配置文件
3、把这些n个配置文件都以xml的方式读取,然后合并其中的各个节点,得到一个合并后的xml文件
4、以合并后的xml文件初始化SqlMapClient
可以如下初始化
SqlMapClientBuilder.buildSqlMapClient("sqlmap-iw-config.xml,sqlmap-dc.config.xml");
SqlMapClientBuilder.buildSqlMapClient("sqlmap-*-config.xml");
或者使用spring配置
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/liwj/example/ibatis/SqlMapConfigExample.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${driver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:conf/ibatis/sqlmap-*-config.xml</value>
<!--
<property name="configLocation">
<value>classpath:conf/ibatis/sqlmap-iw-config.xml,conf/ibatis/sqlmap-dc-config.xml</value>
-->
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
想法是这样子的,还有没有具体去写,有时间了补充完整……
现在终于补充完毕了,修改com.ibatis.sqlmap.client.SqlMapClientBuilder
增加全局变量
private static final String PROPERTIES="properties";
private static final String SETTINGS="settings";
private static final String RESULTOBJECTFACTORY="resultObjectFactory";
private static final String TYPEALIAS="typeAlias";
private static final String TYPEALIAS_ALIAS="alias";
private static final String TYPEHANDLER="typeHandler";
private static final String TRANSACTIONMANAGER="transactionManager";
private static final String SQLMAP="sqlMap";
private static final String SQLMAP_RESOURCE="resource";
private static final String SQLMAPCONFIG_ROOT="sqlMapConfig";
增加方法如下:
public static SqlMapClient buildSqlMapClient(String locationPath){
ResourceLoader resourceLoader=new PathMatchingResourcePatternResolver();
Document d_doc=null;
try{
Resource[] a_resource=((ResourcePatternResolver) resourceLoader).getResources(locationPath);
SAXReader reader=new SAXReader();
for(int i=0;i<a_resource.length;i++){
Reader fileReader=new FileReader(a_resource[i].getFile());
if(i==0) d_doc=reader.read(fileReader);
else{
Document r_doc=reader.read(fileReader);
new SqlMapClientBuilder().combinationAll(r_doc, d_doc);
}
}
}catch(Exception e){
e.printStackTrace();
}
d_doc.addDocType("sqlMapConfig", "-//iBatis.com//DTD SQL Map Config 2.0//EN", "http://www.iBatis.com/dtd/sql-map-config-2.dtd");
return new SqlMapClientBuilder().buildSqlMapClient(new ByteArrayInputStream(d_doc.asXML().getBytes()));
}
public void combinationAll(Document resource,Document destination){
combinationGlobalPropNodelets(resource, destination);
combinationSettingsNodelets(resource, destination);
combinationSqlMapNodelets(resource, destination);
combinationTypeAliasNodelets(resource, destination);
combinationResultObjectFactoryNodeltes(resource, destination);
combinationTransactionManagerNodeltes(resource, destination);
combinationTypeHandlerNodeltes(resource, destination);
}
private void combinationSqlMapNodelets(Document resource,Document destination){
combination(this.SQLMAP, resource, destination);
}
private void combinationTypeAliasNodelets(Document resource,Document destination){
combination(this.TYPEALIAS, resource, destination);
}
private void combinationSettingsNodelets(Document resource,Document destination){
combination(this.SETTINGS, resource, destination);
}
private void combinationGlobalPropNodelets(Document resource,Document destination){
combination(this.PROPERTIES, resource, destination);
}
private void combinationTransactionManagerNodeltes(Document resource,Document destination){
combination(this.TRANSACTIONMANAGER, resource, destination);
}
private void combinationResultObjectFactoryNodeltes(Document resource,Document destination){
combination(this.RESULTOBJECTFACTORY, resource, destination);
}
private void combinationTypeHandlerNodeltes(Document resource,Document destination){
combination(this.TYPEHANDLER, resource, destination);
}
private void combination(String nodePath,Document resource,Document destination){
/*只允许一个节点*/
StringBuffer onePro=new StringBuffer();
onePro.append(PROPERTIES).append(",").append(SETTINGS);
StringBuffer zeroPro=new StringBuffer();
zeroPro.append(TYPEALIAS).append(",").append(",").append(SQLMAP);
String nodeXpath="/sqlMapConfig/"+nodePath;
List L_R_element=resource.selectNodes(nodeXpath);
List L_D_element=destination.selectNodes(nodeXpath);
/*源节点,判断目的节点是否有此节点,无则添加,有则比较*/
if(L_R_element!=null&&L_R_element.size()>0){
/*如目的节点为null,则把源节点全部加入到目的节点中*/
if(L_D_element==null||L_D_element.size()==0){
for(int i=0;i<L_R_element.size();i++){
Element element=(Element)L_R_element.get(i);
destination.add(element);
}
}else{
if(onePro.toString().indexOf(nodePath)>=0){
/*只有一个节点,则合并两个节点的属性*/
Element r_element=(Element)L_R_element.get(0);
Element d_element=(Element)L_D_element.get(0);
/*比较所有属性*/
List r_attribute=r_element.attributes();
if(r_attribute!=null&&!r_attribute.isEmpty()){
List d_attribute=d_element.attributes();
/*如果目的节点没有任何属性,则将原属性全部加入目的节点*/
if(d_attribute==null||d_attribute.isEmpty()){
d_element.setAttributes(r_attribute);
}else{
List list_temp=new ArrayList();
for(int i=0;i<d_attribute.size();i++){
list_temp.add(((Attribute)d_attribute.get(i)).getName());
}
/*循环源节点的所有属性,如目的节点中没有此属性,则添加*/
for(int i=0;i<r_attribute.size();i++){
Attribute temp_attr=(Attribute)r_attribute.get(i);
if(!list_temp.contains(temp_attr.getName())){
d_element.add((Attribute)temp_attr.clone());
//d_element.addAttribute(temp_attr.getName(), temp_attr.getValue());
}
}
}
}
}else if(zeroPro.toString().indexOf(nodePath)>=0){
String comp="";
if(nodePath.equals(TYPEALIAS)) comp=TYPEALIAS_ALIAS;
else if(nodePath.equals(SQLMAP)) comp=SQLMAP_RESOURCE;
List d_temp=new ArrayList();
for(int i=0;i<L_D_element.size();i++){
Element element=(Element)L_D_element.get(i);
d_temp.add(element.attribute(comp).getValue());
}
/*循环源节点,如果目的节点由此节点,则忽略,否则添加*/
Element ele_root=destination.getRootElement();
for(int i=0;i<L_R_element.size();i++){
Element element=(Element)L_R_element.get(i);
if(!d_temp.contains(element.attribute(comp).getValue()))
ele_root.add((Element)element.clone());
}
}else{
}
}
}
使用了dom4j来操作xml