java lang IllegalArgumentException Property 'sqlSessionFact

               

代码简介

这两天在搭建一个web框架时候,遇到了一个奇怪的问题,因为是自己搭建着玩的,所以采用的框架为
SpringMVC + Spring + Mybatis,采用alibaba的 druid连接池,其他配置都OK,但是在tomcat运行时候总是报错

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

倒弄了半天,终于找到问题所在,原来是配置文件中的一个坑

代码片段

import org.mybatis.spring.support.SqlSessionDaoSupport;public abstract class BaseDao<T> extends SqlSessionDaoSupport { protected <S> S getMapper(Class<S> clazz) {  return getSqlSession().getMapper(clazz); }}

代码片段

1.2.0版本/* *    Copyright 2010-2013 The MyBatis Team * *    Licensed under the Apache License, Version 2.0 (the "License"); *    you may not use this file except in compliance with the License. *    You may obtain a copy of the License at * *       http://www.apache.org/licenses/LICENSE-2.0 * *    Unless required by applicable law or agreed to in writing, software *    distributed under the License is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *    See the License for the specific language governing permissions and *    limitations under the License. */package org.mybatis.spring.support;import static org.springframework.util.Assert.notNull;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.dao.support.DaoSupport;/** * 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;  }  /**   * Users should use this method to get a SqlSession to call its statement methods   * This is SqlSession is managed by spring. Users should not commit/rollback/close it   * because it will be automatically done.   *   * @return Spring managed thread safe SqlSession   */  public SqlSession getSqlSession() {    return this.sqlSession;  }  /**   * {@inheritDoc}   */  protected void checkDaoConfig() {    notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");  }}1.1.1 版本/* *    Copyright 2010 The myBatis Team * *    Licensed under the Apache License, Version 2.0 (the "License"); *    you may not use this file except in compliance with the License. *    You may obtain a copy of the License at * *       http://www.apache.org/licenses/LICENSE-2.0 * *    Unless required by applicable law or agreed to in writing, software *    distributed under the License is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *    See the License for the specific language governing permissions and *    limitations under the License. */package org.mybatis.spring.support;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.support.DaoSupport;import org.springframework.util.Assert;/** * 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. * * @see #setSqlSessionFactory * @see #setSqlSessionTemplate * @see SqlSessionTemplate * @version $Id: SqlSessionDaoSupport.java 3266 2010-11-22 06:56:51Z simone.tripodi $ */public abstract class SqlSessionDaoSupport extends DaoSupport {    private SqlSession sqlSession;    private boolean externalSqlSession;    @Autowired(required = false)    public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {        if (!this.externalSqlSession) {            this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);        }    }    @Autowired(required = false)    public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {        this.sqlSession = sqlSessionTemplate;        this.externalSqlSession = true;    }    /**     * Users should use this method to get a SqlSession to call its statement methods     * This is SqlSession is managed by spring. Users should not commit/rollback/close it     * because it will be automatically done.     *      * @return Spring managed thread safe SqlSession      */    public final SqlSession getSqlSession() {        return this.sqlSession;    }    /**     * {@inheritDoc}     */    protected void checkDaoConfig() {        Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");    }}

代码片段

配置文件跟我 贴出来的配置文件一样,不需要做改动,只需要在baseDao中注入sqlSessionFactory 或者 sqlSessionTemplate即可,代码如下:BaseDao.javaimport org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.support.SqlSessionDaoSupport;import org.springframework.beans.factory.annotation.Autowired;public abstract class BaseDao<T> extends SqlSessionDaoSupport { @Autowired public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {  super.setSqlSessionTemplate(sqlSessionTemplate); } protected <S> S getMapper(Class<S> clazz) {  return getSqlSession().getMapper(clazz); }}
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值