Skip to content

Commit 829e14a

Browse files
committed
added SqlSessionDaoTemplate and SqlSessionDaoSupport chapter
1 parent 14d1cd5 commit 829e14a

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/docbkx/index.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545

4646
<xi:include href="introduction.xml" />
4747
<xi:include href="bootstrap.xml" />
48+
<xi:include href="templates_support.xml" />
4849

4950
</book>

src/docbkx/templates_support.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
3+
<!--
4+
Copyright 2010 The myBatis Team
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<!--
20+
version: $Id$
21+
-->
22+
<chapter id="templates_support">
23+
<title>Using SqlSessionDaoTemplate and SqlSessionDaoSupport</title>
24+
25+
<section id="templates_support.template">
26+
<title>SqlSessionDaoSupport</title>
27+
<para>
28+
SqlSessionDaoSupport is a support class that helps building DAOs with a very simple API, like in the sample
29+
below:
30+
<programlisting language="java"><![CDATA[public class UserMapperTemplateImpl
31+
extends SqlSessionDaoSupport
32+
implements UserMapper {
33+
34+
public User getUser(String userId) {
35+
return (User) getSqlSessionTemplate().selectOne("sample.UserMapper.getUser", userId);
36+
}
37+
38+
}]]></programlisting>
39+
As the example shows instead of using a <literal>SqlSession</literal> you just use
40+
<literal>SqlSessionDaoTemplate</literal> to execute MyBatis methods (<literal>selectOne</literal>,
41+
<literal>selectList</literal>...)
42+
</para>
43+
</section>
44+
45+
<section id="templates_support.support">
46+
<title></title>
47+
<para>
48+
<literal>SqlSessionDaoTemplate</literal> is able to create a new <literal>SqlSession</literal> or get the
49+
active <literal>SqlSession</literal> from current transaction. It also translates exceptions to Spring's
50+
generic<literal>DataAccessException</literal> hierarchy.
51+
</para>
52+
53+
<para>
54+
The <literal>SqlSessionDaoTemplate</literal> offers a generic method, taking a custom
55+
<literal>SqlSessionCallback</literal> as argument so that you can execute more than one method over a
56+
<literal>SqlSession</literal>:
57+
<programlisting language="java"><![CDATA[public void inserUser(final User user) {
58+
59+
getSqlSessionTemplate().execute(new SqlSessionCallback<Object>() {
60+
61+
public Object doInSqlSession(SqlSession sqlSession) throws SQLException {
62+
sqlSession.insert("sample.UserMapper.insertUser", user);
63+
sqlSession.insert("sample.UserMapper.insertAccount", user.getId());
64+
return null;
65+
}
66+
67+
});
68+
69+
}]]></programlisting>
70+
71+
A <literal>SqlSessionDaoTemplate</literal> can also be created using a <literal>SqlSessionFactory</literal>
72+
as a constructor argument. In this case there is no need that you DAOs extends <literal>SqlSessionDaoSupport</literal>.
73+
</para>
74+
</section>
75+
76+
</chapter>

0 commit comments

Comments
 (0)