专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 软件架构设计

rcp集成ibatis的兑现

发布时间:2011-06-20 02:11:29 文章来源:www.iduyao.cn 采编人员:星星草
rcp集成ibatis的实现

想通过RCP直接连数据库,网上有不少集成jdbc的教程,但是jdbc太繁琐,所以打算集成ibatis

RCP因为自身的特性,直接通过ibatis的类加载资源,报找不到文件的。下面sql-map-config.xml文件,找不到

String resource = "sql-map-config.xml";

reader = Resources.getResourceAsReader(resource);

google好久,说是rcp的87775号bug,解决办法是自己重新实现classloader加载资源。

实现如下

try {
       String pathstr = "sql-map-config.xml";
        System.out.println(pathstr);
        InputStream is = getResourceAsStream(getClassLoader(),pathstr);
        reader = new InputStreamReader(is);
        sqlMapClient =  SqlMapClientBuilder.buildSqlMapClient(reader);
        return sqlMapClient;
      } catch (Exception e) {
        e.printStackTrace();
      }finally{
        currThread.setContextClassLoader(originalContextCL);
      }

调用的函数实现

private static  ClassLoader getClassLoader() {
    ClassLoader cl = null;
       cl = Thread.currentThread().getContextClassLoader();
     return cl;
   }
 public static  InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
     InputStream in = null;
     if (loader != null)
  {
      in = loader.getResourceAsStream(resource);
  }
     if (in == null)
     {
      in = ClassLoader.getSystemResourceAsStream(resource);
     }
     if (in == null) throw new IOException("Could not find resource " + resource);
     return in;
   }

同时需要在rcp的plugin.xml里Runtime页的classpath项添加ibatis和数据库驱动jar包。

其他的照着正常ibatis在普通项目中的使用而使用就可以了

 

主要参考网址http://www.ibm.com/developerworks/cn/opensource/os-lo-ecl-classloader/index.html


 

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: