关于sql执行方面

  1. 通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗?

XML文件中的每一个SQL标签就对应一个MappedStatement对象,这里面有两个属性很重要。

id全限定类名+方法名组成的ID。
sqlSource
当前SQL标签对应的SqlSource对象。

创建完MappedStatement对象,将它缓存到Configuration#mappedStatements中。 Configuration对象,我们知道它就是Mybatis中的大管家,基本所有的配置信息都维护在这里。把所有的XML都解析完成之后,Configuration就包含了所有的SQL信息。到目前为止,XML就解析完成了。看到上面的图示,聪明如你,也许就大概知道了。当我们执行Mybatis方法的时候,就通过全限定类名+方法名找到MappedStatement对象,然后解析里面的SQL内容,执行即可。
在这里插入图片描述

Mybatis是如何进行分页的?分页插件的原理是什么?

插件的原理其实很简单,就是在创建组件的时候生成代理对象(Plugin),执行组件方法的时候拦截即可。下面就来详细介绍一下插件在Mybatis底层是如何工作的?Mybatis的四大组件都是在Mybatis的配置类Configuration中创建的,具体的方法如下:
//创建Executor
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    //调用pluginAll方法,生成代理对象
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

1、创建SqlSource
Mybatis会把每个SQL标签封装成SqlSource对象。然后根据SQL语句的不同,又分为动态SQL和静态SQL![在这里插入图片描述](https://img-blog.csdnimg.cn/a2dd5a1dc4794dc7a01337b11f3c0433.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAc2oxMDI3NTIz,size_20,color_FFFFFF,t_70,g_se,x_16)
。其中,静态SQL包含一段String类型的sql语句;而动态SQL则是由一个个SqlNode组成。

假如我们有这样一个SQL:<select id="getUserById" resultType="user">
	select * from user 
	<where>
		<if test="uid!=null">
			and uid=#{uid}
		</if>
	</where>
</select>	它对应的SqlSource对象看起来应该是这样的:

![在这里插入图片描述](https://img-blog.csdnimg.cn/c18f8541a72c42f7a42867b4c0d798b5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAc2oxMDI3NTIz,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/ee8ab7a6614c4cfab22a4ec6c1c12e31.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/fe25fed3a4964d6a81d3120d67c65f00.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAc2oxMDI3NTIz,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/aefeddb11d6a482297a5860ec1559316.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAc2oxMDI3NTIz,size_20,color_FFFFFF,t_70,g_se,x_16)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值