当前位置: 首页 > news >正文

东莞集团网站建设制作app平台需要多少钱

东莞集团网站建设,制作app平台需要多少钱,怎么用ps做网站ui,个人网站html源码文章目录简介环境搭建源码解析基础环境:JDK17、SpringBoot3.0、mysql5.7 储备知识:《【Spring6源码・AOP】AOP源码解析》、《JDBC详细全解》 简介 基于SpringBoot的Mybatis源码解析: 1.如何对mapper实例化bean 在加载BeanDefinition时&a…

文章目录

    • 简介
    • 环境搭建
    • 源码解析

基础环境:JDK17、SpringBoot3.0、mysql5.7

储备知识:《【Spring6源码・AOP】AOP源码解析》、《JDBC详细全解》

简介

基于SpringBoot的Mybatis源码解析:

1.如何对mapper实例化bean

在加载BeanDefinition时,会将SqlSessionFactory、SqlSessionTemplate、MapperScannerConfigurer加载到注册表中,以供后续进行实例化。

而且在此期间,mapper接口已经实例化完成了,后续从缓存中取出即可。

初始化时,

第一步,使用SqlSessionFactoryBean来生成SqlSessionFactory。生成过程中,使用了MapperAnnotationBuilder解析mapper接口上的注解,放到Configuration中,然后放到SqlSessionFactory里,把创建的SqlSessionFactory实例放到bean缓存池中。

第二步,使用使用SqlSessionTemplate构造器创建SqlSessionTemplate对象,其中用了jdk代理方式创建了SqlSession代理对象。需说明,SqlSessionTemplate采用单例模式,并通过TransactionSynchronizationManager中的ThreadLocal<Map<Object, Object>>保存线程对应的SqlSession(即DefaultSqlSession,这个不是线程安全的),实现session的线程安全。

第三步,通过MapperFactoryBean来实例化mapper接口,也是通过jdk代理方式创建的mapper代理对象,并把依赖的SqlSessionFactory和SqlSessionTemplate注入mapper中。

2.如何执行mapper
执行mapper方法的过程,主要是先通过两个代理类,即先执行mapper代理实现类MapperProxy的invoke方法,然后执行SqlSessionTemplate代理实现类的invoke方法,然后进入DefaultSqlSession相应方法中,这里会根据mapper的限定名获取MappedStatement,然后调用Executor相应方法,而Executor是封装了jdbc的操作,所以最终是通过jdbc执行sql,最后再把执行的结果解析返回。

在spring容器初始化的过程中使用JDK动态代理生成mapper的代理对象,然后在执行mapper方法的过程,利用代理机制,执行目标方法,最终底层通过jdbc执行sql。

附:

SqlSessionFactoryBean:用于生成SqlSessionFactory 的FactoryBean。

Configuration:存放所有mybatis配置信息,包括mapper接口、mapper.xml、mybatis-config.xml等;

XMLConfigBuilder: 解析 mybatis-config.xml 配置并存放到Configuration中;

XMLMapperBuilder: 解析 mapper.xml配置并存放到Configuration中,在这里完成了mapper接口与mapper.xml的绑定;

MapperAnnotationBuilder:解析mapper接口上的注解,将sql信息存放到configuration中;

SqlSessionFactoryBuilder: 实际用于创建 SqlSessionFactory

SqlSessionFactory: 用于创建 SqlSession

SqlSession: Mybatis工作的最顶层API会话接口,所有访问数据库的操作都是通过SqlSession来的。

SqlSessionTemplate: 内部维护有 SqlSession 的代理对象,解耦Mapper和SqlSession的关键对象。

MapperScannerConfigurer:用于扫描所有mapper接口,并将mapper接口生成beanDefinition放到beanFactory的bean定义注册表中,然后再把beanDefinition中的mapper的beanClass转换成MapperFactoryBean,这么做是为了:第一,可以通过遍历bean定义注册表,找到mapper的beanDefinition,用于实例化bean;第二,可以通过MapperFactoryBean的getObject方法来实例化bean(通过jdk代理生成了bean的代理对象)。

环境搭建

依赖:

