SPRING 整合IBATIS或者hibernate等持久数据层流程与实现
一、首先引用网上对spring整合的概述
Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。
与hibernate类似,Spring提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些概念都与hibernate类似。
通过SqlMapClientTemplate来操纵数据库的CRUD是没有问题的。
我们本地使用IBATIS 大概流程就是这样:
1、通过实例化sqlMapConfig,这个对象;
eg:
<bean id= "sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
<property name= "configLocations">
<list>
<value> classpath:sqlmap-config.xml</value >
<value> classpath:sqlmap-process-config.xml</value >
</list>
</property>
</bean>
2、实例化sqlmap-config.xml文件后,使用里面包含字节IBATIS的命名空间与SQL
<sqlMapConfig>
<settings cacheModelsEnabled= "true" enhancementEnabled ="true"
lazyLoadingEnabled="false" errorTracingEnabled= "true" maxRequests ="32"
maxSessions="10" maxTransactions= "5" useStatementNamespaces ="true" />
<sqlMap resource= "sqlmap/Source.xml"/>
</sqlMapConfig>
3、用户执行sql语句时,我们的dao会继承SqlMapClientTemplate,然
public class BaseDao extends SqlMapClientTemplate {
@Override
public T find(PK id) {
return (T) super.queryForObject(getNameSpace() + ".FIND-BY-ID", id);
}
@Overrpublic PK save(T t) { return (PK) super.insert(getNameSpace() + ".SAVE", t); } ......
.....
}以直接使用对应的SQL语句了;
二、SqlMapClientTemplate分析
1、打开SqlMapClientTemplate的源码:
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.ibatis;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import com.ibatis.common.util.PaginatedList;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ibatis.sqlmap.client.SqlMapSession;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.jdbc.support.JdbcAccessor;
import org.springframework.util.Assert;
/**
* Helper class that simplifies data access via the iBATIS
* {@link com.ibatis.sqlmap.client.SqlMapClient} API, converting checked
* SQLExceptions into unchecked DataAccessExceptions, following the
* <code>org.springframework.dao</code> exception hierarchy.
* Uses the same {@link org.springframework.jdbc.support.SQLExceptionTranslator}
* mechanism as {@link org.springframework.jdbc.core.JdbcTemplate}.
*
* <p>The main method of this class executes a callback that implements a
* data access action. Furthermore, this class provides numerous convenience
* methods that mirror {@link com.ibatis.sqlmap.client.SqlMapExecutor}'s
* execution methods.
*
* <p>It is generally recommended to use the convenience methods on this template
* for plain query/insert/update/delete operations. However, for more complex
* operations like batch updates, a custom SqlMapClientCallback must be implemented,
* usually as anonymous inner class. For example:
*
* <pre class="code">
* getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
* public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
* executor.startBatch();
* executor.update("insertSomething", "myParamValue");
* executor.update("insertSomethingElse", "myOtherParamValue");
* executor.executeBatch();
* return null;
* }
* });</pre>
*
* The template needs a SqlMapClient to work on, passed in via the "sqlMapClient"
* property. A Spring context typically uses a {@link SqlMapClientFactoryBean}
* to build the SqlMapClient. The template an additionally be configured with a
* DataSource for fetching Connections, although this is not necessary if a
* Data