您好,登錄后才能下訂單哦!
本篇內容主要講解“基于Spring怎么整合mybatis的mapper”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于Spring怎么整合mybatis的mapper”吧!
mapperScannerConfigurer實現了BeandifinitionRegistryPostProcessor
后置處理beanFactory時會調用其postProcessBeanDefinitionRegistry
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { if (this.processPropertyPlaceHolders) { processPropertyPlaceHolders(); } ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); scanner.setAddToConfig(this.addToConfig); scanner.setAnnotationClass(this.annotationClass); scanner.setMarkerInterface(this.markerInterface); scanner.setSqlSessionFactory(this.sqlSessionFactory); scanner.setSqlSessionTemplate(this.sqlSessionTemplate); scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName); scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName); scanner.setResourceLoader(this.applicationContext); scanner.setBeanNameGenerator(this.nameGenerator); scanner.setMapperFactoryBeanClass(this.mapperFactoryBeanClass); scanner.registerFilters(); //掃描包集合,注冊mapper的beandifinition scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS)); }
public Set<BeanDefinitionHolder> doScan(String... basePackages) { Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { LOGGER.warn(() -> "No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration."); } else { //會對bean信息進行一些更改和添加,如設置propertyValues,向其中添加sqlsessionFactory等屬性信息等 processBeanDefinitions(beanDefinitions); } return beanDefinitions; }
protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>(); for (String basePackage : basePackages) { //遍歷集合掃描包,獲取包下的可用的mapper信息 Set<BeanDefinition> candidates = findCandidateComponents(basePackage); for (BeanDefinition candidate : candidates) { ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate); candidate.setScope(scopeMetadata.getScopeName()); String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry); if (candidate instanceof AbstractBeanDefinition) { postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName); } if (candidate instanceof AnnotatedBeanDefinition) { AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); } if (checkCandidate(beanName, candidate)) { BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); beanDefinitions.add(definitionHolder); registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; }
如上在進行包掃描時,會調用ClassPathBeanDefinitionScanner#doScan,進行beanDefinition的掃描和注冊
執行完后調用ClassPathMapperScanner#processBeanDefinitions
private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) { //有省略部分 //這里有遍歷所有mapper //將所有mapper的beanClass改為了mapperFactoryBean,最后生成的bean也是該類型的,只有在用到的 //時候才會調用getObject生成mapper definition.setBeanClass(this.mapperFactoryBeanClass); //接下來是添加一些MapperFactoryBean的屬性信息到beandefinition里,在生成實例的時候,會根據 //此處傳入的值來進行具體設置,如sqlsessionFactory definition.getPropertyValues().add("addToConfig", this.addToConfig); if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName)); } else if (this.sqlSessionFactory != null) { definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory); } if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) { definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName)); } else if (this.sqlSessionTemplate != null) { definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate); } }
確定了所有的mapper的beanDefinition為MapperFactoryBean類型后,
確定在生成的bean皆為MapperFactoryBean,在getBean時會調用MapperFactoryBean#getObject
mapperInterface是在MapperFactoryBean創建時,會調用有參構造創建實例,值是創建是
構造方法參數的獲取從mdb中
創建除了傳入mapperInterface,還會在設置屬性值時調用MapperFactoryBean的父類SqlSessionDaoSupport#setSqFactory 將mdb中的sqlsessionfactory傳入其中
mdb的sqlsessionFactory信息來歷
查看方法棧調用,可以看到在開始填充屬性時會去mdb中判斷是否有propertyValue,如果有就返回,這就是之前掃描mapper后ClassPathMapperScanner#processBeanDefinitions對mdb的一些設置
在創造service時,如果有@Autowire 注入mapper,在createBean時會調用getObject
mdb只有一份,可得sqlsessionfactory即只有一份,sqlsessionTemplate一個mapper一份。
到此,相信大家對“基于Spring怎么整合mybatis的mapper”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。