Dao层继承了SqlSessionDaoSupport,该类会自动注入SQLSessionFectory,我们只要使用this.getSqlSession()就可以拿到SqlSession。
但是如果使用的是"mybatis-spring-1.0.0-RC3.jar"这个版本是没有问题的。但是高版本就会有问题,原因是Mybatis3依赖的jar包"mybatis-spring-1.2.0.jar"这个版本以及以上的版本中,对SqlSessionDaoSupport类中的'sqlSessionFactory'或'sqlSessionTemplate'注入方式进行了调整。
可能是为了解决多数据源的问题吧,取消了自动注入。
解决办法:创建一个Dao的基类CommonDao,让这个基类继承SqlSessionDaoSupport,并通过set方法注入SqlSessionFactory属性即可:
/** * Created by SYJ on 2017/4/16. */ public class CommonDao extends SqlSessionDaoSupport { @Resource public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){ super.setSqlSessionFactory(sqlSessionFactory); } }
然后让Dao实现类再继承这个CommonDao基类即可:
@Repository public class PersonDaoImpl extends CommonDao implements PersonDao { //此处省略... }
重新启动web项目,问题解决。