日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot Mybatis Starter 解析

發(fā)布時間:2023/12/20 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot Mybatis Starter 解析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.Mybatis-starter模塊介紹

????????我們知道Mybatis有兩大核心組件:SqlSessionFactory和Mapper接口。該starter可以幫助我們自動完成SqlSessionFactory和Mapper的裝配與注入,可以極大的簡化我們的開發(fā)難度,提高開發(fā)效率。

Mybatis-starter主要完成以下功能:

  • Autodetect an existing DataSource(自動發(fā)現(xiàn)存在的datasource)
  • Will create and register an instance of a SqlSessionFactory passing that DataSource as an input using the SqlSessionFactoryBean(SqlSessionFactoryBean根據(jù)DataSource完成SqlSessionFactory實(shí)例的創(chuàng)建)
  • Will create and register an instance of a SqlSessionTemplate got out of the SqlSessionFactory(根據(jù)SqlSessionFactory完成SqlSessionTemplate實(shí)例的創(chuàng)建與注冊)
  • Auto-scan your mappers,link them to the SqlSessionTemplate and register them to Spring context,so they can be injected into your beans(自動掃描mappers,關(guān)聯(lián)于SqlSessionTemplate并注入Spring容器中以便其他beans使用)
  • 2. Starter 自動裝配解析

    ????????從該模塊的pom文件可以看出,它里面引入了mybatis-spring-boot-autoconfigure依賴,由此我們可以知道在該依賴的META-INF目錄下的spring.factories中配置了EnableAutoConfiguration的實(shí)現(xiàn)類,總共有2個分別是:MybatisLanguageDriverAutoConfiguration和MybatisAutoConfiguration。MybatisLanguageDriverAutoConfiguration主要是mapper配合注解方式編寫sql語句,適用于簡單的sql,一般不加載。

    MybatisAutoConfiguration裝配的條件是:

  • 存在SqlSessionFactory和SqlSessionFactoryBean的class文件;
  • 存在單個Datasource bean或者多個但是定義了primary datasource;
  • 存在MybatisProperties配置文件;
  • 在DataSourceAutoConfiguration和MybatisLanguageDriverAutoConfiguration初始化完成之后。
  • ????????此外,在該配置類中注入了兩個Bean,分別是SqlSessionFactory,該類是單個數(shù)據(jù)庫經(jīng)過編譯之后內(nèi)存的鏡像,通過它可以于數(shù)據(jù)完成交互。另外一個是SqlSessionTemplate,該類封裝了SqlSessionFactory和SqlSession,里面還定義了數(shù)據(jù)庫操作方法,方便對數(shù)據(jù)進(jìn)行操作。操作數(shù)據(jù)的類是一個代理類,代理的核心邏輯在SqlSessionInterceptor類中。該代理方法也很簡單,首先獲取SqlSession,之后調(diào)用method invoke方法。SqlSession的獲取是以SqlSessionFactory為key去本地的ThreadLocal中獲取,如果不存在就創(chuàng)建一個。

    3.Mapper類掃描

    ??在MybatisAutoConfiguration中有2個內(nèi)部類,分別是:MapperScannerRegistrarNotFoundConfiguration和AutoConfiguredMapperScannerRegistrar,MapperScannerRegistrarNotFoundConfiguration的主要作用是ImportAutoConfiguredMapperScannerRegistrar,在方法registerBeanDefinitions中注冊了MapperScannerConfigurer類,該類實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor接口,會在bean實(shí)例化之前調(diào)用postProcessBeanDefinitionRegistry方法

    ?

    ? ? ?在processBeanDefinitions方法中,將beanDefinition的class替換成了MapperFactoryBeanClass的clas,MapperFactoryBeanClass是實(shí)現(xiàn)了FactoryBean,它里面有個getObject方法(getSqlSession().getMapper(this.mapperInterface)),該方法返回一個mapper 的代理對象MapperProxy,每次調(diào)用mappe 的方法都會去調(diào)用該類的invoke方法,最終調(diào)用MapperMethod的invoke方法,之后在調(diào)用mapperMethod的execute方法中,然后根據(jù)sql類型完成操作。

    ????????當(dāng)我們查看@MapperScan注解時,發(fā)現(xiàn)它引入了MapperScannerRegistrar,該類實(shí)現(xiàn)了ImportBeanDefinitionRegistrar類,最終會調(diào)用RegisterBeanDefinitions方法,該方法與我們上面的解析流程一樣。在MybatisAutoConfiguration配置類中,如果要讓MapperScannerRegistrarNotFoundConfigurration配置生效,那么就需要不存在MapperScannerConfigurer的bean。因此該自動配置類與@MapperScan只能有一個生效,放置mapper重復(fù)掃描。

    總結(jié)

    以上是生活随笔為你收集整理的SpringBoot Mybatis Starter 解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。