码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis-sparing- 1.1.1 和1.1.2版本的区别

时间:2015-02-09 12:26:58      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

之前一直使用mybatis+mybatis-spring-1.1.1,系统升级mybatis后使用 mybatis-spring-1.2.0,

再其它配置均为修改的情况下运行出错: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required

从SqlSessionDaoSupport 这个类的源码中可以看出,原因是 mybatis-spring-1.2.0 中取消了自动注入 SqlSessionFactory 和 SqlSessionTemplate

/**
* 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中代码片段为:

public abstract class SqlSessionDaoSupport extends DaoSupport {

private SqlSession sqlSession;

private boolean externalSqlSession;

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

@Autowired(required = false)
public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSession = sqlSessionTemplate;
this.externalSqlSession = true;
}
……

}
可能是为了解决多数据源的问题吧,取消了自动注入。没用到多数据源,不太关心这个。

解决方案:因为我们dao层是继承于一个dao基类,所以只要在这个基类中注入任意一个属性即可。 SqlSessionFactory 在spring配置文件中已经配置。

public class CommonDao extends SqlSessionDaoSupport {
@Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
super.setSqlSessionFactory(sqlSessionFactory);
}

mybatis-sparing- 1.1.1 和1.1.2版本的区别

标签:

原文地址:http://www.cnblogs.com/leonkobe/p/4280982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!