mybatis sql执行流程【简化】
创始人
2024-06-03 11:44:19

一、添加一个mapper.xml




二、再mybatis-config.xml中引入mapper

    

扩展:mybatis加载mapper的方式有四种:

package -> resource -> url -> class

三、通过sqlSessionFactory 操作查询:

        String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);Object selectOne = sqlSessionFactory.openSession().selectOne("com.example.demo23.mapper.TestMapper.selectBlog",1L);
 
四、执行sql:源码
Object selectOne = sqlSessionFactory.openSession().selectOne("com.example.demo23.mapper.TestMapper.selectBlog",1L);

进入selectOne方法

  public  T selectOne(String statement, Object parameter) {// Popular vote was to return null on 0 results and throw exception on too many.List list = this.selectList(statement, parameter);if (list.size() == 1) {return list.get(0);}if (list.size() > 1) {throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());} else {return null;}}

断点至:

private  List selectList(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {try {//从configuration中获取mapperStatement数据MappedStatement ms = configuration.getMappedStatement(statement);dirty |= ms.isDirtySelect();return executor.query(ms, wrapCollection(parameter), rowBounds, handler);} catch (Exception e) {throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);} finally {ErrorContext.instance().reset();}
}

执行表达式: configuration.getMappedStatement(statement)

,可以看到对应的statement保存的信息。 

 拿到ms对象后,交给:

executor.query(ms, wrapCollection(parameter), rowBounds, handler)

拓展:mybatid  executor有几种执行器?通过接口类图可以看见有三中执行器 

断点进入

public  List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler)throws SQLException {//从MappedStatement  获取当前绑定的sqlBoundSql boundSql = ms.getBoundSql(parameterObject);CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

 拿到bondsql后继续执行:

  public  List query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler,CacheKey key, BoundSql boundSql) throws SQLException {
//获取缓存Cache cache = ms.getCache();
//判断是否有缓存if (cache != null) {flushCacheIfRequired(ms);if (ms.isUseCache() && resultHandler == null) {ensureNoOutParams(ms, boundSql);@SuppressWarnings("unchecked")List list = (List) tcm.getObject(cache, key);if (list == null) {list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);tcm.putObject(cache, key, list); // issue #578 and #116}return list;}}return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);}

执行doQuery

public  List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,BoundSql boundSql) throws SQLException {Statement stmt = null;try {Configuration configuration = ms.getConfiguration();StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler,boundSql);stmt = prepareStatement(handler, ms.getStatementLog());return handler.query(stmt, resultHandler);} finally {closeStatement(stmt);}
}

查询数据

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;
}

执行完毕。

相关内容

热门资讯

尚纬股份跌0.45%,成交额2... 1月12日,尚纬股份跌0.45%,成交额2.68亿元,换手率4.88%,总市值55.19亿元。异动分...
光洋股份涨2.01%,成交额3... 1月12日,光洋股份涨2.01%,成交额3.52亿元,换手率5.04%,总市值77.18亿元。异动分...
恒华科技涨2.97%,成交额1... 1月12日,恒华科技涨2.97%,成交额1.92亿元,换手率5.47%,总市值41.57亿元。异动分...
鲍斯股份涨0.38%,成交额1... 1月12日,鲍斯股份涨0.38%,成交额1.30亿元,换手率2.66%,总市值50.52亿元。异动分...
江化微涨停,成交额10.02亿... 1月12日,江化微涨停,成交额10.02亿元,换手率12.40%,总市值82.60亿元。异动分析光刻...