工厂接口(sqlSessionFactory)如何创建sqlsession会话接口.
public static SqlSession openSession() {
return getSqlSessionFactory().openSession();
}
openSession() 方法调用package org.apache.ibatis.session.defaults ,DefaultSqlSessionFactory类的openSession方法。
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
调用同一个类下的openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);方法。从这我们就可以猜到这里设定了Session的SQL执行器的类型、事务类型和是否自动提交。而这些设置信息都是由配置文件configuration决定的。这样我们就知道了配置信息是如何决定了sqlSession的功能。
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
*return new DefaultSqlSession(configuration, executor, autoCommit);*
} catch (Exception e) {
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
返回了DefaultSqlSession(configuration,executor,autoCommit)的 实例,这是DefaultSqlSession类的构造方法。
补充:package org.apache.ibatis.session;下ExecutorType.class源码。
public enum ExecutorType {
SIMPLE, REUSE, BATCH
}
package org.apache.ibatis.session; 下的Configuration.class类,设置defaultExecutorType的值是“SIMPLE”
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
我们已经拿到了一个SqlSession的实例,也就是说我们已经明白了1.的执行步骤,现在我们来看2.3.的执行过程。
1. SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
2. StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
3. return studentMapper.findAllStudents();
sqlsession.getMapper(StudentMapper.class)方法,调用了package org.apache.ibatis.session.defaults;下的DefaultSqlSession的getMapper(xxx.class)方法。源码如下:
@Override
public <T> T getMapper(Class<T> type) {
return configuration.<T>getMapper(type, this);
}
调用了configuration实例的
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
调用了package org.apache.ibatis.binding;下的mapperRegistry实例的:
@SuppressWarnings("unchecked")
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
通过knownMappers获取一个MapperProxyFactory。knowMappers代码如下:
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
然后newInstance,调用package org.apache.ibatis.binding;下的MapperProxyFactory 类的newInstance(SqlSession sqlSession)方法。再调用此类下的newInstance(MapperProxy mapperProxy)方法,通过动态代理,我们就可以使用接口了。 (这个我也不懂。。。),
private final Class<T> mapperInterface;
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
得到了mapper实例。