面试被问Mybatis底层实现:你连这个知识点都说不明白?

编程入门 行业动态 更新时间:2024-10-18 01:27:06

面试被问Mybatis底层实现:你连这个知识点都说<a href=https://www.elefans.com/category/jswz/34/1765475.html style=不明白?"/>

面试被问Mybatis底层实现:你连这个知识点都说不明白?

面试真的是越来越难了,一直关注我的朋友应该清楚,小编有一个习惯,喜欢每年都去面试一下,试试水也看看自己的缺陷,这不,前几天有一个机会,让小编成功的被刺激了一耙(虽然知道这样不是很好,但是还是想操作一下,给各位hr道歉)

事情是这样的,小编的简历一直挂在网上,然后有人给我打电话,说有没有时间去面试一下,刚过完双十一,闲着也是闲着,我就应下来去了,然后前面谈的都不错,直到要结束最后一下面试,马上就是hr面了,面试官脑回路不知道怎么回事,就问了我一个意想不到的问题:看你简历写的精通mybatis源码,那你能不能给我讲一下sqlsession啊,WTF?我就反问一句,这有点多啊,那我主要说那一块呢?

1、SqlSession 操作模板实现类==SqlSessionTemplate

2、用于保存当前 SqlSession 对象==SqlSessionInterceptor

3、SqlSession 的事务同步器==SqlSessionSynchronization

幸好我之前看过这一块,然后磕磕绊绊的讲完了,面试官眼神复杂的看了我一下,说可以了,先这样吧。。。。

公众号:Java架构师联盟,每日更更新技术好文

偶,你NNGT,我这美好的一天,就这么没了,面试完了也没啥心思去吃好吃的,回到家就开始冲浪,将这几个知识点进行整理

其实官网上,sqlsession相关得知识还是很多的,但是时间问题,就针对源码整理被问到的这几个知识点,大家有什么其他想看的可以评论区告诉我,我会抽时间进行整理,好了,话不多说,看重点

SqlSessionTemplate

org.mybatis.spring.SqlSessionTemplate:实现 SqlSession 和 DisposableBean 接口,SqlSession 操作模板实现类

实际上,代码实现和 org.apache.ibatis.session.SqlSessionManager 相似,承担 SqlSessionFactory 和 SqlSession 的职责

构造方法

public class SqlSessionTemplate implements SqlSession, DisposableBean {/*** a factory of SqlSession*/private final SqlSessionFactory sqlSessionFactory;/*** {@link Configuration} 中默认的 Executor 执行器类型,默认 SIMPLE*/private final ExecutorType executorType;/*** SqlSessionInterceptor 代理对象*/private final SqlSession sqlSessionProxy;/*** 异常转换器,MyBatisExceptionTranslator 对象*/private final PersistenceExceptionTranslator exceptionTranslator;public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType());}public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {this(sqlSessionFactory, executorType,new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true));}public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,PersistenceExceptionTranslator exceptionTranslator) {notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");notNull(executorType, "Property 'executorType' is required");this.sqlSessionFactory = sqlSessionFactory;this.executorType = executorType;this.exceptionTranslator = exceptionTranslator;// 创建一个 SqlSession 的动态代理对象,代理类为 SqlSessionInterceptorthis.sqlSessionProxy = (SqlSession) newProxyInstance(SqlSessionFactory.class.getClassLoader(),new Class[] { SqlSession.class }, new SqlSessionInterceptor());}
}
  • sqlSessionFactory:用于创建 SqlSession 对象
  • executorType:执行器类型,创建 SqlSession 对象是根据它创建对应的 Executor 执行器,默认为
  • sqlSessionProxy:SqlSession 的动态代理对象,代理类为 SqlSessionInterceptor
  • exceptionTranslator:异常转换器

在调用SqlSessionTemplate中的SqlSession相关方法时,内部都是直接调用sqlSessionProxy动态代理对象的方法,我们来看看是如何处理的

SqlSessionInterceptor

SqlSessionTemplate的内部类,实现了 InvocationHandler 接口,作为sqlSessionProxy动态代理对象的代理类,对 SqlSession 的相关方法进行增强

代码如下:

private class SqlSessionInterceptor implements InvocationHandler {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// <1> 获取一个 SqlSession 对象SqlSession sqlSession = getSqlSession(SqlSessionTemplate.this.sqlSessionFactory,SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator);try {// 执行 SqlSession 的方法Object re

更多推荐

面试被问Mybatis底层实现:你连这个知识点都说不明白?

本文发布于:2024-03-14 11:30:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1736369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不明白   都说   知识点   底层   你连

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!