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)
上一篇 2022年1月14日 下午11:00
下一篇 2022年1月14日 下午11:00


相关推荐

  • 40+个性鲜明的企业网站展示

    40+个性鲜明的企业网站展示何谓企业网站 一般指的事生产或者服务型企业的官方网站 对提升产品或服务知名度起着重要的作用 如果让你的企业网站脱颖而出 看看一下的展示吧 或许能给你灵感 原文地址 http www goodfav com brilliant business websites that showcase products 15856 htmlNorquayc nbsp MOREINFO

    2025年10月6日
    5
  • 处理pdb文件

    处理pdb文件importosfrommathimportsqrtimportnumpyimporttorchfromBio.PDBimportPDBParserfromtorch.utils.dataimportDataset,DataLoaderfromtorch.utils.tensorboardimportSummaryWriterdevice=torch.device(“cuda”)classP450Dataset(Dataset):de.

    2022年6月2日
    52
  • object finalized_finalize()方法

    object finalized_finalize()方法一、一次标记首先finalize方法是在垃圾回收时,用于确认该对象是否确认被回收的一个标记过程。确认一个对象真正被回收需要经历两次标记过程:可达性分析没有引用,这是第一次标记是否有必要执行finalize方法,如果对象没有重写finalize方法或者finalize方法已经被调用过了,那么finalize方法就是没有必要执行的,没有必要执行finalize方法的对象就会被直接回收。如果对象被判定为有必要执行finalize()方法,那么这个对象将会放置在一个叫做F-Queue的队列之中,并在稍后

    2026年1月21日
    5
  • 《github一天一道算法题》:分治法求数组最大连续子序列和「建议收藏」

    《github一天一道算法题》:分治法求数组最大连续子序列和

    2022年1月29日
    98
  • GridLayout 使用总结「建议收藏」

    GridLayout 使用总结「建议收藏」一、简介GridLayout是Android4.0引入的网格布局,使用它可以减少布局嵌套。也算是常用,但一直没仔细看过,今天研究一下二、常用属性介绍GridLayout使用属性属性作用android:columnCount最大列数android:rowCount最大行数android:orientationGr…

    2022年6月14日
    30
  • REST接口规范总结

    REST接口规范总结REST 接口规范总结

    2026年3月17日
    2

发表回复

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

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