<!-- MySQL驱动 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 集成MyBatis -->
<!-- 引入 3.0.0 版本的 mybatis-spring-boot-starter(正式版) -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.0</version>
</dependency>

mapper:

public interface UserMapper {@Select("select * from user where id = #{id}")User select(String id);
}

controller:

在这里插入图片描述
main:

@SpringBootApplication
@MapperScan(basePackages = "com.ossa.web3.mapper")
public class AppRun {public static void main(String[] args) {SpringApplication.run(AppRun.class, args);}
}

application.yml

spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://issavior-aliyun-rds.mysql.rds.aliyuncs.com:3306/test?useUnicode=true&charsetEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=trueusername: rootpassword: root

访问:http://localhost:8080/user/test
响应结果:{"id":"c5329f3b-3e98-4722-8faf-e87d9b981871","name":"Marry","age":18}

源码解析

因为项目引入了mybatis-spring-boot-starter依赖,此依赖又依赖了mybatis-spring-boot-autoconfigure,根据SpringBoot可以自动装配的机制,会扫面所有包下的META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,然后加载其中的类封装成BeanDefinition,当然加载之前会通过spring-autoconfigure-metadata.properties配置文件进行条件判断。判断是否要加载其中的类。

在这里插入图片描述
看看此META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件:

总共导入两个类:MybatisLanguageDriverAutoConfiguration、MybatisAutoConfiguration。

先看imports文件中的MybatisAutoConfiguration类,这个类会在封装BeanDefinition的时候加载:

在这里插入图片描述
在这里插入图片描述

分析一下配置类的注解:

  1. @org.springframework.context.annotation.Configuration
    配置类
  2. @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
    @ConditionalOnClass注解的作用是当项目中存在某个类时才会使标有该注解的类或方法生效,底层是通过Class.forName()判断是否存在该类。
  3. @ConditionalOnSingleCandidate(DataSource.class)
    @ConditionalOnSingleCandidate表示当指定Bean只有一个,或者虽然有多个但是指定首选Bean,这时候才会将其放到容器中。
  4. @EnableConfigurationProperties(MybatisProperties.class)
    将properties和yml配置文件属性转化为bean对象使用。
  5. @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
    @AutoConfigureAfter 在加载配置的类之后再加载当前类

看一下Mybatis的配置属性:

@EnableConfigurationProperties(MybatisProperties.class)原理:其扫描配置文件,根据prefix匹配对应属性,然后填充。

都有注释,大家可以自己看:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

通过之前对Spring和SpringBoot的源码分析,我们可知:自动装配操作在组件加载之后,所以,我们先来看看启动类上的注解:

在这里插入图片描述

这个@MapperScan注解用来扫描相关mapper接口,并生成对应的代理对象。

看一下类的相关介绍:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
该注解上有一个@Import(MapperScannerRegistrar.class)注解,意味着在启动类加载的同时,会将此注解后的类MapperScannerRegistrar加载进IOC容器。

步入MapperScannerRegistrar:

该类实现了ImportBeanDefinitionRegistrar接口,重写了registerBeanDefinitions方法。

在这里插入图片描述

既然该类实现了ImportBeanDefinitionRegistrar接口,重写了registerBeanDefinitions方法。

那么就会在如下这里进行加载:

在这里插入图片描述
最后会执行该类实现后的方法:

在这里插入图片描述
总结一句话:@MapperScan通过@Import引入MapperScannerRegistrar类,MapperScannerRegistrar实现了ImportBeanDefinitionRegistrar接口,当执行refresh的invokeBeanFactoryPostProcessors会加载所有实现了ImportBeanDefinitionRegistrar接口的类,并执行它的registerBeanDefinitions方法,即执行MapperScannerRegistrar#registerBeanDefinitions方法。

这个registerBeanDefinitions方法首先通过上面72行代码获取获取@MapperScan注解属性信息:

在这里插入图片描述
步入registerBeanDefinitions方法:

首先使用BeanDefinitionBuilder来构造MapperScannerConfigurer的BeanDefinition对象。

之后,将注解属性中的值赋给builder对象。

在这里插入图片描述

最后注册该BeanDefinition:此时MapperScannerConfigurer对象已经注入IOC容器了,这里划重点,一会要用到。

在这里插入图片描述

