设为首页 加入收藏

TOP

深入浅出MyBatis-Sqlsession(一)
2015-07-24 10:48:21 来源: 作者: 【 】 浏览:1
Tags:深入浅出 MyBatis-Sqlsession
前面的章节主要讲mybatis如何解析配置文件,这些都是一次性的过程。从本章开始讲解动态的过程,它们跟应用程序对mybatis的调用密切相关。本章先从sqlsession开始。

创建

正如其名,Sqlsession对应着一次 数据库会话。由于数据库回话不是永久的,因此Sqlsession的生命周期也不应该是永久的,相反,在你每次访问数据库时都需要创建它(当然并不是说在Sqlsession里只能执行一次sql,你可以执行多次,当一旦关闭了Sqlsession就需要重新创建它)。创建Sqlsession的地方只有一个,那就是SqlsessionFactory的openSession方法:
[java] view plaincopy
public SqlSessionopenSession() { returnopenSessionFromDataSource(configuration.getDefaultExecutorType(),null, false);
} [java] view plaincopy
private SqlSessionopenSessionFromDataSource(ExecutorType execType, TransactionIsolationLevellevel, boolean autoCommit) {
Connectionconnection = null;
try {
finalEnvironment environment = configuration.getEnvironment();
final DataSourcedataSource = getDataSourceFromEnvironment(environment);
TransactionFactory transactionFactory =getTransactionFactoryFromEnvironment(environment);
connection = dataSource.getConnection();
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
connection = wrapConnection(connection);
Transaction tx = transactionFactory.newTransaction(connection,autoCommit);
Executorexecutor = configuration.newExecutor(tx, execType);
returnnewDefaultSqlSession(configuration, executor, autoCommit);
} catch (Exceptione) {
closeConnection(connection);
throwExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
} [java] view plaincopy
public ExecutornewExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType :executorType;
executorType = executorType == null ?ExecutorType.SIMPLE : executorType;
Executor executor;
if(ExecutorType.BATCH == executorType) { executor = new BatchExecutor(this,transaction);
} elseif(ExecutorType.REUSE == executorType) { executor = new ReuseExecutor(this,transaction);
} else { executor = newSimpleExecutor(this, transaction);
}
if (cacheEnabled) { executor = new CachingExecutor(executor);
} executor =(Executor) interceptorChain.pluginAll(executor);
return executor; }

[java] view plaincopy
session = sqlSessionFactory.openSession(); UserDao userDao= session.getMapper(UserDao.class);
UserDto user =new UserDto(); user.setUsername("iMbatis");
user.setPassword("iMbatis"); userDao.insertUser(user); [java] view plaincopy
public T getMapper(Class type, SqlSession sqlSession) { if (!knownMappers.contains(type))
thrownewBindingException("Type " + type + " isnot known to the MapperRegistry."); try {
returnMapperProxy.newMapperProxy(type, sqlSession); } catch (Exceptione) {
thrownewBindingException("Error getting mapper instance. Cause: " + e, e); }
} [java] view plaincopy
publicstatic T newMapperProxy(Class mapperInterface, SqlSession sqlSession) { ClassLoaderclassLoader = mapperInterface.getClassLoader();
Class[] interfaces = new Class[]{mapperInterface}; MapperProxyproxy = new MapperProxy(sqlSession);
return (T) Proxy.newProxyInstance(classLoader,interfaces, proxy); } [java] view plaincopy
public Objectinvoke(Object proxy, Method method, Object[] args) throws Throwable{ if (method.getDeclaringClass()== Object.class) {
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇oraInst.loc文件在不同os下有不同.. 下一篇Addnode.sh失败,日志文件报错PRKC..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)
·透彻理解 C 语言指针 (2025-12-26 00:22:52)
·C语言指针详解 (经典 (2025-12-26 00:22:49)
·C 指针 | 菜鸟教程 (2025-12-26 00:22:46)