白森 发表于 2016-11-24 08:29:21

OSGI-mybatis整合文档

osgi-mybatis整合,整合过程中,未采用spring-osgi机制,ds方式。

关键技术说明

OSGI   Mybatis    eclipse(spring-tool)    felix
Bundle 功能描述

bundle名称功能
com.gooagoo.si.mybatis.lib常用jar集合
com.gooagoo.si.mybatis.ormorm 对象
com.gooagoo.si.mybatis.datasource数据源,以及OSGI-Mybatis整合,命名不够规范 ^_^
com.gooagoo.si.mybatis.article.category应用示例

公共Bundle重点说明
com.gooagoo.si.mybatis.datasource说明,获得Mybatis中的SqlSessionFactory,并注入dataSource

public SqlSessionFactory getSqlSessionFactory(Bundle bundle)
    {
      // TODO Auto-generated method stub
      ClassLoader loader = bundle.adapt(BundleWiring.class).getClassLoader();
      String mybatisConfig = bundle.getHeaders().get(MybatisConstants.MybatisConfig);
      System.out.println("【mybatisConfig】" + mybatisConfig);
      Reader reader = null;
      try
      {

            Resources.setDefaultClassLoader(loader);
            reader = Resources.getResourceAsReader(mybatisConfig);
            XMLConfigBuilder parser = new XMLConfigBuilder(reader);
            configuration = parser.parse();
            TransactionFactory transactionFactory = new JdbcTransactionFactory();
            Environment environment = new Environment("development", transactionFactory, dataSource);
            configuration.setEnvironment(environment);
            sessionFactory = new DefaultSqlSessionFactory(configuration);
      }
      catch (IOException e)
      {
            e.printStackTrace();
      }
      return sessionFactory;
    }

基于以上的OSGI-Mybatis,应用示例
在每一个service实现对象中,加载一下Mybatis mapper类

private MapperRegistry mapperRegistry = null;

    public void bind(CommonService commonService)
    {
      sessionFactory = commonService.getSqlSessionFactory();
      mapperRegistry = sessionFactory.getConfiguration().getMapperRegistry();
      mapperRegistry.addMapper(CategoryMapper.class);
      logger.info("v1 " + sessionFactory.getConfiguration().getMapperRegistry().hasMapper((CategoryMapper.class)));
      logger.info(getAllCategoryInfo() + "");
    }

使用Mapper.class对象

public List<CategoryInfo> getAllCategoryInfo()
    {
      // TODO Auto-generated method stub
      List<CategoryInfo> retlist = new ArrayList<CategoryInfo>();
      SqlSession sqlSession = null;
      try
      {
            sqlSession = sessionFactory.openSession();
            retlist = sqlSession.getMapper(CategoryMapper.class).findAll();
      }
      catch (Exception e)
      {
            e.printStackTrace();
      }
      finally
      {
            if (sqlSession != null)
            {
                sqlSession.close();
            }
      }
      return retlist;
    }

OSGI hessian整合
osgi hessian 为第三方系统
页: [1]
查看完整版本: OSGI-mybatis整合文档