Skip to content

Commit 53f58c7

Browse files
committed
[mybatis-spring] minor docbook updates
1 parent 10296c4 commit 53f58c7

File tree

7 files changed

+54
-76
lines changed

7 files changed

+54
-76
lines changed

src/docbkx/getting_started.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<para>
2626
This chapter will show you in a few steps how to install and setup MyBatis-Spring and how to build
27-
a simple data access transactional application.
27+
a simple transactional application.
2828
</para>
2929

3030
<section id="getting_started.installation">
@@ -37,13 +37,11 @@
3737
<para>
3838
If you are using Maven just add the following dependency
3939
to your pom.xml:
40-
<programlisting language="xml"><![CDATA[
41-
<dependency>
40+
<programlisting language="xml"><![CDATA[<dependency>
4241
<groupId>org.mybatis</groupId>
4342
<artifactId>mybatis-spring</artifactId>
4443
<version>]]><?eval ${project.version}?><![CDATA[</version>
45-
</dependency>]]>
46-
</programlisting>
44+
</dependency>]]></programlisting>
4745
</para>
4846
</section>
4947

@@ -105,8 +103,7 @@
105103

106104
<para>
107105
Calling MyBatis data methods is now only one line of code:
108-
<programlisting language="java"><![CDATA[
109-
public class FooServiceImpl implements FooService {
106+
<programlisting language="java"><![CDATA[public class FooServiceImpl implements FooService {
110107
111108
private UserMapper userMapper;
112109
@@ -117,9 +114,7 @@ public class FooServiceImpl implements FooService {
117114
public User doSomeBusinessStuff(String userId) {
118115
return this.userMapper.getUser(userId);
119116
}
120-
}
121-
]]></programlisting>
117+
}]]></programlisting>
122118
</para>
123-
124119
</section>
125120
</chapter>

src/docbkx/introduction.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030
Spring. Using the classes in this library, Spring will load the necessary
3131
MyBatis factory and session classes for you. This library also provides
3232
an easy way to inject MyBatis data mappers and <literal>SqlSession</literal>s
33-
into your service beans. It will also handle transactions and translate MyBatis
34-
exceptions into Spring <literal>DataAccessException</literal>s.
35-
And Finally, it will let you build your application code free of
33+
into your service beans. It will also handle transactions and translate MyBatis
34+
exceptions into Spring <literal>DataAccessException</literal>s.
35+
Finally, it will let you build your application code free of
3636
dependencies on MyBatis, Spring or MyBatis-Spring.
3737
</para>
3838
</section>
3939

4040
<section id="introduction.motivation">
4141
<title>Motivation</title>
4242
<para>
43-
As of version 2, Spring 3.0 only supports iBATIS version 2. An attempt
44-
was made to add MyBatis 3 support into Spring 3.0 (see the Spring Jira
43+
Spring version 2 only supports iBATIS version 2. An attempt was made to
44+
add MyBatis 3 support into Spring 3.0 (see the Spring Jira
4545
<ulink url="https://jira.springsource.org/browse/SPR-5991">issue</ulink>).
4646
Unfortunately, Spring 3.0 development ended before MyBatis 3.0 was
4747
officially released. Because the Spring team did not want to release with
@@ -77,5 +77,4 @@
7777
this project wouldn't exist.
7878
</para>
7979
</section>
80-
8180
</chapter>

src/docbkx/mappers.xml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
MyBatis, Spring or MyBatis-Spring. The proxy that
3939
<literal>MapperFactoryBean</literal> creates handles opening and closing
4040
the session as well as translating any exceptions into Spring
41-
<literal>DataAccessExceptions</literal>. In addition, the proxy will start
41+
<literal>DataAccessException</literal>s. In addition, the proxy will start
4242
a new Spring transaction if required or participate in an existing one if
4343
it a transaction is active.
4444
</para>
@@ -73,11 +73,11 @@
7373
</para>
7474

7575
<para>
76-
Note that while <literal>MapperFactoryBean</literal> requires either an
76+
Note that <literal>MapperFactoryBean</literal> requires either an
7777
<literal>SqlSessionFactory</literal> or an
7878
<literal>SqlSessionTemplate</literal>. These can be set through the
79-
respective <literal>sqlSessionFactory</literal> or
80-
<literal>sqlSessionTemplate</literal> properties or they can
79+
respective <literal>sqlSessionFactory</literal> and
80+
<literal>sqlSessionTemplate</literal> properties, or they can
8181
be autowired by Spring. If both properties are set, the
8282
<literal>SqlSessionFactory</literal> is ignored. Since the
8383
<literal>SqlSessionTemplate</literal> is required to have a session
@@ -120,7 +120,7 @@
120120
There is no need to register all your mappers in the Spring XML file.
121121
Instead, you can use a <literal>MapperScannerConfigurer</literal> that
122122
will search the classpath for your mappers and set them up
123-
automatically as <literal>MapperFactoryBeans</literal>.
123+
automatically as <literal>MapperFactoryBean</literal>s.
124124
</para>
125125

126126
<para>
@@ -141,7 +141,7 @@
141141
<literal>SqlSessionFactory</literal> or <literal>SqlSessionTemplate</literal>
142142
because the <literal>MapperScannerConfigurer</literal> will autowire
143143
<literal>MapperFactoryBeans</literal>. But, if you are using more than
144-
one <literal>DataSource</literal>, (thus, more than one
144+
one <literal>DataSource</literal> (thus, more than one
145145
SqlSessionFactory), autowiring will not work. In this case you can use
146146
the <literal>sqlSessionFactory</literal> or
147147
<literal>sqlSessionTemplate</literal> properties to set the right
@@ -158,20 +158,18 @@
158158
By default, these two properties are null, so all interfaces in the given
159159
base package(s) will be loaded as mappers.
160160
</para>
161-
161+
162162
<para>
163163
Discovered mappers will be named using Spring default naming strategy for
164-
autodetected components (see Spring manual 3.14.4).
164+
autodetected components (see section 3.14.4 of the Spring manual).
165165
That is, if no annotation is found, it will use the uncapitalized non-qualified class
166166
name of the mapper. But if either a @Component or a JSR-330 @Named annotation is
167-
found it will get the name from it.
167+
found it will get the name from the annotation.
168168
Notice that you can set the <literal>annotationClass</literal> property
169169
to <literal>org.springframework.stereotype.Component</literal>,
170170
<literal>javax.inject.Named</literal> (if you have JSE 6) or to your own annotation
171171
(that must be itself annotated) so the annotation will work both as a marker
172172
and as a name provider.
173173
</para>
174-
175174
</section>
176-
177175
</chapter>

src/docbkx/sample.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
The service is composed by an interface <literal>FooService.java</literal>
5252
and an implementation <literal>FooServiceImpl.java</literal>. This service is transactional
5353
so a transaction is started when any of its methods is called and committed when
54-
the method ends without throwing a unchecked exception.
54+
the method ends without throwing an unchecked exception.
5555
<programlisting language="java"><![CDATA[@Transactional
5656
public interface FooService {
5757
User doSomeBusinessStuff(String userId);
@@ -131,7 +131,7 @@ public interface FooService {
131131
</tr>
132132
</tbody>
133133
</table>
134-
Please have a look at the different <literal>applicationContext.xml</literal>
134+
Please take a look at the different <literal>applicationContext.xml</literal>
135135
files to see MyBatis-Spring in action.
136136
</para>
137137
</chapter>

src/docbkx/templates_support.xml

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
</para>
3535

3636
<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
3939
into your beans, will be all that is needed. The
4040
<link linkend="mappers.mapperfactorybean"><literal>MapperFactoryBean</literal></link>
4141
will be explained in detail in the next chapter.
@@ -63,23 +63,19 @@
6363

6464
<para>
6565
<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>
7168
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.
7572
</para>
7673

7774
<para>
7875
A <literal>SqlSessionTemplate</literal> can be constructed
7976
using an <literal>SqlSessionFactory</literal> as a constructor argument.
8077

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">
8379
<constructor-arg index="0" ref="sqlSessionFactory" />
8480
</bean>]]></programlisting>
8581
</para>
@@ -88,8 +84,7 @@
8884
This bean can now be injected directly in your DAO beans. You need a
8985
<literal>SqlSession</literal> property in your bean like the following
9086

91-
<programlisting language="java"><![CDATA[
92-
public class UserDaoImpl implements UserDao {
87+
<programlisting language="java"><![CDATA[public class UserDaoImpl implements UserDao {
9388
9489
private SqlSession sqlSession;
9590
@@ -104,31 +99,28 @@ public class UserDaoImpl implements UserDao {
10499

105100
And inject the <literal>SqlSessionTemplate</literal> as follows
106101

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">
109103
<property name="sqlSession" ref="sqlSession" />
110104
</bean>]]></programlisting>
111105
</para>
112106

113107
<para>
114108
<literal>SqlSessionTemplate</literal> has also a constructor that takes
115109
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
117111
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">
120113
<constructor-arg index="0" ref="sqlSessionFactory" />
121114
<constructor-arg index="1" value="BATCH" />
122115
</bean>]]></programlisting>
123116

124117
Now all your statements will be batched so the following could be coded
125118
in a DAO
126-
<programlisting language="java"><![CDATA[
127-
public void insertUsers(User[] users) {
119+
<programlisting language="java"><![CDATA[public void insertUsers(User[] users) {
128120
for (User user : users) {
129121
sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", user);
130122
}
131-
}]]></programlisting>
123+
}]]></programlisting>
132124
</para>
133125

134126
<para>
@@ -142,7 +134,7 @@ public class UserDaoImpl implements UserDao {
142134
there <emphasis>cannot</emphasis> be an existing transaction running with
143135
a different ExecutorType when this method is called. Either ensure that
144136
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
146138
completely outside of a transaction.
147139
</para>
148140
</section>
@@ -156,8 +148,7 @@ public class UserDaoImpl implements UserDao {
156148
<literal>getSqlSession()</literal> you will get a <literal>SqlSessionTemplate</literal>
157149
which can then be used to execute SQL methods, like the following:
158150

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 {
161152
public User getUser(String userId) {
162153
return (User) getSqlSession()
163154
.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
@@ -182,11 +173,9 @@ public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
182173
<literal>SqlSessionDaoSupport</literal>, it can be configured in Spring
183174
like the following:
184175

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">
187177
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
188178
</bean>]]></programlisting>
189179
</para>
190180
</section>
191-
192181
</chapter>

src/docbkx/transactions.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
</para>
3131

3232
<para>
33-
Once a Spring PlatformTransactionManager is configured, you can configure
33+
Once a Spring transaction manager is configured, you can configure
3434
transactions in Spring as you normally would. Both
35-
<literal>@Transactional</literal> annotations as well as AOP style
35+
<literal>@Transactional</literal> annotations and AOP style
3636
configurations are supported. A single <literal>SqlSession</literal>
3737
object will be created and used for the duration of the transaction. This
3838
session will be committed or rolled back as appropriate when then
@@ -99,8 +99,7 @@
9999
also configure the <literal>SqlSessionFactoryBean</literal> to use the base
100100
MyBatis <literal>ManagedTransactionFactory</literal>:
101101

102-
<programlisting language="xml"><![CDATA[
103-
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
102+
<programlisting language="xml"><![CDATA[<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
104103
<property name="dataSource" ref="dataSource" />
105104
<property name="transactionFactory">
106105
<bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />
@@ -110,7 +109,7 @@
110109
</section>
111110

112111
<section id="transactions.programmatic">
113-
<title>Programmatic transaction management</title>
112+
<title>Programmatic Transaction Management</title>
114113
<para>
115114
MyBatis <literal>SqlSession</literal> provides you with specific methods to handle
116115
transactions programmatically. But when using MyBatis-Spring your beans will be
@@ -121,20 +120,18 @@
121120
You cannot call <literal>SqlSession.commit()</literal>, <literal>SqlSession.rollback()</literal>
122121
or <literal>SqlSession.close()</literal> over a Spring managed <literal>SqlSession</literal>.
123122
If you try to do so, a <literal>UnsupportedOperationException</literal> exception
124-
will be thrown. In case you are using injected mappers you won't even have access
125-
to those methods.
123+
will be thrown. Note these methods are not exposed in injected mapper classes.
126124
</para>
127125
<para>
128-
Regardless your connection's autocommit is set or not, any execution of a
126+
Regardless of your JDBC connection's autocommit setting, any execution of a
129127
<literal>SqlSession</literal> data method or any call to a mapper
130-
method outside a Spring transaction will be automatically committed.
128+
method outside a Spring transaction will be automatically committed.
131129
</para>
132130
<para>
133131
If you want to control your transactions programmatically please refer to chapter 10.6 of
134132
the Spring reference manual. This code shows how to handle a transaction manually
135133
using the <literal>PlatformTransactionManager</literal> described in section 10.6.2.
136-
<programlisting language="java"><![CDATA[
137-
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
134+
<programlisting language="java"><![CDATA[DefaultTransactionDefinition def = new DefaultTransactionDefinition();
138135
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
139136
140137
TransactionStatus status = txManager.getTransaction(def);
@@ -145,9 +142,9 @@ catch (MyException ex) {
145142
txManager.rollback(status);
146143
throw ex;
147144
}
148-
txManager.commit(status);
149-
]]></programlisting>
150-
Notice that this code shows a mapper but it will also work with a <literal>SqlSession</literal>.
145+
txManager.commit(status);]]></programlisting>
146+
147+
Notice that this code uses a mapper, but it will also work with a <literal>SqlSession</literal>.
151148
</para>
152149
</section>
153150
</chapter>

src/docbkx/using_api.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
</para>
5050

5151
<para>
52-
Use this option <emphasis>with care</emphasis> because wrong usage may produce runtime errors or,
53-
much worse, data integrity problems. These are the caveats:
52+
Use this option <emphasis>with care</emphasis> because wrong usage may produce runtime errors or
53+
worse, data integrity problems. Be aware of the following caveats with direct API usage:
5454
<itemizedlist>
5555
<listitem>
5656
<para>
@@ -66,20 +66,20 @@
6666
</listitem>
6767
<listitem>
6868
<para>
69-
MyBatis default <literal>SqlSession</literal> is not thread safe. If you
69+
MyBatis' <literal>DefaultSqlSession</literal> is not thread safe. If you
7070
inject it in your beans you <emphasis>will</emphasis> get errors.
7171
</para>
7272
</listitem>
7373
<listitem>
7474
<para>
75-
Mappers got from MyBatis default <literal>SqlSession</literal> are not thread safe either.
75+
Mappers created using <literal>DefaultSqlSession</literal> are not thread safe either.
7676
If you inject them it in your beans you <emphasis>will</emphasis> get errors.
7777
</para>
7878
</listitem>
7979
<listitem>
8080
<para>
8181
You must make sure that your <literal>SqlSession</literal>s
82-
are <emphasis>always</emphasis> closed.
82+
are <emphasis>always</emphasis> closed in a finally block.
8383
</para>
8484
</listitem>
8585
</itemizedlist>

0 commit comments

Comments
 (0)