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


相关推荐

  • 如何准备技术串讲活动_技术准备包括哪些

    如何准备技术串讲活动_技术准备包括哪些如何准备技术串讲?

    2025年6月15日
    4
  • 一张图理清SpringMVC工作原理

    一张图理清SpringMVC工作原理一、首先,我们先来认识一下SpringMVC的主要组件前端控制器(DisatcherServlet):接收请求,响应结果,返回可以是json,String等数据类型,也可以是页面(Model)。处理器映射器(HandlerMapping):根据URL去查找处理器,一般通过xml配置或者注解进行查找。处理器(Handler):就是我们常说的controller控制器啦,由程序员编写。处理器适配器(Ha

    2022年5月14日
    47
  • Brup插件开发手记

    Brup插件开发手记前言在一些攻防演练中,像Shiro、Fastjson等常见高危漏洞一直被高频利用。但在一些情况下,这些漏洞通过几轮的洗刷下来出现的频率会逐渐变少。在打点的时候,一些平时并不会去

    2021年12月13日
    57
  • Eclipse Building Workspace 编译慢 解决办法

    在svn下载的项目都会一般都会有一个 .project 的文件,在导入项目前将改文件中的一些验证属性删除掉   去掉Validator 相关的,  如:                    org.eclipse.wst.jsdt.core.javascriptValidator                                   

    2022年2月24日
    41
  • 克隆节点的方法_手机克隆phone

    克隆节点的方法_手机克隆phone<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8&quo

    2022年8月4日
    11
  • lua编写游戏脚本教程_ps2游戏转ps4pkg

    lua编写游戏脚本教程_ps2游戏转ps4pkg先扫盲WQSG是干什么用的一些掌机类游戏汉化比方PSPNDS汉化必备之物它能够依据字典转换文本假设你不知道这是啥玩意,快去充电染成茜色的坂道文本提取(导出)方法(下文称导出文章)在导出文章,我

    2022年8月4日
    15

发表回复

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

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