Today, when integrating springmvc and mybatis, I encountered such a problem when starting the server,
by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required at org.springframework.util.Assert.notNull(Assert.java:112)
Exception means missing sqlSessionFactory or sqlSessionTemplate
My dao layer is to use inheritance SqlSessionDaoSupport and then directly through this.getSqlSession () to perform database operations.
Later, by searching and viewing the source code, it was found that mybatis-spring 1.2 version did not automatically inject sqlSessionFactory
The workaround is to inject manually
1 public class BaseDaoImpl extends SqlSessionDaoSupport { 2 3 @Autowired 4 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){ 5 super.setSqlSessionFactory(sqlSessionFactory); 6 } 7 }
Then you can use the dao implementation class to inherit 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 }