`
lwj0212
  • 浏览: 48905 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ibatis支持多配置文件实现

阅读更多
思路如下:
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
分享到:
评论
1 楼 ddandyy 2008-03-27  
您说的是这个么.........

<sqlMapConfig>
    <settings enhancementEnabled="true" maxTransactions="50" maxRequests="100" maxSessions="50" useStatementNamespaces="true"/>
    <!-- Identify all SQL Map XML files to be loaded by this SQL map. Relative to classpath -->
	<sqlMap resource="com/cn/dao/ibatis/testASQL.xml" />
	<sqlMap resource="com/cn/dao/ibatis/testBSQL.xml" />
</sqlMapConfig>

相关推荐

    iBATIS实战

    13.2 管理iBATIS配置文件 237 13.2.1 将其保存在类路径上 237 13.2.2 集中放置文件 238 13.2.3 主要按返回类型来组织映射文件 239 13.3 命名规范 239 13.3.1 语句的命名 239 13.3.2 参数映射的命名 239 13.3.3 结果...

    xorm:xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常正确。本库是基于原版xorm的定制增强版本,为xorm提供类似ibatis的配置文件及动态SQL支持,支持AcitveRecord操作

    Having,Table,Sql,Cols等函数和结构体等方式作为条件支持级联加载结构支持类ibatis方式配置SQL语句(支持XML配置文件,json配置文件,xsql配置文件,支持 , , 模板和自定义实现配置多种方式)支持动态SQL功能...

    iBATIS SQL Maps 开发指南

    SQL Map使用简单的XML配置文件将Java Bean映射成SQL语句,对比其他的数据库持续层和ORM框架(如JDO的实现,Hibernate等),SQL Map最大的优点在于它简单易学。要使用SQL Map,只要熟悉Java Bean,XML和SQL,就能使您...

    基于iBatis SQL Map的数据持久层实现应用研究 (2008年)

    介绍了iBatis SQL Map建立ORM机制的原理和特点,结合某物流系统的开发,给出了SQL Map建立ORM的主要内容,包括映射关系、SQL Map配置文件和SQL Map API等.实践表明,iBatis SQL Map的ORM实现技术非常方便、灵活,并较好地...

    Struts2.0+Springframework2.5+ibatis2.3完美整合用户登录及增删改查

    最流行技术Struts2.1 +Spring 2.5.1+ibatis2.3整合开发而成,这与我以前发布的版本中最大特色是整合了Spring2.5.1中的注解功能和半自动化工具ibatis技术,这是本示例的两大特色,简化了配置文件的烦锁,希望能给更多喜欢...

    ibatis 开发指南(pdf)

    ibatis 实例配置 一个典型的配置文件如下(具体配置项目的含义见后): &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS....

    JAVA WEB典型模块与项目实战大全

    6.2 网络硬盘功能具体实现——浏览磁盘和显示文件信息  6.3 网络硬盘功能具体实现——操作文件夹和文件  6.4 小结  第7章 网站统计模块(jsp+servlet)  7.1 网站统计模块原理  7.2 实现显示欢迎信息功能,...

    Step By Step写测试(书签版).pdf

    5.15 Martini项目下的ibatis文件配置 5.16 数据库测试FAQ 6 Spring和SQL跟踪 6.1 @Tracer 6.2 FAQ 7 JTester插件的使用 7.1 插件功能 7.2 插件安装 7.3 录制变量的功能 7.4 dbFit插件编辑功能 7.5 从数据库中直接拖...

    spring4.3.2参考文档(英文)

    Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。 Spring AOP:通过配置管理特性,Spring AOP 模块...

    springmybatis

    1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...

    spring4.3.9相关jar包

    spring-beans.jar(必须):这 个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form标签 ...

    spring chm文档

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form...

    Spring中文帮助文档

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置 13.9.2. form标签 ...

    Spring API

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置 13.9.2. form标签 ...

    Spring面试题

    类与类之间的关系主要体现在表与表之间的关系进行操作,它们都市对对象进行操作,我们程序中把所有的表与类都映射在一起,它们通过配置文件中的many-to-one、one-to-many、many-to-many、 4. 说下Hibernate的缓存...

    Spring基础与快速入门

    你可以写一个配置文件(通常是xml文件),在上面定义对象的名字,是否是单例,以及设置与其他对象的依赖关系。那么在容器启动之后,这些对象就被实例化好了,你直接去用就好了,而且依赖关系也建立好了。 3 IOC:...

    Spring 2.0 开发参考手册

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form...

    AppFramework_V1.0

    除了实现数据的增删改查,数据访问层还要提供一些与业务无关功能,例如面向对象的持久化与访问机制、本地事务与分布式事务支持、多数据库支持,这些机制或功能形成相对独立的逻辑领域,其主要目的有: &lt;br&gt;1、 ...

Global site tag (gtag.js) - Google Analytics