Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

from http://www.tuicool.com/articles/jYJZFn


之前一直使用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中代码片段为:

 1 public abstract class SqlSessionDaoSupport extends DaoSupport {
 2 
 3     private SqlSession sqlSession;
 4 
 5     private boolean externalSqlSession;
 6 
 7     @Autowired(required = false)
 8     public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
 9         if (!this.externalSqlSession) {
10             this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
11         }
12     }
13 
14     @Autowired(required = false)
15     public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
16         this.sqlSession = sqlSessionTemplate;
17         this.externalSqlSession = true;
18     }
19     ……
20 
21 }

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

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

1 public class CommonDao extends SqlSessionDaoSupport {
2     @Resource
3     public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
4         super.setSqlSessionFactory(sqlSessionFactory);
5     }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值