美文网首页
Mybatis 执行顺序

Mybatis 执行顺序

作者: 超_1f3a | 来源:发表于2020-02-18 11:35 被阅读0次

    ListappMuserident = appMuseridentService.getAppMuserident(startDate, endDate, IOS_LIVE_VIDEO_CONTROLLER, null);

    //MapperFactoryBean

    public T getObject() throws Exception {

        return this.sqlSession.getMapper(this.mapperInterface);

    }

    //SqlSessionFactoryBean 建造者模式

    public void afterPropertiesSet() throws Exception {

        Assert.notNull(this.dataSource, "Property 'dataSource' is required");

        Assert.notNull(this.sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");

        Assert.notNull(this.transactionFactoryClass, "Property 'transactionFactoryClass' is required");

        this.sqlSessionFactory = this.buildSqlSessionFactory();

    }

    public SqlSessionFactory getObject() throws Exception {

        if (this.sqlSessionFactory == null) {

            this.afterPropertiesSet();

        }

        return this.sqlSessionFactory;

    }

    //SqlSessionFactoryBuilder

    public SqlSessionFactory build(Configuration config) {

      return new DefaultSqlSessionFactory(config);

    }

    发现,其实调用DefaultSqlSesion的getMapper

    //DefaultSqlSession

    public <T> T getMapper(Class<T> type) {

      return configuration.getMapper(type, this);

    }

    //Configuration

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

      return mapperRegistry.getMapper(type, sqlSession);

    }

    //MapperRegistry

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

      if (!knownMappers.contains(type))

        throw new BindingException("Type " + type + " is not known to the MapperRegistry.");

      try {

        return MapperProxy.newMapperProxy(type, sqlSession);

      } catch (Exception e) {

        throw new BindingException("Error getting mapper instance. Cause: " + e, e);

      }

    }

    //MapperProxy 动态代理

    public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {

        ClassLoader classLoader = mapperInterface.getClassLoader();

        Class<?>[] interfaces = new Class[]{mapperInterface};

        MapperProxy proxy = new MapperProxy(sqlSession);

        return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);

      }

    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

      if (!OBJECT_METHODS.contains(method.getName())) {

        final Class<?> declaringInterface = findDeclaringInterface(proxy, method);

        final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);

        final Object result = mapperMethod.execute(args);

        if (result == null && method.getReturnType().isPrimitive()) {

          throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");

        }

        return result;

      }

      return null;

    }

    查看invoke

    //MapperMethod

    public Object execute(Object[] args) {

      Object result;

      if (SqlCommandType.INSERT == type) {

        Object param = getParam(args);

        result = sqlSession.insert(commandName, param);

      } else if (SqlCommandType.UPDATE == type) {

        Object param = getParam(args);

        result = sqlSession.update(commandName, param);

      } else if (SqlCommandType.DELETE == type) {

        Object param = getParam(args);

        result = sqlSession.delete(commandName, param);

      } else if (SqlCommandType.SELECT == type) {

        if (returnsList) {

          result = executeForList(args);

        } else if (returnsMap) {

          result = executeForMap(args);

        } else {

          Object param = getParam(args);

          result = sqlSession.selectOne(commandName, param);

        }

      } else {

        throw new BindingException("Unkown execution method for: " + commandName);

      }

      return result;

    }

    private List executeForList(Object[] args) {

      List result;

      Object param = getParam(args);

      if (rowBoundsIndex != null) {

        RowBounds rowBounds = (RowBounds) args[rowBoundsIndex];

        result = sqlSession.selectList(commandName, param, rowBounds);

      } else {

        result = sqlSession.selectList(commandName, param);

      }

      return result;

    }

    private Map executeForMap(Object[] args) {

      Map result;

      Object param = getParam(args);

      if (rowBoundsIndex != null) {

        RowBounds rowBounds = (RowBounds) args[rowBoundsIndex];

        result = sqlSession.selectMap(commandName, param, mapKey, rowBounds);

      } else {

        result = sqlSession.selectMap(commandName, param, mapKey);

      }

      return result;

    }

    private Object getParam(Object[] args) {

      final int paramCount = paramPositions.size();

      if (args == null || paramCount == 0) {

        return null;

      } else if (!hasNamedParameters && paramCount == 1) {

        return args[paramPositions.get(0)];

      } else {

        Map<String, Object> param = new HashMap<String, Object>();

        for (int i = 0; i < paramCount; i++) {

          param.put(paramNames.get(i), args[paramPositions.get(i)]);

        }

        return param;

      }

    }

    最后发现执行还是SqlSession 里的方法

    //DefaultSqlSession

    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();

      }

    }

    发现执行的是Executor中的方法

    //SimpleExecutor

    public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {

      Statement stmt = null;

      try {

        Configuration configuration = ms.getConfiguration();

        StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, rowBounds, resultHandler);

        stmt = prepareStatement(handler);

        return handler.query(stmt, resultHandler);

      } finally {

        closeStatement(stmt);

      }

    }

    //SimpleStatementHandlerpublic List query(Statement statement, ResultHandler resultHandler)    throws SQLException {  String sql = boundSql.getSql();  statement.execute(sql);  return resultSetHandler.handleResultSets(statement);}

    相关文章

      网友评论

          本文标题:Mybatis 执行顺序

          本文链接:https://www.haomeiwen.com/subject/quqgfhtx.html