标签:int 程序 cti for 方法 etc 个数 cto eset
1. 在创建好sqlSessionFactory之后,接着就要配置sqlSession的创建。
<bean id="simpleTempalte" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> <constructor-arg index="1" value="SIMPLE" /> </bean>
2. sqlSession接口
simpleTempalte.delete(Statement.getStatement(CxCaseMapper.class, "deleteById"), id);
public class SqlSessionTemplate implements SqlSession { //session 工场的引用 private final SqlSessionFactory sqlSessionFactory; // 对于数据库的操作类型 private final ExecutorType executorType; //sqlSession代理 private final SqlSession sqlSessionProxy; private final PersistenceExceptionTranslator exceptionTranslator; }
@Override public int delete(String statement) { return this.sqlSessionProxy.delete(statement); }
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator;
//构造代理对象 this.sqlSessionProxy = (SqlSession) newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class[] { SqlSession.class }, new SqlSessionInterceptor()); } private class SqlSessionInterceptor implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//这里获取的sqlSession 其实是DefaultSqlSession SqlSession sqlSession = getSqlSession( SqlSessionTemplate.this.sqlSessionFactory, SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator); try {
// 代理对象对于目标对象的调用,其实是defaultSqlSession 对于目标方法的调用 Object result = method.invoke(sqlSession, args); if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) { // force commit even on non-dirty sessions because some databases require // a commit/rollback before calling close() sqlSession.commit(true); } return result; } catch (Throwable t) { Throwable unwrapped = unwrapThrowable(t); if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) { // release the connection to avoid a deadlock if the translator is no loaded. See issue #22 closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory); sqlSession = null; Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped); if (translated != null) { unwrapped = translated; } } throw unwrapped; } finally { if (sqlSession != null) { closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory); } } } }
public class DefaultSqlSession implements SqlSession { private Configuration configuration; private Executor executor; private boolean autoCommit; private boolean dirty; @Override public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { try { MappedStatement ms = configuration.getMappedStatement(statement); executor.query(ms, wrapCollection(parameter), rowBounds, handler); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } } }
mybatis源码分析(2)-----SqlSession创建
标签:int 程序 cti for 方法 etc 个数 cto eset
原文地址:http://www.cnblogs.com/chihirotan/p/6591722.html