1、Mybatis的SQLSession获取MapperDao,有两种

1.1、独立框架使用

全局配置文件(SqlMapConfig.xml)+SqlSession获取方式+MapperDao

SqlSession获取语句

String config="SqlMapConfig.xml";

Reader reader=Resources.getResourceAsReader(config);

SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();

SqlSessionFactory factory=sfb.build(reader);

SqlSession session=factory.openSession();

//获取映射Dao

//::'resource'表示mappers元素下mapper元素下属性resource-->

SqlMapConfig.xml-->::'resource'-->CostMapperDao;

//取得session之后:

EmpMapDao empDao=session.getMapper(EmpMapDao.class);

SqlMapConfig.xml

/p>

"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8" />

1.2、与springmvc框架集成获取

spring配置文件+MapperDao(不需要Mybatis的全局配置文件了)

applicationContext.xml

获取MapperDao的实例

bookDao=ac.getBean("noteBookMapperDao", NoteBookMapperDao.class);

@Resource

private NoteBookMapperDao noteBookMapperDao;

method{

//noteBookMapperDao....

}

2、Hibernate框架获取Session

2.1、框架单独使用

String config="hibernate.cfg.xml";

Configuration conf=new Configuration();

conf.configure();

//获取sessionFactory

sf=conf.buildSessionFactory();

//获取session

Session session=sf.openSession();

3、spring获取session

3、1在web.xml配置(我也不清楚是否必须)

org.springframework.web.context.request.RequestContextListener

3、2在需要的地方添加

@Resource

private HttpSession session;