4040 * DataAccessExceptions, following the <code>org.springframework.dao</code> exception hierarchy.
4141 * Uses the same {@link org.springframework.jdbc.support.SQLExceptionTranslator} mechanism as
4242 * {@link org.springframework.jdbc.core.JdbcTemplate}.
43- *
43+ * <p>
4444 * The main method of this class executes a callback that implements a data access action.
4545 * Furthermore, this class provides numerous convenience methods that mirror
4646 * {@link org.apache.ibatis.session.SqlSession}'s execution methods.
47- *
47+ * <p>
4848 * It is generally recommended to use the convenience methods on this template for plain
4949 * query/insert/update/delete operations. However, for more complex operations like batch updates, a
5050 * custom SqlSessionCallback must be implemented, usually as anonymous inner class. For example:
5151 *
5252 * <pre class="code">
53+ * {@code
5354 * getSqlSessionTemplate().execute(new SqlSessionCallback<Object>() {
5455 * public Object doInSqlSession(SqlSession sqlSession) throws SQLException {
5556 * sqlSession.getMapper(MyMapper.class).update(parameterObject);
5657 * sqlSession.update("MyMapper.update", otherParameterObject);
5758 * return null;
5859 * }
5960 * }, ExecutorType.BATCH);
61+ * }
6062 * </pre>
6163 *
6264 * The template needs a SqlSessionFactory to create SqlSessions, passed in via the
63- * "sqlSessionFactory" property. A Spring context typically uses a {@link SqlSessionFactoryBean} to
64- * build the SqlSessionFactory. The template can additionally be configured with a DataSource for
65- * fetching Connections, although this is not necessary since a DataSource is specified for the
66- * SqlSessionFactory itself (through configured Environment).
65+ * "sqlSessionFactory" property or as a constructor argument.
66+ * <p>
67+ * SqlSessionTemplate is thread safe, so a single instance can be shared by all DAOs; there
68+ * should also be a small memory savings by doing this. To support a shared template, this class has
69+ * a constructor that accepts an SqlSessionTemplate. This pattern can be used in Spring
70+ * configuration files as follows:
71+ *
72+ * <pre class="code">
73+ * {@code
74+ * <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
75+ * <property name="sqlSessionFactory" ref="sqlSessionFactory" />
76+ * </bean>
77+ * }
78+ * </pre>
6779 *
6880 * @see #execute
6981 * @see #setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)
7587 */
7688public class SqlSessionTemplate extends JdbcAccessor implements SqlSessionOperations {
7789
78- private final SqlSessionFactory sqlSessionFactory ;
90+ private SqlSessionFactory sqlSessionFactory ;
91+
92+ /**
93+ * This constructor is left here to enable the creation of the SqlSessionTemplate
94+ * using this xml in the applicationContext.xml. Otherwise constructor should be used
95+ * and that will not match how other mybatis-spring beans are created.
96+ *
97+ * <pre class="code">
98+ * {@code
99+ * <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
100+ * <property name="sqlSessionFactory" ref="sqlSessionFactory" />
101+ * </bean>
102+ * }
103+ * </pre>
104+ */
105+ public SqlSessionTemplate () {
106+ }
79107
80108 public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory ) {
81- Assert . notNull (sqlSessionFactory , "Property 'sqlSessionFactory' is required" );
82- this . sqlSessionFactory = sqlSessionFactory ;
109+ setSqlSessionFactory (sqlSessionFactory );
110+ afterPropertiesSet () ;
83111 }
84112
85113 public SqlSessionFactory getSqlSessionFactory () {
86114 return sqlSessionFactory ;
87115 }
88116
117+ public void setSqlSessionFactory (SqlSessionFactory sqlSessionFactory ) {
118+ this .sqlSessionFactory = sqlSessionFactory ;
119+ }
120+
89121 /**
90122 * {@inheritDoc}
91123 */
@@ -102,6 +134,15 @@ public DataSource getDataSource() {
102134 return this .sqlSessionFactory .getConfiguration ().getEnvironment ().getDataSource ();
103135 }
104136
137+ /**
138+ * {@inheritDoc}
139+ */
140+ @ Override
141+ public void afterPropertiesSet () {
142+ Assert .notNull (this .sqlSessionFactory , "Property 'sqlSessionFactory' is required" );
143+ super .afterPropertiesSet ();
144+ }
145+
105146 /**
106147 * Execute the given data access action with the proper SqlSession (got from current transaction or
107148 * a new one)
@@ -207,7 +248,7 @@ public List<T> doInSqlSession(SqlSession sqlSession) {
207248// }
208249// });
209250// }
210-
251+
211252 /**
212253 * {@inheritDoc}
213254 */
0 commit comments