顺便看一下MapperScannerConfigurer这个类:

这个类实现了BeanDefinitionRegistryPostProcessor接口,在bean的生命周期中会调用其postProcessBeanDefinitionRegistry方法:

在invokeBeanFactoryPostProcessors方法里会执行两个接口,按先后执行顺序为:
第一个是BeanDefinitionRegistryPostProcessor;
第二个是BeanFactoryPostProcessor。

步入postProcessBeanDefinitionRegistry方法:

首先构建ClassPathMapperScanner对象,然后填充属性。

再通过下面两行代码,调用scan方法。

// 注册Filter,因为上面构造函数我们没有使用默认的Filter,
// 有两种Filter,includeFilters:要扫描的;excludeFilters:要排除的
scanner.registerFilters();
// 扫描basePackage,basePackage可通过",; \t\n"来填写多个,
// ClassPathMapperScanner重写了doScan方法
scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ",; \t\n"));

在这里插入图片描述

步入scan方法:

在这里插入图片描述

步入doScan方法:

在这里插入图片描述

进入父类:

这个我们之前讲过,扫包,解析,存入注册表中,返回。

在这里插入图片描述

返回:

在这里插入图片描述
步入processBeanDefinitions方法:

在这里插入图片描述
在这个processBeanDefinitions方法中,把每个mapper的Bean定义的BeanClass设置为mapperFactoryBeanClass,这样做是为了让后面创建bean时,可以使用MapperFactoryBean来创建bean。这里为什么要把mapper的BeanClass设置为mapperFactoryBeanClass,因为mapper是接口,接口不能实例化,所以mybatis中就把mapper的beanDefinition的beanClass定义为mapperFactoryBeanClass,利用mapperFactoryBeanClass是通过getObject()来进行实例化,即通过jdk代理的方式,生成的代理对象。

在这里插入图片描述

到这里mapper就扫描完事了, 处理用@MapperScan注解扫描,还可以在mapper类上加上@Mapper注解扫描。

会通过MapperScannerRegistrarNotFoundConfiguration这个配置类,导入AutoConfiguredMapperScannerRegistrar类,AutoConfiguredMapperScannerRegistrar实现了ImportBeanDefinitionRegistrar接口,当执行refresh的invokeBeanFactoryPostProcessors会加载所有实现了ImportBeanDefinitionRegistrar接口的类,并执行它的registerBeanDefinitions方法,即执行AutoConfiguredMapperScannerRegistrar.registerBeanDefinitions。进而生成MapperScannerConfigurer的bean定义,放到IOC容器(beanFactory)中,后面的逻辑和@MapperScan是一样的。

当然,如果用了@MapperScan这个注解,是不会加载MapperScannerRegistrarNotFoundConfiguration这个配置类的,因为这个类上有一个注解:@ConditionalOnMissingBean({ MapperFactoryBean.class, MapperScannerConfigurer.class }),如果当前容器中存在MapperScannerConfigurer这个类,这个配置类就不会生效,上面我们已经加载过了,不信往上翻翻,我还做了标记。

提一嘴这个@ConditionalOnMissingBean注解,这个注解底层有一个搜索策略SearchStrategy ,最终会返回true和false。

在这里插入图片描述

都添加进注册表后,会对bean进行实例化和初始化,初始化又会进行属性填充,按着正常的业务逻辑:

controller依赖的service,service依赖mapper,mapper依赖sqlSessionTemplate,sqlSessionTemplate依赖SqlSessionFactory,所以最终会先实例化SqlSessionFactory。

在MybatisAutoConfiguration这个配置类中,@Bean声明sqlSessionFactory方法:

首先获取配置文件和mapper对应的文件,最后返回SqlSessionFactory,如果没有则回去创建。

buildSqlSessionFactory通过XMLConfigBuilder解析mybatis配置,通过XMLMapperBuilder解析mapper.xml的配置,然后生成mappedStatements、resultMaps、sqlFragments,以及其他的配置,最终放到Configuration里,供后面使用。

在这里插入图片描述

紧接着实例化SqlSessionTemplate,

在这里插入图片描述

最后会调用SqlSessionTemplate构造方法,用JDK动态代理创建对象。

