升級mybatis後改用mybatis-spring-1.2.0
, 再其它配置均未修改的狀況下運行出錯,錯誤信息爲:
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are requiredspring
從SqlSessionDaoSupport 這個類的源碼中能夠看出,緣由是mybatis-spring-1.2.0
中取消了自動注入SqlSessionFactory 和 SqlSessionTemplate sql
/** * Convenient super class for MyBatis SqlSession data access objects. * It gives you access to the template which can then be used to execute SQL methods. * <p> * This class needs a SqlSessionTemplate or a SqlSessionFactory. * If both are set the SqlSessionFactory will be ignored. * <p> * {code Autowired} was removed from setSqlSessionTemplate and setSqlSessionFactory * in version 1.2.0. * * @see #setSqlSessionFactory * @see #setSqlSessionTemplate * @see SqlSessionTemplate * @version $Id$ */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; } …… }
1.1.1中代碼片斷爲:mybatis
1 public abstract class SqlSessionDaoSupport extends DaoSupport { 2 3 private SqlSession sqlSession; 4 5 private boolean externalSqlSession; 6 7 @Autowired(required = false) 8 public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { 9 if (!this.externalSqlSession) { 10 this.sqlSession = new SqlSessionTemplate(sqlSessionFactory); 11 } 12 } 13 14 @Autowired(required = false) 15 public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { 16 this.sqlSession = sqlSessionTemplate; 17 this.externalSqlSession = true; 18 } 19 …… 20 21 }
多是爲了解決多數據源的問題吧,取消了自動注入。沒用到多數據源,不太關心這個。ui
解決方案:由於咱們dao層是繼承於一個dao基類,因此只要在這個基類中注入任意一個屬性便可。SqlSessionFactory在spring配置文件中已經配置。this
1 public class CommonDao extends SqlSessionDaoSupport { 2 @Resource 3 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){ 4 super.setSqlSessionFactory(sqlSessionFactory); 5 }