Removed iBATIS SQL Maps support
This commit is contained in:
@@ -1824,236 +1824,4 @@ TR: REVISED, PLS REVIEW. Should be *inside your war*. --><!-- </para>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
<section xml:id="orm-ibatis">
|
||||
<title>iBATIS SQL Maps</title>
|
||||
|
||||
<para>The iBATIS support in the Spring Framework much resembles the JDBC
|
||||
support in that it supports the same template style programming, and as
|
||||
with JDBC and other ORM technologies, the iBATIS support works with
|
||||
Spring's exception hierarchy and lets you enjoy Spring's IoC
|
||||
features.</para>
|
||||
|
||||
<para>Transaction management can be handled through Spring's standard
|
||||
facilities. No special transaction strategies are necessary for iBATIS,
|
||||
because no special transactional resource involved other than a JDBC
|
||||
<interfacename>Connection</interfacename>. Hence, Spring's standard JDBC
|
||||
<classname>DataSourceTransactionManager</classname> or
|
||||
<classname>JtaTransactionManager</classname> are perfectly
|
||||
sufficient.</para>
|
||||
|
||||
<note>
|
||||
<para>Spring supports iBATIS 2.x. The iBATIS 1.x support classes are no
|
||||
longer provided.<!--directed where? TR: REVISED, PLS REVIEW.--></para>
|
||||
</note>
|
||||
|
||||
<section xml:id="orm-ibatis-setup">
|
||||
<title>Setting up the <classname>SqlMapClient</classname></title>
|
||||
|
||||
<para>Using iBATIS SQL Maps involves creating SqlMap configuration files
|
||||
containing statements and result maps. Spring takes care of loading
|
||||
those using the <classname>SqlMapClientFactoryBean</classname>. For the
|
||||
examples we will be using the following <classname>Account</classname>
|
||||
class:</para>
|
||||
|
||||
<programlisting language="xml">public class Account {
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return this.email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
<para>To map this <classname>Account</classname> class<!--*previous account class*:Identify the account class and the section you're talking about
|
||||
TR: REVISED, PLS REVIEW. The Account class was part of the iBATIS 1.0 examples that were dropped a long time ago. No one has complained.
|
||||
Makes you wonder if anyone actually reads thes docs :)--> with iBATIS 2.x we
|
||||
need to create the following SQL map
|
||||
<filename>Account.xml</filename>:</para>
|
||||
|
||||
<programlisting language="xml"><sqlMap namespace="Account">
|
||||
|
||||
<resultMap id="result" class="examples.Account">
|
||||
<result property="name" column="NAME" columnIndex="1"/>
|
||||
<result property="email" column="EMAIL" columnIndex="2"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="getAccountByEmail" resultMap="result">
|
||||
select ACCOUNT.NAME, ACCOUNT.EMAIL
|
||||
from ACCOUNT
|
||||
where ACCOUNT.EMAIL = #value#
|
||||
</select>
|
||||
|
||||
<insert id="insertAccount">
|
||||
insert into ACCOUNT (NAME, EMAIL) values (#name#, #email#)
|
||||
</insert>
|
||||
|
||||
</sqlMap></programlisting>
|
||||
|
||||
<para>The configuration file for iBATIS 2 looks like this:</para>
|
||||
|
||||
<programlisting language="xml"><sqlMapConfig>
|
||||
|
||||
<sqlMap resource="example/Account.xml"/>
|
||||
|
||||
</sqlMapConfig></programlisting>
|
||||
|
||||
<para>Remember that iBATIS loads resources from the class path, so be
|
||||
sure to add the<filename>Account.xml</filename> file to the class
|
||||
path.</para>
|
||||
|
||||
<para>We can use the <classname>SqlMapClientFactoryBean</classname> in
|
||||
the Spring container. Note that with iBATIS SQL Maps 2.x, the JDBC
|
||||
<interfacename>DataSource</interfacename> is usually specified on the
|
||||
<classname>SqlMapClientFactoryBean</classname>, which enables lazy
|
||||
loading. This is the configuration needed for these bean
|
||||
definitions:<!--Need intro to this example; what's its purpose? TR: REVISED, PLS REVIEW.--></para>
|
||||
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.username}"/>
|
||||
<property name="password" value="${jdbc.password}"/>
|
||||
</bean>
|
||||
|
||||
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
|
||||
<property name="configLocation" value="WEB-INF/sqlmap-config.xml"/>
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
</beans></programlisting>
|
||||
</section>
|
||||
|
||||
<section xml:id="orm-ibatis-template">
|
||||
<title>Using <classname>SqlMapClientTemplate</classname> and
|
||||
<classname>SqlMapClientDaoSupport</classname></title>
|
||||
|
||||
<para>The <classname>SqlMapClientDaoSupport</classname> class offers a
|
||||
supporting class similar to the <classname>SqlMapDaoSupport</classname>.
|
||||
We extend it to implement our DAO:</para>
|
||||
|
||||
<programlisting language="java">public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
|
||||
|
||||
public Account getAccount(String email) throws DataAccessException {
|
||||
return (Account) getSqlMapClientTemplate().queryForObject("getAccountByEmail", email);
|
||||
}
|
||||
|
||||
public void insertAccount(Account account) throws DataAccessException {
|
||||
getSqlMapClientTemplate().update("insertAccount", account);
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
<para>In the DAO, we use the pre-configured
|
||||
<classname>SqlMapClientTemplate</classname> to execute the queries,
|
||||
after setting up the <literal>SqlMapAccountDao</literal> in the
|
||||
application context and wiring it with our
|
||||
<literal>SqlMapClient</literal> instance:</para>
|
||||
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="accountDao" class="example.SqlMapAccountDao">
|
||||
<property name="sqlMapClient" ref="sqlMapClient"/>
|
||||
</bean>
|
||||
|
||||
</beans></programlisting>
|
||||
|
||||
<para>An <classname>SqlMapTemplate</classname> instance can also be
|
||||
created manually, passing in the <literal>SqlMapClient</literal> as
|
||||
constructor argument. The <literal>SqlMapClientDaoSupport</literal> base
|
||||
class simply preinitializes a
|
||||
<classname>SqlMapClientTemplate</classname> instance for us.</para>
|
||||
|
||||
<para>The <classname>SqlMapClientTemplate</classname> offers a generic
|
||||
<literal>execute</literal> method, taking a custom
|
||||
<literal>SqlMapClientCallback</literal> implementation as argument. This
|
||||
can, for example, be used for batching:</para>
|
||||
|
||||
<programlisting language="java">public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
|
||||
|
||||
public void insertAccount(Account account) throws DataAccessException {
|
||||
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
|
||||
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
|
||||
executor.startBatch();
|
||||
executor.update("insertAccount", account);
|
||||
executor.update("insertAddress", account.getAddress());
|
||||
executor.executeBatch();
|
||||
}
|
||||
});
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
<para>In general, any combination of operations offered by the native
|
||||
<literal>SqlMapExecutor</literal> API can be used in such a callback.
|
||||
Any thrown <literal>SQLException</literal> is converted automatically to
|
||||
Spring's generic <classname>DataAccessException</classname>
|
||||
hierarchy.</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="orm-ibatis-straight">
|
||||
<title>Implementing DAOs based on plain iBATIS API</title>
|
||||
|
||||
<para>DAOs can also be written against plain iBATIS API, without any
|
||||
Spring dependencies, directly using an injected
|
||||
<literal>SqlMapClient</literal>. The following example shows a
|
||||
corresponding DAO implementation:</para>
|
||||
|
||||
<programlisting language="java">public class SqlMapAccountDao implements AccountDao {
|
||||
|
||||
private SqlMapClient sqlMapClient;
|
||||
|
||||
public void setSqlMapClient(SqlMapClient sqlMapClient) {
|
||||
this.sqlMapClient = sqlMapClient;
|
||||
}
|
||||
|
||||
public Account getAccount(String email) {
|
||||
try {
|
||||
return (Account) this.sqlMapClient.queryForObject("getAccountByEmail", email);
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
throw new MyDaoException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void insertAccount(Account account) throws DataAccessException {
|
||||
try {
|
||||
this.sqlMapClient.update("insertAccount", account);
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
throw new MyDaoException(ex);
|
||||
}
|
||||
}
|
||||
}</programlisting>
|
||||
|
||||
<para>In this scenario, you need to handle the
|
||||
<literal>SQLException</literal> thrown by the iBATIS API in a custom
|
||||
fashion, usually by wrapping it in your own application-specific DAO
|
||||
exception. Wiring in the application context would still look like it
|
||||
does in the example for the
|
||||
<classname>SqlMapClientDaoSupport</classname><!--Clarify what wiring the app context looks like. What do you mean *before*? Looks like *what* specifically? TR: REVISED, PLS REVIEW.-->,
|
||||
due to the fact that the plain iBATIS-based DAO still follows the
|
||||
dependency injection pattern:</para>
|
||||
|
||||
<programlisting language="xml"><beans>
|
||||
|
||||
<bean id="accountDao" class="example.SqlMapAccountDao">
|
||||
<property name="sqlMapClient" ref="sqlMapClient"/>
|
||||
</bean>
|
||||
|
||||
</beans></programlisting>
|
||||
</section>
|
||||
</section>
|
||||
</chapter>
|
||||
|
||||
Reference in New Issue
Block a user