src/main/java/javacommon/SqlSessionFactoryFactoryBean.java
(开头部分) 3KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
package javacommon;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.util.Assert;
public class SqlSessionFactoryFactoryBean implements FactoryBean,InitializingBean{
Logger logger = LoggerFactory.getLogger(SqlSessionFactoryFactoryBean.class);
private Resource configLocation;
private Resource[] mapperLocations;
private DataSource dataSource;
private boolean useTransactionAwareDataSource = true;
SqlSessionFactory sqlSessionFactory;
public void afterPropertiesSet() throws Exception {
Assert.notNull(configLocation,"configLocation must be not null");
sqlSessionFactory = createSqlSessionFactory();
}
private SqlSessionFactory createSqlSessionFactory() throws IOException {
Reader reader = new InputStreamReader(getConfigLocation().getInputStream());
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
Configuration conf = sqlSessionFactory.getConfiguration();
if(dataSource != null) {
DataSource dataSourceToUse = this.dataSource;
if (this.useTransactionAwareDataSource && !(this.dataSource instanceof TransactionAwareDataSourceProxy)) {
dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource);
}
conf.setEnvironment(new Environment("development",new ManagedTransactionFactory(),dataSourceToUse));
sqlSessionFactory = new SqlSessionFactoryBuilder().build(conf);
}
if(mapperLocations != null) {
Map<String, XNode> sqlFragments = new HashMap<String, XNode>();
for(Resource r : mapperLocations) {
logger.info("Loading iBatis3 mapper xml from file["+r.getFile().getAbsolutePath()+"]");
Reader mapperReader = new InputStreamReader(r.getInputStream());
后面还有 47 行代码,购买后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
