Mybatis design

Execution process of Mybatis: Execution process of Mybatis_Mona Lisa's Java Blog-CSDN Blog_mybatis Execution Plan

1. create 

Build the SqlSessionFactory

public static SqlSessionManager newInstance(Reader reader, String environment) {

    //load configuration build object 

    return new SqlSessionManager(new SqlSessionFactoryBuilder().build(reader, environment, null));

  }

  private SqlSessionManager(SqlSessionFactory sqlSessionFactory) {

 When constructing the sqlsessionManager, call the private constructor to create the sqlsession proxy
    this.sqlSessionFactory = sqlSessionFactory;
    this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(
        SqlSessionFactory.class.getClassLoader(),
        new Class[]{SqlSession.class},
        new SqlSessionInterceptor());
  }

Reflection call SqlSession

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

      final SqlSession sqlSession = SqlSessionManager.this.localSqlSession.get();

      if (sqlSession != null) {

        try {

          return method.invoke(sqlSession, args);

        } catch (Throwable t) {

          throw ExceptionUtil.unwrapThrowable(t);

        }

      } else {

        final SqlSession autoSqlSession = openSession();

        try {

          final Object result = method.invoke(autoSqlSession, args);

          autoSqlSession.commit();

          return result;

//When executing the sqlsession method, dynamically execute the invoke call

Dynamic Sql execution process

Myabtis-dynamic sql analysis process_Fa Yi's blog's blog-CSDN blog_dynamic analysis sql

1. First, parse the dynamic sql fragment into sqlNode objects, and each sqlNode corresponds to different processing logic

2. Then perform parameter replacement according to the parameters passed in by the user to form the actual SQL statement

3. Finally, parse each sql fragment into an actual executable sql statement

Guess you like

Origin blog.csdn.net/qq_29857681/article/details/80776427