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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c语言解析xml文件「建议收藏」

    c语言解析xml文件「建议收藏」#include”stdafx.h”#include#include”Mytext.h”#include#include#include#include#include#include#include#include#include#includeusingnamespacestd;#pragmacomment(lib,”Oleac

    2022年7月14日
    65
  • iPython的安装过程

    iPython的安装过程

    2021年9月18日
    32
  • Oracle 批量插入(insert all into)

    Oracle 批量插入(insert all into)项目需要用到导入excel表,并解析数据批量插入到oracle数据库中。1)直接解析excel,循环行,拼了sql,executeUpdate。执行一波…咦,这效率很低啊,有多少行数据就执行了多少句sql,基本是一万行已经接近一分钟了。2)每次都仅执行一条sql语句,时间是不是都花在建立连接放开连接balabala的过程上了,用executebatch批量执行sql语句试试。没…

    2022年7月25日
    12
  • html标签<td><tr><th>全称及缩写说明[通俗易懂]

    html标签<td><tr><th>全称及缩写说明[通俗易懂]<td>是tabledatacell的缩写,单元格<tr>是tablerow的缩写,表格中的一行<th>是tableheadercell的

    2022年8月2日
    4
  • 视频转gif mac_有没有录屏时生成gif的软件

    视频转gif mac_有没有录屏时生成gif的软件Mac自带QuicktimePlayer上次完成word添加好看的格式化代码后,又想着做一个gif图演示功能效果,展示出来会更好。就来摸索摸索了…简单的工具图示如下:1、Launched中搜索2、双击触摸板3、按照工具提示来随意录屏吧~AppStore下载工具PicGIFLite双击图标简单的界面,更深入我心,实测有效,就是gif图片分辨率不高。下图是我做的实测的效果(这个滑动的功能后…

    2022年9月16日
    0
  • 【深度学习入门】——亲手实现图像卷积操作[通俗易懂]

    【深度学习入门】——亲手实现图像卷积操作[通俗易懂]深度学习中有一个很重要的概念就是卷积神经网络CNN,卷积神经网络中又有卷积层、池化层的概念。尤其是卷积层,理解难度比较大,虽然书中或者是视频中都有详细介绍过它的基础概念,但对于求知欲望很强烈的我,我总心里痒痒的,总想亲手实现,看看效果,怕的就是自己会眼高手低,做技术人最可怕的就是眼高手低。所以,我打算用python来亲自验证一遍。什么是卷积?卷积(convolution)是数学知…

    2022年5月8日
    71

发表回复

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

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