`
cnpoint
  • 浏览: 1480 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.orm orm 对象
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 为第三方系统

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics