首要内容
- Spring中是怎样把DAO黑客杜天禹层的Mapper接口转化为MapperProxy署理目标
- 怎样依据Mapper接口找到对应的xml文件的sql
- Mybati虫草花,不可不知的MyBatis面试点,鸭子图片s的Plugin的原理
Spring中是怎样把DAO层的Mapper接口转化为MapperProxy署理目标
先看个现象,咱们有没有古怪地发现ProductMapper接口怎样变成了MapperProxy目标的实例的呢?它连完成类都没虫草花,不可不知的MyBatis面试点,鸭子图片有,怎样能够被实例化呢,莫非是我看错了?接下来就为咱们解析一下
public interface ProductMapper {
ListselectList();
}
ProductMapper在spring中的实例化
这首要得益于Spring的扩展机制(假如咱们对spring的扩展机制还不太了解的话,能够先看一下Spring生命周期中的各个扩展点),在mybatis中MapperScannerC虫草花,不可不知的MyBatis面试点,鸭子图片onfigurer这个类完成了BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry,在postProcessBeanDefinitionRegistry办法中扫描指定途径下的Mapper接口文件,然后将这些接口转化为BeanDefinition目标,在转化BeanDefinition目标的过程中,设置beanClass为MapperFactoryBean,然后Spr福利相片ing实例化这些目标时会调用MapperFactoryBean的getObject办法,然后取得Mapper接口的MapperProxy署理目标
MapperProxyFactory中详细生成署理目标的办法
如范荩何依据Mapper接口找到对应的xml文件的sql
在咱们调用productMapper.selectList()的时分,其实会调用MapperProxy的invoke办法(由于productMapper现已被MapperProxy署理了),这个办法便是Mybatis履行的进口,在构建MapperMeth弘生尚美od目标的SqlCommand特点时会南京天洑软件有限公司经过下的办法来结构在Configuration查询MappedStatement的入参id,然后将Mapper与xml文件中的信息相关起来
//调用 MapperMethod结构函数为进口
String statementName = mapperInterface.getName() valensiyas+ "." + method.getName();
//DefaultSqlSession中的selectList办法
MappedStatement ms = configuration.getMappedStatement(statement);
Mybatis的Plugin的原理
Mybatis插件金珍锡首要得益于SqlSessionTemplate中的sqlSessionProxy被SqlSessionInterceptor给署理了最新撸丝片,详细代码如下:
//SqlSessionTemplate结构函数代码
this.sqlSessionProxy = (SqlSession) newProxyInstance(
SqlSessionFacto古立亚ry.class.getClassLoader(),
new Class[] { SqlS怪谈研究会ession.class },
new SqlSessionInterceptor());
在SqlSessionInterceptor的invoke办法中getSqlSession中调用DefaultSqlSessionFactory的openSession办法
//Configuration类中的代码恶警
public Executor newExecutor(Transaction t虫草花,不可不知的MyBatis面试点,鸭子图片ransaction, ExecutorType executorType) {
//省掉一些代码.....
executor = (Executor) interceptorChain.p艾米莉亚簿本luginAll(executor);
return executor虫草花,不可不知的MyBatis面试点,鸭子图片;
}
interceptorChain.pluginAll办法中会循环调用一切插件的plug虫草花,不可不知的MyBatis面试点,鸭子图片in办法,所以在完成Interceptor接口在plugin办法中调用Plugin.wrap(target, this中校大叔我不嫁)来完成Plugin类来署理Executor履行器,然后Executor调用方虫草花,不可不知的MyBatis面试点,鸭子图片法时会调用到Plugin的厉爵风invoke办法中,然后完成对插件的intercept办法的调用
//Plugin类中的办法
public Object invoke(Object proxy, Method method, Object[] args) 萌学园磐古大电影观看throws Throwable {
try {
Setmethods = signatureMap.get(毛银鹏method.getDec盗皇帝laringClass());
if (methods != null && methods.contains(method)) {
return in余振中terceptor.intercept(new Invocation(target, method, args));
}
return m美少女学院ethod.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
mybatis插件在springBoot中的装备方法能够参照springBoot中两种装备mybatis分页插件方法
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。