篇八:SqlSessionTemplate与SqlSessionDaoSupport

 

一、SqlSessionFactory、SqlSession基础知识

  源码解析:http://donald-draper.iteye.com/blog/2339029

  总结:DefaultSqlSessionFactory根据执行器类型,事务级别,是否提交等信息,来构建执行器Executor,然后根据执行器和configuration构建SqlSession,默认为DefaultSqlSession,从DefaultSqlSession的类信息方法来看,DefaultSqlSession的 
查询则委托给executor的query,插入,更新,删除,则委托给executor的update,提交,回滚,清除缓存,刷新Statement,关闭SqlSession,都是委托给Executor的相应方法。 

  SqlSessionFactory获取SqlSession

/**
  * SqlSessionFactory是工厂类,负责生成SqlSession;
  * sqlSessionFactory默认为DefaultSqlSessionFactory 
  */
public class DefaultSqlSessionFactory  implements SqlSessionFactory  
{  
    public DefaultSqlSessionFactory(Configuration configuration)  
    {  
        this.configuration = configuration;  
    }  
    public SqlSession openSession()  
    {  
        return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);  
    }  
      
    public SqlSession openSession(boolean autoCommit)  
    {  
        return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);  
    }  
    public SqlSession openSession(ExecutorType execType)  
    {  
        return openSessionFromDataSource(execType, null, false);  
    }  
    public SqlSession openSession(TransactionIsolationLevel level)  
    {  
        return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false);  
    }  
    public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level)  
    {  
        return openSessionFromDataSource(execType, level, false);  
    }  
    public SqlSession openSession(ExecutorType execType, boolean autoCommit)  
    {  
        return openSessionFromDataSource(execType, null, autoCommit);  
    }  
     public SqlSession openSession(Connection connection)  
    {  
        return openSessionFromConnection(configuration.getDefaultExecutorType(), connection);  
    }  
  
    public SqlSession openSession(ExecutorType execType, Connection connection)  
    {  
        return openSessionFromConnection(execType, connection);  
    }  
      
    private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)  
    {  
      //  
    }  
    private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection)  
    {  
    }  
}  

/**
  * SqlSession是interface,定义了操作(增删该查)数据库的方法
  * openSession实际上,是通过openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) 
  * 其中ExecutorType为执行器类型,TransactionIsolationLevel为事务级别,autoCommit是否自动提交;
  */
  SqlSession session = sqlSessionFactory.openSession();

 

二、SqlSessionTemplate

  源码解析:http://www.cnblogs.com/daxin/p/3544188.html

  SqlSessionTemplate 实现了SqlSession接口,使用SqlSessionTemplate 来代理以往的DefailtSqlSession完成对数据库的操作,DefailtSqlSession是线程不安全的;

  与Spring集成以后,Spring提供了一个全局唯一的SqlSessionTemplate示例 来完成DefailtSqlSession的功能。无论是多个dao使用一个SqlSessionTemplate,还是一个dao使用一个SqlSessionTemplate,SqlSessionTemplate都是对应一个sqlSession,当多个web线程调用同一个dao时,它们使用的是同一个SqlSessionTemplate,也就是同一个SqlSession。

 

  在使用Mybatis与Spring集成的时候我们用到了SqlSessionTemplate 这个类。

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
     <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

  在SpringMVC+MyBatis中,使用SqlSessionTemplate

 

三、SqlSessionDaoSupport

  SqlSessionDaoSupport是一个抽象的支持类,用来提供SqlSession。调用getSqlSession()方法你会得到一个SqlSessionTemplate

    

  具体实现

    1、mybatis-spring1.2.0及以上版本:只需要获取SqlSession对象

/**
  * BaseDaoImpl
  */
public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>
{
    //获取SqlSession执行数据库操作,不需要SqlSessionFactory定义
    public SqlSession getSqlSession() 
  {
return super.getSqlSession(); } } /** * DaoImpl 实现类 */ @Repository("UserInfoDao") public class UserInfoDaoImpl extends BaseDaoImpl<UserInfo> implements UserInfoDao { @Autowired private SqlSessionTemplate sqlSession; @Override public UserInfo getUserInfo() { UserInfo user = sqlSession.selectOne("selectById", "2012117153"); System.out.println(user.getRealName()); return user; } }

 

  2、mybatis-spring1.2.0以前版本,支持自动导入SqlSessionTempale

/**
  * BaseDaoImpl
  */
public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>
{
    @Autowired
    private SqlSessionTemplate sqlSession;
    
    //可以自动依赖导入
    public SqlSession getSqlSession() 
  {
        return this.sqlSession;
    }
         
}


/**
  * DaoImpl 实现类
  */
@Repository("UserInfoDao")
public class UserInfoDaoImpl extends BaseDaoImpl<UserInfo> implements UserInfoDao {
    
    @Autowired
    private SqlSessionTemplate sqlSession;
    

    @Override
    public UserInfo getUserInfo() {
        UserInfo user = sqlSession.selectOne("selectById", "2012117153");
        System.out.println(user.getRealName());
        return user;
    }
}

 

  

posted @ 2017-01-05 13:59  刘广平  阅读(671)  评论(0)    收藏  举报