OSGI-mybatis整合文档

mrsuddenflash 2013-06-06

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

关键技术说明

OSGIMybatiseclipse(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

publicSqlSessionFactorygetSqlSessionFactory(Bundlebundle)

{

//TODOAuto-generatedmethodstub

ClassLoaderloader=bundle.adapt(BundleWiring.class).getClassLoader();

StringmybatisConfig=bundle.getHeaders().get(MybatisConstants.MybatisConfig);

System.out.println("【mybatisConfig】"+mybatisConfig);

Readerreader=null;

try

{

Resources.setDefaultClassLoader(loader);

reader=Resources.getResourceAsReader(mybatisConfig);

XMLConfigBuilderparser=newXMLConfigBuilder(reader);

configuration=parser.parse();

TransactionFactorytransactionFactory=newJdbcTransactionFactory();

Environmentenvironment=newEnvironment("development",transactionFactory,dataSource);

configuration.setEnvironment(environment);

sessionFactory=newDefaultSqlSessionFactory(configuration);

}

catch(IOExceptione)

{

e.printStackTrace();

}

returnsessionFactory;

}

基于以上的OSGI-Mybatis,应用示例

在每一个service实现对象中,加载一下Mybatismapper类

privateMapperRegistrymapperRegistry=null;

publicvoidbind(CommonServicecommonService)

{

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对象

publicList<CategoryInfo>getAllCategoryInfo()

{

//TODOAuto-generatedmethodstub

List<CategoryInfo>retlist=newArrayList<CategoryInfo>();

SqlSessionsqlSession=null;

try

{

sqlSession=sessionFactory.openSession();

retlist=sqlSession.getMapper(CategoryMapper.class).findAll();

}

catch(Exceptione)

{

e.printStackTrace();

}

finally

{

if(sqlSession!=null)

{

sqlSession.close();

}

}

returnretlist;

}

OSGIhessian整合

osgihessian为第三方系统

相关推荐