Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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

再其他配置均为改动的情况下执行出错: 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 BaseDaoImpl extends SqlSessionDaoSupport {
2     @Resource
3     public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
4         super.setSqlSessionFactory(sqlSessionFactory);
5     }

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116828.html原文链接:https://javaforall.net

(1)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HeartBleed漏洞详解与利用「建议收藏」

    HeartBleed漏洞详解与利用「建议收藏」看一下流传的Python利用脚本。#!/usr/bin/python#QuickanddirtydemonstrationofCVE-2014-0160byJaredStafford(jspenguin@jspenguin.org)#Theauthordisclaimscopyrighttothissourcecode.

    2022年7月25日
    12
  • tf2013_access下载免费中文版

    tf2013_access下载免费中文版
    TFS2010中文版下载 
     
    MicrosoftVisualStudioTeamFoundationServer2010中文版90天试用,可直接加序列号成正式版快速描述MicrosoftVisualStudioTeamFoundationServer2010是一个协作平台,它是Microsoft应用程序生命周期管理解决方案的核心,可帮助团队在整个IT生命周期中降低风险、简化交互过程并杜绝浪费。
    http://www.micros

    2022年9月24日
    3
  • 信息学奥赛GoC编程测试题题库

    信息学奥赛GoC编程测试题题库在线答题和答案详解请参考https://blog.csdn.net/zhengzyx2040/article/details/118632561一、单选题(15题,每题4分,满分60分)1、GoC的编译+运行的快捷键是()A、F5B、F11C、F8D、F122、GoC程序的源文件扩展名是(),经过编译后生成的可执行文件扩展名是()A、CPPEXEB、EXECPP…

    2022年6月18日
    62
  • Verilog 流水线设计[通俗易懂]

    Verilog 流水线设计[通俗易懂]一、什么是流水线流水线设计就是将组合逻辑系统地分割,并在各个部分(分级)之间插入寄存器,并暂存中间数据的方法。目的是将一个大操作分解成若干的小操作,每一步小操作的时间较小,所以能提高频率,各小操作能并行执行,所以能提高数据吞吐率(提高处理速度)。二、什么时候用流水线设计使用流水线一般是时序比较紧张,对电路工作频率较高的时候。典型情况如下:1)功能模块之间的流水线,用乒乓buffer来交互数据。代价是增加了memory的数量,但是和获得的巨大性能提升相比,可以忽略不计。2)I/O瓶

    2022年8月14日
    4
  • 胶南人才交流中心[通俗易懂]

    胶南人才交流中心[通俗易懂]电话:0532-86164282山东胶南珠海中路273号邮编:266400

    2022年7月3日
    32
  • opencv跟踪视频上的目标(理论分析框架)

    出处:http://hi.baidu.com/icekeydnet/blog/item/965b25154a19f3dea6ef3ffe.html如前面说到的,OpenCVVS提供了6组算法的接口,分别是:前景检测、新目标检测、目标跟踪、轨迹生成、跟踪后处理、轨迹分析,除了轨迹生成用于轨迹数据的保存以外,其他5个部分都是标准的视频监控算法体系中不可或缺的部分。      OpenC

    2022年4月16日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号