Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowiring in mybatis-spring 1.2.0

Tags:

spring

mybatis

Javadoc to SqlSessionDaoSupport class says:

...@Autowired was removed from setSqlSessionTemplate and setSqlSessionFactory in version 1.2.0.

Why? In this case I have to manualy set sqlSessionFactory to my daos and also to test classes(I use dbUnit for persistance tests)

like image 591
maks Avatar asked Dec 03 '25 05:12

maks


1 Answers

@Autowired was removed according to this issue : https://code.google.com/p/mybatis/issues/detail?id=763

As a generic purpose framework, MyBatis should not make strong assertions such as "There's only one sqlSessionFactory in the ApplicationContext", that's why @Autowired was removed.

As a workaround, you could write your own MyAppSqlSessionDaoSupport that extends SqlSessionDaoSupport, and override setSqlSessionFactory(...) with

@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  if (!this.externalSqlSession) {
    this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
  }
}

Then, make your DAOs extends MyAppSqlSessionDaoSupport instead of SqlSessionDaoSupport.

like image 131
Guillaume Darmont Avatar answered Dec 05 '25 22:12

Guillaume Darmont