40
40
* DataAccessExceptions, following the <code>org.springframework.dao</code> exception hierarchy.
41
41
* Uses the same {@link org.springframework.jdbc.support.SQLExceptionTranslator} mechanism as
42
42
* {@link org.springframework.jdbc.core.JdbcTemplate}.
43
- *
43
+ * <p>
44
44
* The main method of this class executes a callback that implements a data access action.
45
45
* Furthermore, this class provides numerous convenience methods that mirror
46
46
* {@link org.apache.ibatis.session.SqlSession}'s execution methods.
47
- *
47
+ * <p>
48
48
* It is generally recommended to use the convenience methods on this template for plain
49
49
* query/insert/update/delete operations. However, for more complex operations like batch updates, a
50
50
* custom SqlSessionCallback must be implemented, usually as anonymous inner class. For example:
51
51
*
52
52
* <pre class="code">
53
+ * {@code
53
54
* getSqlSessionTemplate().execute(new SqlSessionCallback<Object>() {
54
55
* public Object doInSqlSession(SqlSession sqlSession) throws SQLException {
55
56
* sqlSession.getMapper(MyMapper.class).update(parameterObject);
56
57
* sqlSession.update("MyMapper.update", otherParameterObject);
57
58
* return null;
58
59
* }
59
60
* }, ExecutorType.BATCH);
61
+ * }
60
62
* </pre>
61
63
*
62
64
* 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>
67
79
*
68
80
* @see #execute
69
81
* @see #setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)
75
87
*/
76
88
public class SqlSessionTemplate extends JdbcAccessor implements SqlSessionOperations {
77
89
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
+ }
79
107
80
108
public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory ) {
81
- Assert . notNull (sqlSessionFactory , "Property 'sqlSessionFactory' is required" );
82
- this . sqlSessionFactory = sqlSessionFactory ;
109
+ setSqlSessionFactory (sqlSessionFactory );
110
+ afterPropertiesSet () ;
83
111
}
84
112
85
113
public SqlSessionFactory getSqlSessionFactory () {
86
114
return sqlSessionFactory ;
87
115
}
88
116
117
+ public void setSqlSessionFactory (SqlSessionFactory sqlSessionFactory ) {
118
+ this .sqlSessionFactory = sqlSessionFactory ;
119
+ }
120
+
89
121
/**
90
122
* {@inheritDoc}
91
123
*/
@@ -102,6 +134,15 @@ public DataSource getDataSource() {
102
134
return this .sqlSessionFactory .getConfiguration ().getEnvironment ().getDataSource ();
103
135
}
104
136
137
+ /**
138
+ * {@inheritDoc}
139
+ */
140
+ @ Override
141
+ public void afterPropertiesSet () {
142
+ Assert .notNull (this .sqlSessionFactory , "Property 'sqlSessionFactory' is required" );
143
+ super .afterPropertiesSet ();
144
+ }
145
+
105
146
/**
106
147
* Execute the given data access action with the proper SqlSession (got from current transaction or
107
148
* a new one)
@@ -207,7 +248,7 @@ public List<T> doInSqlSession(SqlSession sqlSession) {
207
248
// }
208
249
// });
209
250
// }
210
-
251
+
211
252
/**
212
253
* {@inheritDoc}
213
254
*/
0 commit comments