springmvc與mybatis整合時 java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 異常

來源:互聯網
上載者:User

標籤:tsql   framework   tis   今天   autowire   實作類別   sql   mapper   epo   

今天在整合springmvc與mybatis時,啟動伺服器遇到這樣一個問題,

 by: java.lang.IllegalArgumentException: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required at org.springframework.util.Assert.notNull(Assert.java:112)

異常的意思是  缺少sqlSessionFactory 或者是  sqlSessionTemplate

我的dao層是利用繼承SqlSessionDaoSupport  然後可以直接通過this.getSqlSession() 來進行資料庫的操作。

後來通過搜尋以及查看原始碼發現,mybatis-spring 1.2版本沒有自動注入sqlSessionFactory

解決方案是手動注入

1 public class BaseDaoImpl extends SqlSessionDaoSupport {2     3     @Autowired4     public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){  5             super.setSqlSessionFactory(sqlSessionFactory);  6     }7 }

然後在將dao實作類別繼承BaseDaoImpl就可以使用了

 1 @Repository 2 public class PersonDaoImpl extends BaseDaoImpl implements PersonDao { 3  4     String ns = "cn.tx.mapper.PersonMapper."; 5     @Override 6     public void savePerson(Person p) { 7         this.getSqlSession().insert(ns+"savePerson", p); 8     } 9 10     @Override11     public Person selectPersonById(Integer personId) {12         return this.getSqlSession().selectOne(ns+"selectPersonById", personId);13     }14 15     @Override16     public void update(Person p) {17         this.getSqlSession().update(ns + "update", p);18     }19 20     @Override21     public void delete(Integer personId) {22         this.getSqlSession().delete(ns + "delete", personId);23     }24 25     @Override26     public List<Person> selectPersonByCondition(QueryCondition qc) {27         return this.getSqlSession().selectList(ns + "selectPersonByCondition", qc);28     }29 30 }

 

springmvc與mybatis整合時 java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.