SqlSessionDaoSupport.java

  1. /*
  2.  * Copyright 2010-2024 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *    https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.mybatis.spring.support;

  17. import static org.springframework.util.Assert.notNull;

  18. import org.apache.ibatis.session.SqlSession;
  19. import org.apache.ibatis.session.SqlSessionFactory;
  20. import org.mybatis.spring.SqlSessionTemplate;
  21. import org.springframework.dao.support.DaoSupport;

  22. /**
  23.  * Convenient super class for MyBatis SqlSession data access objects. It gives you access to the template which can then
  24.  * be used to execute SQL methods.
  25.  * <p>
  26.  * This class needs a SqlSessionTemplate or a SqlSessionFactory. If both are set the SqlSessionFactory will be ignored.
  27.  * <p>
  28.  *
  29.  * @author Putthiphong Boonphong
  30.  * @author Eduardo Macarron
  31.  *
  32.  * @see #setSqlSessionFactory
  33.  * @see #setSqlSessionTemplate
  34.  * @see SqlSessionTemplate
  35.  */
  36. public abstract class SqlSessionDaoSupport extends DaoSupport {

  37.   private SqlSessionTemplate sqlSessionTemplate;

  38.   /**
  39.    * Set MyBatis SqlSessionFactory to be used by this DAO. Will automatically create SqlSessionTemplate for the given
  40.    * SqlSessionFactory.
  41.    *
  42.    * @param sqlSessionFactory
  43.    *          a factory of SqlSession
  44.    */
  45.   public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  46.     if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) {
  47.       this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory);
  48.     }
  49.   }

  50.   /**
  51.    * Create a SqlSessionTemplate for the given SqlSessionFactory. Only invoked if populating the DAO with a
  52.    * SqlSessionFactory reference!
  53.    * <p>
  54.    * Can be overridden in subclasses to provide a SqlSessionTemplate instance with different configuration, or a custom
  55.    * SqlSessionTemplate subclass.
  56.    *
  57.    * @param sqlSessionFactory
  58.    *          the MyBatis SqlSessionFactory to create a SqlSessionTemplate for
  59.    *
  60.    * @return the new SqlSessionTemplate instance
  61.    *
  62.    * @see #setSqlSessionFactory
  63.    */
  64.   @SuppressWarnings("WeakerAccess")
  65.   protected SqlSessionTemplate createSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
  66.     return new SqlSessionTemplate(sqlSessionFactory);
  67.   }

  68.   /**
  69.    * Return the MyBatis SqlSessionFactory used by this DAO.
  70.    *
  71.    * @return a factory of SqlSession
  72.    */
  73.   public final SqlSessionFactory getSqlSessionFactory() {
  74.     return this.sqlSessionTemplate != null ? this.sqlSessionTemplate.getSqlSessionFactory() : null;
  75.   }

  76.   /**
  77.    * Set the SqlSessionTemplate for this DAO explicitly, as an alternative to specifying a SqlSessionFactory.
  78.    *
  79.    * @param sqlSessionTemplate
  80.    *          a template of SqlSession
  81.    *
  82.    * @see #setSqlSessionFactory
  83.    */
  84.   public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
  85.     this.sqlSessionTemplate = sqlSessionTemplate;
  86.   }

  87.   /**
  88.    * Users should use this method to get a SqlSession to call its statement methods This is SqlSession is managed by
  89.    * spring. Users should not commit/rollback/close it because it will be automatically done.
  90.    *
  91.    * @return Spring managed thread safe SqlSession
  92.    */
  93.   public SqlSession getSqlSession() {
  94.     return this.sqlSessionTemplate;
  95.   }

  96.   /**
  97.    * Return the SqlSessionTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
  98.    * <p>
  99.    * <b>Note: The returned SqlSessionTemplate is a shared instance.</b> You may introspect its configuration, but not
  100.    * modify the configuration (other than from within an {@link #initDao} implementation). Consider creating a custom
  101.    * SqlSessionTemplate instance via {@code new SqlSessionTemplate(getSqlSessionFactory())}, in which case you're
  102.    * allowed to customize the settings on the resulting instance.
  103.    *
  104.    * @return a template of SqlSession
  105.    */
  106.   public SqlSessionTemplate getSqlSessionTemplate() {
  107.     return this.sqlSessionTemplate;
  108.   }

  109.   @Override
  110.   protected void checkDaoConfig() {
  111.     notNull(this.sqlSessionTemplate, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
  112.   }

  113. }