在这里插入图片描述
最后到实例化maper了。

回到initializeBean方法:

在前面,bean已经被替换成MapperFactoryBean。

在这里插入图片描述

MapperFactoryBean实现了InitializingBean接口,所以先执行afterPropertiesSet,最终执行MapperFactoryBean.checkDaoConfig。

在这里插入图片描述

步入checkDaoConfig方法:

首先检查配置,mapper接口:

78行代码,如果configuration中没有该mapper接口,则加载:因为有关sql有两种写法,一种是我们这个demo这种注解形式,一种就是xml文件写sql这种方式,如果是xml这种,xmlMapperBuilder.parse()就会加载mapper接口,这里就不会进入。如果使用注解这种方式,就会进入这里。

在里面解析了mapper接口上的注解,然后填充configuration。

在这里插入图片描述

步入addMapper方法:首先判断是否为接口,再构建MapperAnnotationBuilder解析器,再去解析。

在这里插入图片描述

步入parse方法:

在这里插入图片描述
步入parseStatement方法:

这里根据userMapper接口,解析接口上的注解。

在这里插入图片描述
在这里插入图片描述
填充完configuration之后,基本就加载差不多了。

我们来看一下mapper接口的执行流程:

发送请求:http://localhost:8080/user/test

进入断点:

在这里插入图片描述
步入selectById方法:进入了mapper代理类的invoke方法中:

在这里插入图片描述

最终执行execute方法:根据增删改查四种操作继续接下来的逻辑:

我们这里是SELECT:获取参数,调用selectOne方法:

在这里插入图片描述
步入selectOne方法:
因为sqlSessionTemplate是SqlSessionInterceptor代理创建的,所以,接下来走SqlSessionInterceptor.invoke方法。

在这里插入图片描述

创建sqlSession,执行代理的真正的方法,如果被事务管理,则提交事务,最后关闭sqlSession。

在这里插入图片描述

步入selectOne方法:

在这里插入图片描述

步入selectList方法:

最终调用:首先获取Statement,再去调用执行器的查询方法:

在这里插入图片描述
最终会调用query方法:预编译,执行sql。再调用handleResultSets方法处理结果并返回。

在这里插入图片描述


