springboot+mybatis使用SqlSessionDaoSupport出现以下错误:
查看SqlSessionDaoSupport 源码:
/*** Copyright 2010-2016 the original author or authors.** 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.* * @author Putthibong Boonbong** @see #setSqlSessionFactory* @see #setSqlSessionTemplate* @see SqlSessionTemplate*/
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}*/@Overrideprotected void checkDaoConfig() {notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");}}
从上面源代码中可以看出:SqlSessionDaoSuppor类没有自动注入SqlSessionFactory 和 SqlSessionTemplate。
解决办法:在dao的基类中,给SqlSessionDaoSupport 注入SqlSessionFactory 或 SqlSessionTemplate
@Repository("daoSuper")
@Scope("singleton")
public class DaoSuperImpl extends SqlSessionDaoSupport {
@Resource// SqlSessionFactory是已经注入容器是public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){super.setSqlSessionFactory(sqlSessionFactory);}// ...}