本篇是mybatis执行sql的第四步,利用mapper执行sql
动态代理实际会调用MapperProxy的invoke方法,MapperProxy实现了InvocationHandler接口,走JDK的动态代理
动态代理调用MapperProxy的invoke方法//org.apache.ibatis.binding.MapperProxy#invoke public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { //这里判断是否是Object的方法,这些方法不需要处理 if (Object.class.equals(method.getDeclaringClass())) { return method.invoke(this, args); } else if (isDefaultMethod(method)) { //需要处理的接口方法 return cachedInvoker(method).invoke(proxy, method, args, sqlSession); } } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } }获取MapperMethodInvoker
//org.apache.ibatis.binding.MapperProxy#cachedInvoker private MapperMethodInvoker cachedInvoker(Method method) throws Throwable { try { return methodCache.computeIfAbsent(method, m -> { if (m.isDefault()) { // 接口的默认方法(Java8) try { if (privateLookupInMethod == null) { //走jdk8的创建方法 return new DefaultMethodInvoker(getMethodHandleJava8(method)); } else { //走jdk9的创建方法 return new DefaultMethodInvoker(getMethodHandleJava9(method)); } } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { throw new RuntimeException(e); } } else { // 创建了一个 MapperMethod return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration())); } }); } catch (RuntimeException re) { Throwable cause = re.getCause(); throw cause == null ? re : cause; } } //org.apache.ibatis.binding.MapperProxy.PlainMethodInvoker#PlainMethodInvoker //private final MapperMethod mapperMethod; public PlainMethodInvoker(MapperMethod mapperMethod) { super(); this.mapperMethod = mapperMethod; }反射,执行sql
//org.apache.ibatis.binding.MapperProxy.PlainMethodInvoker#invoke public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable { // 执行sql return mapperMethod.execute(sqlSession, args); }执行sql
public Object execute(SqlSession sqlSession, Object[] args) { Object result; switch (command.getType()) { case INSERT: { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.insert(command.getName(), param)); break; } case UPDATe: { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.update(command.getName(), param)); break; } case DELETE: { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.delete(command.getName(), param)); break; } case SELECT: if (method.returnsVoid() && method.hasResultHandler()) { executeWithResultHandler(sqlSession, args); result = null; } else if (method.returnsMany()) { result = executeForMany(sqlSession, args); } else if (method.returnsMap()) { result = executeForMap(sqlSession, args); } else if (method.returnsCursor()) { result = executeForCursor(sqlSession, args); } else { Object param = method.convertArgsToSqlCommandParam(args); // 单条查询 result = sqlSession.selectOne(command.getName(), param); if (method.returnsOptional() && (result == null || !method.getReturnType().equals(result.getClass()))) { result = Optional.ofNullable(result); } } break; case FLUSH: result = sqlSession.flushStatements(); break; default: throw new BindingException("Unknown execution method for: " + command.getName()); } if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) { throw new BindingException("Mapper method '" + command.getName() + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ")."); } return result; }多条查询
//org.apache.ibatis.binding.MapperMethod#executeForMany private多条查询,单条查询也是调用这个Object executeForMany(SqlSession sqlSession, Object[] args) { List result; Object param = method.convertArgsToSqlCommandParam(args); if (method.hasRowBounds()) { RowBounds rowBounds = method.extractRowBounds(args); result = sqlSession.selectList(command.getName(), param, rowBounds); } else { result = sqlSession.selectList(command.getName(), param); } // issue #510 Collections & arrays support if (!method.getReturnType().isAssignableFrom(result.getClass())) { if (method.getReturnType().isArray()) { return convertToArray(result); } else { return convertToDeclaredCollection(sqlSession.getConfiguration(), result); } } return result; }
//org.apache.ibatis.session.defaults.DefaultSqlSession#selectList(java.lang.String, java.lang.Object) public获取MappedStatementList selectList(String statement, Object parameter) { return this.selectList(statement, parameter, RowBounds.DEFAULT); } //org.apache.ibatis.session.defaults.DefaultSqlSession#selectList public List selectList(String statement, Object parameter, RowBounds rowBounds) { try { //获取sql封装类,封装了mapper中一个增删改查的配置块 MappedStatement ms = configuration.getMappedStatement(statement); // 如果 cacheEnabled = true(默认),Executor会被 CachingExecutor装饰 return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
//org.apache.ibatis.session.Configuration#getMappedStatement(java.lang.String) public MappedStatement getMappedStatement(String id) { return this.getMappedStatement(id, true); } //org.apache.ibatis.session.Configuration#getMappedStatement(java.lang.String, boolean) public MappedStatement getMappedStatement(String id, boolean validateIncompleteStatements) { if (validateIncompleteStatements) { buildAllStatements(); } return mappedStatements.get(id); }执行查询
//org.apache.ibatis.executor.baseExecutor#query publicSimple Excutor的查询List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException { BoundSql boundSql = ms.getBoundSql(parameter); // 缓存 CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql); return query(ms, parameter, rowBounds, resultHandler, key, boundSql); } //org.apache.ibatis.executor.baseExecutor#query public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId()); if (closed) { throw new ExecutorException("Executor was closed."); } if (queryStack == 0 && ms.isFlushCacheRequired()) { // flushCache="true"时,即使是查询,也清空一级缓存 clearLocalCache(); } List list; try { // 防止递归查询重复处理缓存 queryStack++; // 查询缓存 list = resultHandler == null ? (List ) localCache.getObject(key) : null; if (list != null) { handleLocallyCachedOutputParameters(ms, key, parameter, boundSql); } else { // 真正的查询 list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql); } } finally { queryStack--; } if (queryStack == 0) { for (DeferredLoad deferredLoad : deferredLoads) { deferredLoad.load(); } // issue #601 deferredLoads.clear(); if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) { // issue #482 clearLocalCache(); } } return list; } //org.apache.ibatis.executor.baseExecutor#queryFromDatabase private List queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { List list; // 设置缓存占位 localCache.putObject(key, EXECUTION_PLACEHOLDER); try { list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql); } finally { // 移除占位符 localCache.removeObject(key); } // 写入一级缓存 localCache.putObject(key, list); if (ms.getStatementType() == StatementType.CALLABLE) { localOutputParameterCache.putObject(key, parameter); } return list; }
//org.apache.ibatis.executor.SimpleExecutor#doQuery public创建StatementHandlerList doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Statement stmt = null; try { Configuration configuration = ms.getConfiguration(); //mybatis四大对象的StatementHandler 创建一个StatementHandler StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); // 获取一个 Statement对象 stmt = prepareStatement(handler, ms.getStatementLog()); // 执行查询 return handler.query(stmt, resultHandler); } finally { // 用完就关闭 closeStatement(stmt); } }
//org.apache.ibatis.session.Configuration#newStatementHandler public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql); // 作用于StatementHandler的插件 statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler); return statementHandler; } //RoutingStatementHandler的构造方法,生成对应的StatementHandler public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { // StatementType 增删改查标签中的 statementType="PREPARED",默认值 PREPARED switch (ms.getStatementType()) { case STATEMENT: delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); break; case PREPARED: delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); break; case CALLABLE://这个是存储过程 delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); break; default: throw new ExecutorException("Unknown statement type: " + ms.getStatementType()); } }获取Statement对象,这里已经是jdbc的东西了
//org.apache.ibatis.executor.SimpleExecutor#prepareStatement private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; //获取数据库连接 Connection connection = getConnection(statementLog); // 获取 Statement 对象 stmt = handler.prepare(connection, transaction.getTimeout()); // 为 Statement 设置参数 handler.parameterize(stmt); return stmt; }获取Statement对象
//org.apache.ibatis.executor.statement.baseStatementHandler#prepare public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException { ErrorContext.instance().sql(boundSql.getSql()); Statement statement = null; try { //获取statement statement = instantiateStatement(connection); //设置超时时间 setStatementTimeout(statement, transactionTimeout); //设置条数 setFetchSize(statement); return statement; } catch (SQLException e) { closeStatement(statement); throw e; } catch (Exception e) { closeStatement(statement); throw new ExecutorException("Error preparing statement. Cause: " + e, e); } } //获取Statement //org.apache.ibatis.executor.statement.PreparedStatementHandler#instantiateStatement protected Statement instantiateStatement(Connection connection) throws SQLException { //获取sql String sql = boundSql.getSql(); //设置PrepareStatement的参数 if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) { //设置了Jdbc3KeyGenerator主键生成器 String[] keyColumnNames = mappedStatement.getKeyColumns(); if (keyColumnNames == null) { return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); } else { return connection.prepareStatement(sql, keyColumnNames); } } else if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) { return connection.prepareStatement(sql); } else { return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } }给Statement设置参数
//org.apache.ibatis.executor.statement.PreparedStatementHandler#parameterize public void parameterize(Statement statement) throws SQLException { parameterHandler.setParameters((PreparedStatement) statement); } //循环给sql设置参数,循环获取sql的每个参数,获取对应的参数类型处理器,然后将参数设置到sql中 //org.apache.ibatis.scripting.defaults.DefaultParameterHandler#setParameters public void setParameters(PreparedStatement ps) { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List执行查询parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); //out是出参 if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params value = boundSql.getAdditionalParameter(propertyName); } else if (parameterObject == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { value = parameterObject; } else { metaObject metaObject = configuration.newmetaObject(parameterObject); value = metaObject.getValue(propertyName); } //mybatis的数据类型处理器,可以自定义 TypeHandler typeHandler = parameterMapping.getTypeHandler(); JdbcType jdbcType = parameterMapping.getJdbcType(); if (value == null && jdbcType == null) { jdbcType = configuration.getJdbcTypeForNull(); } try { typeHandler.setParameter(ps, i + 1, value, jdbcType); } catch (TypeException | SQLException e) { throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e); } } } } }
//org.apache.ibatis.executor.statement.PreparedStatementHandler#query public处理结果List query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; // JDBC执行sql ps.execute(); // 处理结果集 return resultSetHandler.handleResultSets(ps); }
//org.apache.ibatis.executor.resultset.DefaultResultSetHandler#handleResultSets public List
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)