ibatis配置支持通配符

最近做项目,要求<sqlMap resource="">支持通配符,翻了翻ibatis的源码,修改了原来的类com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser 的addSqlMapNodelets()方法,
可以像这样
<sqlMap resource="classpath*:com/liwj/example/ibatis/sql/*.xml" />
来配置
原方法源码如下
parser.addNodelet("/sqlMapConfig/sqlMap", new Nodelet() {
      public void process(Node node) throws Exception {
        vars.errorCtx.setActivity("loading the SQL Map resource");

        Properties attributes = NodeletUtils.parseAttributes(node, vars.properties);

        String resource = attributes.getProperty("resource");
        String url = attributes.getProperty("url");

        if (usingStreams) {
          InputStream inputStream = null;
          if (resource != null) {
            vars.errorCtx.setResource(resource);
            inputStream = Resources.getResourceAsStream(resource);
          } else if (url != null) {
            vars.errorCtx.setResource(url);
            inputStream = Resources.getUrlAsStream(url);
          } else {
            throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
          }

          if (vars.sqlMapConv != null) {
            inputStream = vars.sqlMapConv.convertXml(inputStream);
          }
          new SqlMapParser(vars).parse(inputStream);
        } else {
          Reader reader = null;
          if (resource != null) {
            vars.errorCtx.setResource(resource);
            reader = Resources.getResourceAsReader(resource);
          } else if (url != null) {
            vars.errorCtx.setResource(url);
            reader = Resources.getUrlAsReader(url);
          } else {
            throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
          }

          if (vars.sqlMapConv != null) {
            reader = vars.sqlMapConv.convertXml(reader);
          }
          new SqlMapParser(vars).parse(reader);
        }
      }
    });

针对此方法,修改后的代为如下
  private class ResourceUtil extends PathMatchingResourcePatternResolver{
	  protected String determineRootDir(String arg0) {
			// TODO Auto-generated method stub
			String rootDir= super.determineRootDir(arg0);
			if(rootDir.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX));
				rootDir=rootDir.substring(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX.length());
				
			return rootDir;
	  }
  }
  protected String[] getAllResource(String resource){
	  String[] file_resource=null;
	  ResourceLoader resourceLoader=new PathMatchingResourcePatternResolver();
	  try{
		  Resource[]  a_resource=((ResourcePatternResolver) resourceLoader).getResources(resource);
		  ResourceUtil resourceUtil=new ResourceUtil();
		  String rootDir=resourceUtil.determineRootDir(resource);
		  if(a_resource!=null){
			  file_resource=new String[a_resource.length];
			  for(int i=0;i<a_resource.length;i++){
				  file_resource[i]=rootDir+"/"+a_resource[i].getFilename();
				  System.out.println("fileName="+file_resource[i]);
			  }
		  }
	  }catch(IOException e){
		 throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
	  }
	  return file_resource;
  }
  protected void addSqlMapNodelets() {
    parser.addNodelet("/sqlMapConfig/sqlMap", new Nodelet() {
      public void process(Node node) throws Exception {
        vars.errorCtx.setActivity("loading the SQL Map resource");

        Properties attributes = NodeletUtils.parseAttributes(node, vars.properties);

        String resource = attributes.getProperty("resource");
        String url = attributes.getProperty("url");
        if(resource.indexOf("*")>0){
        	String[] files=getAllResource(resource);
        	for(int i=0;i<files.length;i++){
        		addSqlMapConfigFiles(files[i], url);
        	}
        }else{
        	addSqlMapConfigFiles(resource, url);
        }
      }

代码中使用了spring的PathMatchingResourcePatternResolver类来分析带有通配符的resource的值。由于PathMatchingResourcePatternResolver.determineRootDir方法是protected,故写了内部类继承PathMatchingResourcePatternResolver。

由于水平有限,只能改源代码如上,大家有什么改进或者优化之类的,可以继续修改,大家共同学习,共同进步

你可能感兴趣的:(sql,xml,ibatis)