今天在整合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 @Autowired 4 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 @Override 11 public Person selectPersonById(Integer personId) { 12 return this.getSqlSession().selectOne(ns+"selectPersonById", personId); 13 } 14 15 @Override 16 public void update(Person p) { 17 this.getSqlSession().update(ns + "update", p); 18 } 19 20 @Override 21 public void delete(Integer personId) { 22 this.getSqlSession().delete(ns + "delete", personId); 23 } 24 25 @Override 26 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 异常
原文地址:https://www.cnblogs.com/cat-fish6/p/8961830.html
时间: 2024-10-10 20:12:58