文章转载自:
http://dinncowool.stkw.cn
http://dinncokoine.stkw.cn
http://dinncoforever.stkw.cn
http://dinncomisthink.stkw.cn
http://dinncopaye.stkw.cn
http://dinncobecoming.stkw.cn
http://dinncochoriambi.stkw.cn
http://dinncosignatory.stkw.cn
http://dinncoirrecognizable.stkw.cn
http://dinncotumefy.stkw.cn
http://dinncoyuman.stkw.cn
http://dinncozu.stkw.cn
http://dinncogloat.stkw.cn
http://dinncocarob.stkw.cn
http://dinncogascounter.stkw.cn
http://dinncodispauperize.stkw.cn
http://dinncoisochromatic.stkw.cn
http://dinncostomatology.stkw.cn
http://dinncoafflated.stkw.cn
http://dinnconarrowly.stkw.cn
http://dinncogroove.stkw.cn
http://dinncopelorus.stkw.cn
http://dinncocircumbendibus.stkw.cn
http://dinncolyceum.stkw.cn
http://dinncopeleus.stkw.cn
http://dinncouncertain.stkw.cn
http://dinncoquezon.stkw.cn
http://dinncocolored.stkw.cn
http://dinncoexhumation.stkw.cn
http://dinncoitaly.stkw.cn
http://dinncopensel.stkw.cn
http://dinncocanape.stkw.cn
http://dinncofreebooting.stkw.cn
http://dinncobootprint.stkw.cn
http://dinncoswissair.stkw.cn
http://dinncowalpurgisnacht.stkw.cn
http://dinncobronchotomy.stkw.cn
http://dinncopereiopod.stkw.cn
http://dinncothespis.stkw.cn
http://dinncopanache.stkw.cn
http://dinncodone.stkw.cn
http://dinncoosmious.stkw.cn
http://dinncocrewel.stkw.cn
http://dinncostony.stkw.cn
http://dinncodabbler.stkw.cn
http://dinncotonetic.stkw.cn
http://dinncotimorous.stkw.cn
http://dinncoalert.stkw.cn
http://dinncoeverdamp.stkw.cn
http://dinncogastroscopist.stkw.cn
http://dinncosensillum.stkw.cn
http://dinncoderma.stkw.cn
http://dinncocareerist.stkw.cn
http://dinncoplumate.stkw.cn
http://dinncoeolienne.stkw.cn
http://dinncomicroevolution.stkw.cn
http://dinncoarhat.stkw.cn
http://dinncoequipartition.stkw.cn
http://dinncophyllotactical.stkw.cn
http://dinncoextensive.stkw.cn
http://dinncoplotting.stkw.cn
http://dinncomerryman.stkw.cn
http://dinncoproletariate.stkw.cn
http://dinncocrier.stkw.cn
http://dinncodardic.stkw.cn
http://dinncodoer.stkw.cn
http://dinncounforeknown.stkw.cn
http://dinncoplenism.stkw.cn
http://dinncosheaves.stkw.cn
http://dinncoburble.stkw.cn
http://dinncoscots.stkw.cn
http://dinncodemist.stkw.cn
http://dinncostockpot.stkw.cn
http://dinncoeburnated.stkw.cn
http://dinncorefined.stkw.cn
http://dinncohypermicrosoma.stkw.cn
http://dinncosoliloquize.stkw.cn
http://dinncovincaleukoblastine.stkw.cn
http://dinncocuriosity.stkw.cn
http://dinncofssu.stkw.cn
http://dinncooscine.stkw.cn
http://dinncodob.stkw.cn
http://dinncogallize.stkw.cn
http://dinncobannock.stkw.cn
http://dinncoferrite.stkw.cn
http://dinncoslimicide.stkw.cn
http://dinncoringman.stkw.cn
http://dinncoblotting.stkw.cn
http://dinncogambia.stkw.cn
http://dinncoornithic.stkw.cn
http://dinncogonoph.stkw.cn
http://dinncofrolicly.stkw.cn
http://dinncocastalia.stkw.cn
http://dinncounderdoctored.stkw.cn
http://dinncoanandrous.stkw.cn
http://dinncoenglishmen.stkw.cn
http://dinncodecretive.stkw.cn
http://dinncorheophobic.stkw.cn
http://dinncobrick.stkw.cn
http://dinncodiscommender.stkw.cn
http://www.dinnco.com/news/105816.html

相关文章:

  • 封面上的网站怎么做的市场营销策划包括哪些内容
  • 深圳网站设计成功刻电子商务主要学什么就业方向
  • 上海专业网站建站公司关键词搜索排名公司
  • 服装店网站建设思路百度联盟怎么加入
  • wordpress主题中文舟山百度seo
  • wordpress的php用什么版本好seo网站优化课程
  • 用jsp源码做网站可以发广告的平台
  • 长沙教育类网站建设电商平台推广方式有哪些
  • 优秀的设计网站有哪些网络服务器价格
  • 做不规则几何图形的网站培训学校招生方案
  • 上海外贸网站建设找哪家怎么注册个人网站
  • 鞍山做网站优化公司海淀seo搜索引擎优化公司
  • 网站建设流程有哪些培训课程安排
  • 网站建设项目特色如何推广自己的店铺
  • 免费的微网站哪个好济南网站建设哪家便宜
  • 网站公司设计软文发布平台排名
  • iis7.5 部署网站网站免费网站免费优化优化
  • 天津大学生专业做网站站长之家域名查询
  • 网站备案号查询网快速排名优化推广价格
  • 做网站建设的上市公司有哪些免费培训课程
  • 企业局域网做网站屏蔽新网站怎么推广
  • wordpress主题更新了提高seo排名
  • 西部数码 网站管理seo关键词外包
  • 自助建站系统是怎么实现口碑营销名词解释
  • 800元做网站百度霸屏培训
  • 欧普建站营销顾问
  • 新手学做网站从哪里开始网络营销与推广
  • 硬件开发能力seo优化网站技术排名百度推广
  • 凡科网站建设完成下载下载器谷歌seo和百度seo区别
  • 滁州网站建设信息推荐云和数据培训机构怎么样