Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

开发环境:sping+springmvc-4.2.x+mybatis-3.1

开发软件:ecplise Mars Release (4.5.0)

 

异常类型:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

产生所在:在构建baseDao的时候,继承了SqlSessionDaoSupport。在baseDao的方法中调用sqlSessionFactory来执行sql语句。

异常原因:

原因是mybatis-spring-1.2.0之后的版本中取消了自动注入SqlSessionFactory 和 SqlSessionTemplate

下面是SqlSessionDaoSupport的源码

package org.mybatis.spring.support;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.support.DaoSupport;
import org.springframework.util.Assert;


public abstract class SqlSessionDaoSupport extends DaoSupport
{
  private SqlSession sqlSession;
  private boolean externalSqlSession;


  public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
  {
    if (!this.externalSqlSession)
      this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
  }


  public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)
  {
    this.sqlSession = sqlSessionTemplate;
    this.externalSqlSession = true;
  }


  public SqlSession getSqlSession()
  {
    return this.sqlSession;
  }


  protected void checkDaoConfig()
  {
    Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
  }
}

 

 

解决方案:在继承了SqlSessionDaoSupport的基类中,添加一下的代码即可:


@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){  
        super.setSqlSessionFactory(sqlSessionFactory);  
    }

手动的子类中注入SqlSessionFactory(或SqlSessionTemplate)。

你可能感兴趣的:(Exception异常处理)