Mybatis 设计

Mybatis的执行流程:Mybatis的执行流程_蒙娜丽莎的Java的博客-CSDN博客_mybatis执行计划

1. 创建 

构建SqlSessionFactory

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

    //加载配置  构建对象 

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

  }

  private SqlSessionManager(SqlSessionFactory sqlSessionFactory) {

 当构建sqlsessionManager时 调用私有构造 创建sqlsession代理
    this.sqlSessionFactory = sqlSessionFactory;
    this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(
        SqlSessionFactory.class.getClassLoader(),
        new Class[]{SqlSession.class},
        new SqlSessionInterceptor());
  }

反射调用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;

//当执行sqlsession方法时 动态执行invoke 调用

动态Sql执行流程

Myabtis-动态sql解析流程_法毅的博客的博客-CSDN博客_动态解析sql

1. 首先根据动态sql片段解析为sqlNode对象,每个sqlNode对应不同的处理逻辑

2. 然后根据用户传入的参数进行参数替换,形成实际的sql语句

3. 最后把每个sql片段解析为实际可执行的sql语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值