ibatis3 结合spring 扩展的 SqlSessionFactoryBean
有空补上注释
package com.starit.core.ibatis; import java.io.IOException; import java.io.Reader; import javax.sql.DataSource; import org.apache.ibatis.builder.xml.XMLConfigBuilder; import org.apache.ibatis.io.Resources; import org.apache.ibatis.mapping.Configuration; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory; import org.apache.ibatis.transaction.managed.ManagedTransactionFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; public class SqlSessionFactoryBean implements FactoryBean, InitializingBean { private String configLocation; private DataSource dataSource; private SqlSessionFactory sqlSessionFactory; private boolean useTransactionAwareDataSource = true; private String environmentId = "development"; public void setEnvironmentId(String environmentId) { this.environmentId = environmentId; } public void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource) { this.useTransactionAwareDataSource = useTransactionAwareDataSource; } public Object getObject() throws Exception { return this.sqlSessionFactory; } public Class getObjectType() { return (this.sqlSessionFactory != null ? this.sqlSessionFactory.getClass() : SqlSessionFactory.class); } public boolean isSingleton() { return true; } public void afterPropertiesSet() throws Exception { this.sqlSessionFactory = this.buildSqlSessionFactory(configLocation); } protected SqlSessionFactory buildSqlSessionFactory(String configLocation) throws IOException { if (configLocation == null) { throw new IllegalArgumentException("configLocation entry is required"); } DataSource dataSourceToUse = this.dataSource; if (this.useTransactionAwareDataSource && !(this.dataSource instanceof TransactionAwareDataSourceProxy)) { dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource); } Environment environment = new Environment(environmentId, new ManagedTransactionFactory(), dataSourceToUse); Reader reader = Resources.getResourceAsReader(configLocation); XMLConfigBuilder parser = new XMLConfigBuilder(reader, null, null); Configuration config = parser.parse(); config.setEnvironment(environment); return new DefaultSqlSessionFactory(config); } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void setConfigLocation(String configLocation) { this.configLocation = configLocation; } }
相关推荐
Dullonjiang 2020-01-23
与卿画眉共浮生 2011-06-25
yierxiansheng 2014-06-18
登峰小蚁 2019-12-24
plane 2017-10-19
踩风火轮的乌龟 2019-10-23
pandapanda 2014-05-08
阿泰 2014-07-02
shouen 2016-04-26
whileinsist 2012-04-07
spprogrammer 2018-01-25
Coohx 2017-12-05
AndroidOliver 2012-05-14
BruceWayne 2014-12-02
liubang000 2014-05-30
KOJ 2013-11-26
CXC0 2013-08-28