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