《底层到底做了什么》--- mybatis-plus的一次select调用过程
使用mybatis-plus创建db mapper,只需要写一个接口继承BaseMapper,比如示例中的EntityMapper。
@Mapper
@Repository
public interface EntityMapper extends BaseMapper<Entity> {}
本文将解释在底层一次select调用是怎么完成。 主要包括以下几个部分:
- 核心类及包
- 容器初始化
- Mapper bean初始化
- 代理方法调用
核心类及所在包
主要涉及以下包和类。
<artifactId>mybatis-plus-annotation</artifactId>
MybatisSqlSessionFactoryBean
ServiceImpl
<artifactId>mybatis-spring</artifactId>
MapperFactoryBean
<artifactId>mybatis-plus-core</artifactId>
MybatisConfiguration
MybatisMapperRegistry
MybatisMapperProxyFactory
MybatisMapperProxy
MybatisMapperMethod
容器初始化
spring 递归初始化以下类,包括:
MybatisPlusAutoConfiguration
MybatisSqlSessionFactoryBean
MybatisConfiguration
MybatisMapperRegistry
这里不一一说明各类的初始化过程,只拿出MybatisMapperRegistry。该类用来实际存储mapper的实现
public class MybatisMapperRegistry extends MapperRegistry {
private final Configuration config;
private final Map<Class<?>, MybatisMapperProxyFactory<?>> knownMappers = new HashMap();
}
mapper bean的初始化
1、spring调用 MapperFactoryBean 初始化声明的mapper接口的bean。
public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
public T getObject() throws Exception {
return this.getSqlSession().getMapper(this.mapperInterface);
}
}
该方法会递归调用上文已经初始化的 MybatisConfiguration、 MybatisMapperRegistry,最终使用 MybatisMapperProxyFactory 生成代理类。
- 生成代理类,并放入 MybatisMapperRegistry 容器中
public class MybatisMapperProxyFactory<T> {
protected T newInstance(MybatisMapperProxy<T> mapperProxy) {
return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
}
}
select调用
- 获取注入的代理类bean
- 调用select方法
实际调用代理方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{} //省略 return new MybatisMapperProxy.PlainMethodInvoker(new MybatisMapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration()));
这里的MybatisMapperMethod,包含实际的select方法
到此流程基本执行完成。
12 声望
1 粉丝
推荐阅读
之前很火给女朋友推送微信服务号消息是怎么做的?
经过了几天的奋战,终于把微信服务号的模板消息给写完了。后端其实没花多少时间,因为之前已经有同学提过pull request了,我在这基础之上简单优化下就完事了,主要的时间都是花在前端上,对前端页面和参数的适配...
Java3y赞 3阅读 1.2k
Java项目是不是分布式,真有那么重要吗?
「微服务」「分布式」在我刚毕业的时候还是比较关注的,那时候还入门了一把SpringCloud,写了一篇很长的文章,还是很顶的,有不少的大号都给我转载了,在知乎又获得了很多的赞。
Java3y赞 2阅读 602评论 1
【Spring Boot】快速整合Mybatis-Plus
Mybatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。它已经被大规模地应用在实际项目中,极大地提高了开发效率。
kamier赞 2阅读 1.1k
mds-spring-boot,Mybatis、Mybatis-Plus多数据源及事务处理框架
mds-spring-boot是一个基于SpringBoot2.x的、全场景支持的、多数据源框架,支持Spring-JDBC、Mybatis、Mybatis-Plus、Mybatis-Tiny、ShardingSphere、Mycat等,支持本地事务及完整的基于Spring-@Transactional声...
penggle赞 1阅读 1.1k
线上FullGC问题排查实践——手把手教你排查线上问题 | 京东云技术团队
观察该机器日志发现,此时有很多线程在执行跑批任务。正常来说,跑批任务是低CPU高内存型,所以此时考虑是FullGC引起的大量CPU占用(之前有类似情况,告知用户后重启应用后解决问题)。
京东云开发者赞 2阅读 352
Java-微服务架构之认证服务
之前通过阅读《Spring微服务实战》写过关于spring-cloud+spring-security+oauth2的认证服务和资源服务文章,以及写过关于spring-gateway做token校验的文章,但是在实战过程中还是发现一些问题,于是通过跟朋友沟...
Awbeci阅读 1.1k
springboot升级过程中踩坑定位分析记录 | 京东云技术团队
因所负责的系统使用的spring框架版本5.1.5.RELEASE在线上出过一个偶发的小事故,最后定位为spring-context中的一个bug导致的。
京东云开发者赞 1阅读 483
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。