/*
* Copyright 2002-2008 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.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.util.ObjectUtils;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser;
import com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser_EZ;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.transaction.TransactionConfig;
import com.ibatis.sqlmap.engine.transaction.TransactionManager;
import com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig;
/**
* {@link org.springframework.beans.factory.FactoryBean} that creates an
* iBATIS {@link com.ibatis.sqlmap.client.SqlMapClient}. This is the usual
* way to set up a shared iBATIS SqlMapClient in a Spring application context;
* the SqlMapClient can then be passed to iBATIS-based DAOs via dependency
* injection.
*
* <p>Either {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
* or {@link org.springframework.transaction.jta.JtaTransactionManager} can be
* used for transaction demarcation in combination with a SqlMapClient,
* with JTA only necessary for transactions which span multiple databases.
*
* <p>Allows for specifying a DataSource at the SqlMapClient level. This
* is preferable to per-DAO DataSource references, as it allows for lazy
* loading and avoids repeated DataSource references in every DAO.
*
* <p><b>Note:</b> As of Spring 2.5.5, this class (finally) requires iBATIS 2.3
* or higher. The new "mappingLocations" feature requires iBATIS 2.3.2.
*
* @author Juergen Hoeller
* @since 24.02.2004
* @see #setConfigLocation
* @see #setDataSource
* @see SqlMapClientTemplate#setSqlMapClient
* @see SqlMapClientTemplate#setDataSource
*/
public class SqlMapClientFactoryBean implements FactoryBean, InitializingBean {
private static final ThreadLocal configTimeLobHandlerHolder = new ThreadLocal();
/**
* Return the LobHandler for the currently configured iBATIS SqlMapClient,
* to be used by TypeHandler implementations like ClobStringTypeHandler.
* <p>This instance will be set before initialization of the corresponding
* SqlMapClient, and reset immediately afterwards. It is thus only available
* during configuration.
* @see #setLobHandler
* @see org.springframework.orm.ibatis.support.ClobStringTypeHandler
* @see org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler
* @see org.springframework.orm.ibatis.support.BlobSerializableTypeHandler
*/
public static LobHandler getConfigTimeLobHandler() {
return (LobHandler) configTimeLobHandlerHolder.get();
}
private Resource[] configLocations;
private Resource[] mappingLocations;
private Properties sqlMapClientProperties;
private DataSource dataSource;
private boolean useTransactionAwareDataSource = true;
private Class transactionConfigClass = ExternalTransactionConfig.class;
private Properties transactionConfigProperties;
private LobHandler lobHandler;
private SqlMapClient sqlMapClient;
public SqlMapClientFactoryBean() {
this.transactionConfigProperties = new Properties();
this.transactionConfigProperties.setProperty("SetAutoCommitAllowed", "false");
}
/**
* Set the location of the iBATIS SqlMapClient config file.
* A typical value is "WEB-INF/sql-map-config.xml".
* @see #setConfigLocations
*/
public void setConfigLocation(Resource configLocation) {
this.configLocations = (configLocation != null ? new Resource[] {configLocation} : null);
}
/**
* Set multiple locations of iBATIS SqlMapClient config files that
* are going to be merged into one unified configuration at runtime.
*/
public void setConfigLocations(Resource[] configLocations) {
this.configLocations = configLocations;
}
/**
* Set locations of iBATIS sql-map mapping files that are going to be
* merged into the SqlMapClient configuration at runtime.
* <p>This is an alternative to specifying "<sqlMap>" entries
* in a sql-map-client config file. This property being based on Spring's
* resource abstraction also allows for specifying resource patterns here:
* e.g. "/myApp/*-map.xml".
* <p>Note that this feature requires iBATIS 2.3.2; it will not work
* with any previous iBATIS version.
*/
public void setMappingLocations(Resource[] mappingLocations) {
this.mappingLocations = mappingLocations;
}
/**
* Set optional properties to be passed into the SqlMapClientBuilder, as
* alternative to a <code><properties></code> tag in the sql-map-config.xml
* file. Will be used to resolve placeholders in the config file.
* @see #setConfigLocation
* @see com.ibatis.sqlmap.client.SqlMapClientBuilder#buildSqlMapClient(java.io.InputStream, java.util.Properties)
*/
public void setSqlMapClientProperties(Properties sqlMapClientProperties) {
this.sqlMapClientProperties = sqlMapClientProperties;
}
/**
* Set the DataSource to be used by iBATIS SQL Maps. This will be passed to the
* SqlMapClient as part of a TransactionConfig instance.
* <p>If specified, this will override corresponding settings in the SqlMapClient
* properties. Usually, you will specify DataSource and transaction configuration
* <i>either</i> here <i>or</i> in SqlMapClient properties.
* <p>Specifying a DataSource for the SqlMapClient rather than for each individual
* DAO allows for lazy loading, for example when using PaginatedList results.
* <p>With a DataSource passed in here, you don't need to specify one for each DAO.
* Passing the SqlMapClient to the DAOs is enough, as it already carries a DataSource.
* Thus, it's recommended to specify the DataSource at this central location only.
* <p>Thanks to Brandon Goodin from the iBATIS team for the hint on how to make
* this work with Spring's integration strategy!
* @see #setTransactionConfigClass
* @see #setTransactionConfigProperties
* @see com.ibatis.sqlmap.client.SqlMapClient#getDataSource
* @see SqlMapClientTemplate#setDataSource
* @see SqlMapClientTemplate#queryForPaginatedList
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* Set whether to use a transaction-aware DataSource for the SqlMapClient,
* i.e. whether to automatically wrap the passed-in DataSource with Spring's
* TransactionAwareDataSourceProxy.
* <p>Default is "true": When the SqlMapClient performs direct database operations
* outside of Spring's SqlMapClientTemplate (for example, lazy loading or direct
* SqlMapClient access), it will still participate in active Spring-managed
* transactions.
* <p>As a further effect, using a transaction-aware DataSource will apply
* remaining transaction timeouts to all created JDBC Statements. This means
* that all operations performed by the SqlMapClient will automatically
* participate in Spring-managed transaction timeouts.
* <p>Turn this flag
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
IbatisStudy.rar (39个子文件)
.project 387B
bin
ibatis_sqlMap
campaign
sqlmap_campaign.xml 1KB
org
springframework
orm
ibatis
SqlMapClientFactoryBean$SqlMapParserFactory.class 509B
SqlMapClientFactoryBean.class 7KB
com
esoon
study
ibatis
dao
CampaignDao.class 443B
testCampaignDao.class 4KB
ICampaignDao.class 2KB
domain
OCSCampaign.class 651B
Campaign.class 926B
service
ICampaignService.class 1KB
testCampaignService.class 4KB
CampaignService.class 455B
util
HsqldbUtil.class 2KB
ibatis
sqlmap
engine
builder
xml
SqlMapConfigParser_EZ.class 5KB
SqlMapConfigParser_EZ$ExtensionFileFilter.class 1KB
sql-map-config-2.dtd 2KB
SqlMapConfigParser_EZ$1.class 3KB
sql-map-2.dtd 14KB
ibatis-sql-map-config.xml 226B
config.xml 2KB
sql-map-config-2.dtd 2KB
test
com
esoon
study
ibatis
dao
testCampaignDao.java 3KB
service
testCampaignService.java 3KB
src
ibatis_sqlMap
campaign
sqlmap_campaign.xml 1KB
org
springframework
orm
ibatis
SqlMapClientFactoryBean.java 18KB
com
esoon
study
ibatis
dao
CampaignDao.java 416B
ICampaignDao.java 948B
domain
OCSCampaign.java 273B
Campaign.java 492B
service
ICampaignService.java 840B
CampaignService.java 418B
util
HsqldbUtil.java 2KB
ibatis
sqlmap
engine
builder
xml
sql-map-config-2.dtd 2KB
SqlMapConfigParser_EZ.java 5KB
sql-map-2.dtd 14KB
ibatis-sql-map-config.xml 226B
config.xml 2KB
sql-map-config-2.dtd 2KB
.classpath 1015B
lib
共 39 条
- 1
资源评论
- sinat_202474832015-01-07挺实用的,还不错
- 石凯2014-01-14很好,可以运行。
- luyingjie05702014-06-20不错,有帮助
gammax
- 粉丝: 1
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功