34
34
</para >
35
35
36
36
<para >
37
- Note that is usually not necessary to use a <literal >SqlSession</literal > directly.
38
- In most cases a <literal >MapperFactoryBean</literal >, that will inject mappers
37
+ Note that it is usually not necessary to use a <literal >SqlSession</literal > directly.
38
+ In most cases a <literal >MapperFactoryBean</literal > that will inject mappers
39
39
into your beans, will be all that is needed. The
40
40
<link linkend =" mappers.mapperfactorybean" ><literal >MapperFactoryBean</literal ></link >
41
41
will be explained in detail in the next chapter.
63
63
64
64
<para >
65
65
<literal >SqlSessionTemplate</literal > implements <literal >SqlSession</literal >
66
- and is meant to be a drop-in replacement for MyBatis <literal >SqlSession</literal >.
67
- </para >
68
-
69
- <para >
70
- <literal >SqlSessionTemplate</literal > should <emphasis >always</emphasis >
66
+ and is meant to be a drop-in replacement for any existing use of <literal >SqlSession</literal >
67
+ in your code. <literal >SqlSessionTemplate</literal > should <emphasis >always</emphasis >
71
68
be used instead of default MyBatis implementation <literal >DefaultSqlSession</literal >
72
- because it cannot participate in Spring transactions and it cannot be
73
- injected because it is not thread safe.
74
- Switching between the two classes in the same application can cause data integrity issues.
69
+ because the template can participate in Spring transactions and is thread safe for use by
70
+ multiple injected mapper classes. Switching between the two classes in the
71
+ same application can cause data integrity issues.
75
72
</para >
76
73
77
74
<para >
78
75
A <literal >SqlSessionTemplate</literal > can be constructed
79
76
using an <literal >SqlSessionFactory</literal > as a constructor argument.
80
77
81
- <programlisting language =" xml" ><![CDATA[
82
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
78
+ <programlisting language =" xml" ><![CDATA[ <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
83
79
<constructor-arg index="0" ref="sqlSessionFactory" />
84
80
</bean>]]> </programlisting >
85
81
</para >
88
84
This bean can now be injected directly in your DAO beans. You need a
89
85
<literal >SqlSession</literal > property in your bean like the following
90
86
91
- <programlisting language =" java" ><![CDATA[
92
- public class UserDaoImpl implements UserDao {
87
+ <programlisting language =" java" ><![CDATA[ public class UserDaoImpl implements UserDao {
93
88
94
89
private SqlSession sqlSession;
95
90
@@ -104,31 +99,28 @@ public class UserDaoImpl implements UserDao {
104
99
105
100
And inject the <literal >SqlSessionTemplate</literal > as follows
106
101
107
- <programlisting language =" java" ><![CDATA[
108
- <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
102
+ <programlisting language =" java" ><![CDATA[ <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
109
103
<property name="sqlSession" ref="sqlSession" />
110
104
</bean>]]> </programlisting >
111
105
</para >
112
106
113
107
<para >
114
108
<literal >SqlSessionTemplate</literal > has also a constructor that takes
115
109
an <literal >ExecutorType</literal > as an argument. This allows you to
116
- construct, for example, a batch <literal >SqlSession</literal > but using
110
+ construct, for example, a batch <literal >SqlSession</literal > by using
117
111
the following in Spring's configuration xml:
118
- <programlisting language =" xml" ><![CDATA[
119
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
112
+ <programlisting language =" xml" ><![CDATA[ <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
120
113
<constructor-arg index="0" ref="sqlSessionFactory" />
121
114
<constructor-arg index="1" value="BATCH" />
122
115
</bean>]]> </programlisting >
123
116
124
117
Now all your statements will be batched so the following could be coded
125
118
in a DAO
126
- <programlisting language =" java" ><![CDATA[
127
- public void insertUsers(User[] users) {
119
+ <programlisting language =" java" ><![CDATA[ public void insertUsers(User[] users) {
128
120
for (User user : users) {
129
121
sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", user);
130
122
}
131
- }]]> </programlisting >
123
+ }]]> </programlisting >
132
124
</para >
133
125
134
126
<para >
@@ -142,7 +134,7 @@ public class UserDaoImpl implements UserDao {
142
134
there <emphasis >cannot</emphasis > be an existing transaction running with
143
135
a different ExecutorType when this method is called. Either ensure that
144
136
calls to <literal >SqlSessionTemplate</literal >s with different executor
145
- types run in a separate transaction, with PROPAGATION_REQUIRES_NEW or
137
+ types run in a separate transaction (e.g. with PROPAGATION_REQUIRES_NEW) or
146
138
completely outside of a transaction.
147
139
</para >
148
140
</section >
@@ -156,8 +148,7 @@ public class UserDaoImpl implements UserDao {
156
148
<literal >getSqlSession()</literal > you will get a <literal >SqlSessionTemplate</literal >
157
149
which can then be used to execute SQL methods, like the following:
158
150
159
- <programlisting language =" java" ><![CDATA[
160
- public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
151
+ <programlisting language =" java" ><![CDATA[ public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
161
152
public User getUser(String userId) {
162
153
return (User) getSqlSession()
163
154
.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
@@ -182,11 +173,9 @@ public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
182
173
<literal >SqlSessionDaoSupport</literal >, it can be configured in Spring
183
174
like the following:
184
175
185
- <programlisting language =" xml" ><![CDATA[
186
- <bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserDaoImpl">
176
+ <programlisting language =" xml" ><![CDATA[ <bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserDaoImpl">
187
177
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
188
178
</bean>]]> </programlisting >
189
179
</para >
190
180
</section >
191
-
192
181
</chapter >
0 commit comments