View Javadoc
1   /*
2    * Copyright 2010-2025 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  
18  import static org.springframework.util.Assert.notNull;
19  
20  import org.apache.ibatis.session.SqlSession;
21  import org.apache.ibatis.session.SqlSessionFactory;
22  import org.mybatis.spring.SqlSessionTemplate;
23  import org.springframework.dao.support.DaoSupport;
24  
25  /**
26   * Convenient super class for MyBatis SqlSession data access objects. It gives you access to the template which can then
27   * be used to execute SQL methods.
28   * <p>
29   * This class needs a SqlSessionTemplate or a SqlSessionFactory. If both are set the SqlSessionFactory will be ignored.
30   *
31   * @author Putthiphong Boonphong
32   * @author Eduardo Macarron
33   *
34   * @see #setSqlSessionFactory
35   * @see #setSqlSessionTemplate
36   * @see SqlSessionTemplate
37   */
38  public abstract class SqlSessionDaoSupport extends DaoSupport {
39  
40    private SqlSessionTemplate sqlSessionTemplate;
41  
42    /**
43     * Set MyBatis SqlSessionFactory to be used by this DAO. Will automatically create SqlSessionTemplate for the given
44     * SqlSessionFactory.
45     *
46     * @param sqlSessionFactory
47     *          a factory of SqlSession
48     */
49    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
50      if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) {
51        this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory);
52      }
53    }
54  
55    /**
56     * Create a SqlSessionTemplate for the given SqlSessionFactory. Only invoked if populating the DAO with a
57     * SqlSessionFactory reference!
58     * <p>
59     * Can be overridden in subclasses to provide a SqlSessionTemplate instance with different configuration, or a custom
60     * SqlSessionTemplate subclass.
61     *
62     * @param sqlSessionFactory
63     *          the MyBatis SqlSessionFactory to create a SqlSessionTemplate for
64     *
65     * @return the new SqlSessionTemplate instance
66     *
67     * @see #setSqlSessionFactory
68     */
69    @SuppressWarnings("WeakerAccess")
70    protected SqlSessionTemplate createSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
71      return new SqlSessionTemplate(sqlSessionFactory);
72    }
73  
74    /**
75     * Return the MyBatis SqlSessionFactory used by this DAO.
76     *
77     * @return a factory of SqlSession
78     */
79    public final SqlSessionFactory getSqlSessionFactory() {
80      return this.sqlSessionTemplate != null ? this.sqlSessionTemplate.getSqlSessionFactory() : null;
81    }
82  
83    /**
84     * Set the SqlSessionTemplate for this DAO explicitly, as an alternative to specifying a SqlSessionFactory.
85     *
86     * @param sqlSessionTemplate
87     *          a template of SqlSession
88     *
89     * @see #setSqlSessionFactory
90     */
91    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
92      this.sqlSessionTemplate = sqlSessionTemplate;
93    }
94  
95    /**
96     * Users should use this method to get a SqlSession to call its statement methods This is SqlSession is managed by
97     * spring. Users should not commit/rollback/close it because it will be automatically done.
98     *
99     * @return Spring managed thread safe SqlSession
100    */
101   public SqlSession getSqlSession() {
102     return this.sqlSessionTemplate;
103   }
104 
105   /**
106    * Return the SqlSessionTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
107    * <p>
108    * <b>Note: The returned SqlSessionTemplate is a shared instance.</b> You may introspect its configuration, but not
109    * modify the configuration (other than from within an {@link #initDao} implementation). Consider creating a custom
110    * SqlSessionTemplate instance via {@code new SqlSessionTemplate(getSqlSessionFactory())}, in which case you're
111    * allowed to customize the settings on the resulting instance.
112    *
113    * @return a template of SqlSession
114    */
115   public SqlSessionTemplate getSqlSessionTemplate() {
116     return this.sqlSessionTemplate;
117   }
118 
119   @Override
120   protected void checkDaoConfig() {
121     notNull(this.sqlSessionTemplate, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");
122   }
123 
124 }