本篇文章為大家展示了怎么在Spring Boot中配置Mybatis,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨?。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10多年網(wǎng)站建設(shè)經(jīng)驗(yàn)創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營銷服務(wù)商,為您提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、HTML5建站、網(wǎng)站制作、成都品牌網(wǎng)站建設(shè)、重慶小程序開發(fā)服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。
SpringBoot整合Mybatis
提供SpringBoot整合Mybatis的實(shí)例,通過Mybatis實(shí)現(xiàn)簡單的增刪改查功能;
1.表數(shù)據(jù)
CREATE TABLE `role` ( `note` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `role_name` varchar(255) DEFAULT NULL, `id` bigint(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8
提供創(chuàng)建role表相關(guān)的sql,對(duì)表進(jìn)行增刪改查操作;
2.整合Mybatis的依賴
主要是mybatis-spring-boot-starter和使用的MySQL驅(qū)動(dòng):
org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1 mysql mysql-connector-java 5.1.29
3.配置application.properties
提供連接mysql相關(guān)的信息:url,驅(qū)動(dòng),用戶名,密碼;
spring.datasource.url=jdbc:mysql://localhost/mybatis spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4.提供bean和Dao
分別提供表對(duì)應(yīng)的bean類和操作數(shù)據(jù)庫的dao類;
public class Role { private long id; private String roleName; private String note; //省略get/set方法 }
@Mapper public interface RoleDao { @Select("SELECT id,role_name as roleName,note FROM role WHERE id = #{id}") Role findRoleById(@Param("id") long id); }
5.提供Service和Controller
public interface RoleService { public Role findRoleById(long roleId); } @Service public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDao; @Override public Role findRoleById(long roleId) { return roleDao.findRoleById(roleId); } }
@RestController public class RoleController { @Autowired private RoleService roleService; @RequestMapping("/role") public String getRole(long id) { return roleService.findRoleById(id).toString(); } }
啟動(dòng)服務(wù),進(jìn)行簡單的測(cè)試:http://localhost:8888/role?id=111
結(jié)果如下:
Role [id=111, roleName=zhaohui, note=hello]
6.提出問題
如上實(shí)例中,我們使用了很少的配置,就通過mybatis實(shí)現(xiàn)了操作數(shù)據(jù)庫;正常使用mybatis需要的SqlSessionFactory和SqlSession沒有看到被實(shí)例化,同時(shí)mybatis依賴的數(shù)據(jù)源也沒有看到被引用,那SpringBoot是如何幫我們自動(dòng)配置的,下面重點(diǎn)分析一下;
SpringBoot自動(dòng)配置
1.自動(dòng)配置注解
要想使用自動(dòng)配置功能,SpringBoot提供了注解@EnableAutoConfiguration,當(dāng)然不需要我們配置因?yàn)樵贎SpringBootApplication注解中默認(rèn)以及啟用了;
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { //...省略... }
可以看到@SpringBootApplication注解本身也有注解@EnableAutoConfiguration:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { //...省略... }
在注解@EnableAutoConfiguration中重點(diǎn)看一下@Import注解中使用的AutoConfigurationImportSelector類,此類是自動(dòng)注解的核心類,會(huì)有條件的加載我們默認(rèn)指定的配置類;這里有兩個(gè)概念一個(gè)是有條件,一個(gè)是配置類,分別簡單介紹一下:配置類可以簡單理解就是相關(guān)組件對(duì)接SpringBoot的對(duì)接類,此類可以做一些初始化的工作;有條件表示并不是有配置類就能被對(duì)接上,是有條件的,SpringBoot默認(rèn)提供了大量配置類,但并不是所有配置類都能被加載初始化的,是有條件的,比如mybatis在沒有數(shù)據(jù)源的情況下,沒有mybatis基礎(chǔ)包的情況下是不能被對(duì)接的;下面首先看一下SpringBoot提供的哪些條件類;
2.條件類
SpringBoot提供了很多條件類,可以在配置中上配置注解條件類,相關(guān)條件類可以在spring-boot-autoconfigure包下的org.springframework.boot.autoconfigure.condition下找到,主要包含如下:
ConditionalOnBean:當(dāng)前容器有指定Bean的條件下;
ConditionalOnClass:當(dāng)前類路徑下有指定類的條件下;
ConditionalOnCloudPlatform:當(dāng)指定了云平臺(tái)的時(shí)候;
ConditionalOnExpression:SpEL表達(dá)式作為判斷條件;
ConditionalOnJava:JVM版本作為判斷條件;
ConditionalOnJndi:在JNDI存在的條件下查找指定的位置;
ConditionalOnMissingBean:當(dāng)容器里沒有指定Bean的情況下;
ConditionalOnMissingClass:當(dāng)類路徑下沒有指定的類的條件下;
ConditionalOnNotWebApplication:當(dāng)前項(xiàng)目不是WEB項(xiàng)目的條件下;
ConditionalOnProperty:當(dāng)前應(yīng)用是否配置了指定屬性指定的值;
ConditionalOnResource:只有當(dāng)指定的資源位于類路徑下;
ConditionalOnSingleCandidate:bean工廠中只有一個(gè)或者有多個(gè)情況下是主要的候選bean;
ConditionalOnWebApplication:當(dāng)前項(xiàng)目是WEB項(xiàng)目的條件下。
以上是注解類,注解本身沒有功能,只是提供標(biāo)記的功能,具體功能在@Conditional中指定的,比如ConditionalOnBean注解如下所示:
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnBeanCondition.class) public @interface ConditionalOnBean { //...省略... }
相關(guān)功能的實(shí)現(xiàn)就在OnBeanCondition類中,同樣其他注解類的實(shí)現(xiàn)類也在包org.springframework.boot.autoconfigure.condition下找到;
3.自動(dòng)配置過程
Springboot應(yīng)用啟動(dòng)過程中使用ConfigurationClassParser分析配置類,此類中有一個(gè)processImports方法,此方法用來處理@Import注解,在@EnableAutoConfiguration注解存在@Import注解,這時(shí)候會(huì)實(shí)例化注解中的AutoConfigurationImportSelector,在其內(nèi)部有一個(gè)AutoConfigurationGroup內(nèi)部類,內(nèi)部類有兩個(gè)核心方法分別是:process和selectImports;
@Override public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) { Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector, () -> String.format("Only %s implementations are supported, got %s", AutoConfigurationImportSelector.class.getSimpleName(), deferredImportSelector.getClass().getName())); AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector) .getAutoConfigurationEntry(getAutoConfigurationMetadata(), annotationMetadata); this.autoConfigurationEntries.add(autoConfigurationEntry); for (String importClassName : autoConfigurationEntry.getConfigurations()) { this.entries.putIfAbsent(importClassName, annotationMetadata); } }
此方法主要獲取經(jīng)過條件過濾之后可用的自動(dòng)配置類,主要調(diào)用AutoConfigurationImportSelector中的getAutoConfigurationEntry完成的:
protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } AnnotationAttributes attributes = getAttributes(annotationMetadata); Listconfigurations = getCandidateConfigurations(annotationMetadata, attributes); configurations = removeDuplicates(configurations); Set exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return new AutoConfigurationEntry(configurations, exclusions); }
首先獲取了所有備選的自動(dòng)配置類,然后刪除了重復(fù)和被排除的類,最后通過條件進(jìn)行篩選出可用的配置類,下面分別看一下,首先看一下如何獲取所有備選的配置類:
protected ListgetCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " + "are using a custom packaging, make sure that file is correct."); return configurations; }
通過SpringFactoriesLoader獲取類路徑下META-INF/spring.factories文件中key為org.springframework.boot.autoconfigure.EnableAutoConfiguration的配置類,可以看一下spring-boot-autoconfigure.jar中的spring.factories內(nèi)容:
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\ //...以下省略...
當(dāng)然這里只是截取了其中一個(gè)類路徑j(luò)ar下的部分配置,獲取所有配置類之后進(jìn)行去重,去被排除的類,然后進(jìn)行條件過濾,下面重點(diǎn)看一下:
private Listfilter(List configurations, AutoConfigurationMetadata autoConfigurationMetadata) { long startTime = System.nanoTime(); String[] candidates = StringUtils.toStringArray(configurations); boolean[] skip = new boolean[candidates.length]; boolean skipped = false; for (AutoConfigurationImportFilter filter : getAutoConfigurationImportFilters()) { invokeAwareMethods(filter); boolean[] match = filter.match(candidates, autoConfigurationMetadata); for (int i = 0; i < match.length; i++) { if (!match[i]) { skip[i] = true; candidates[i] = null; skipped = true; } } } if (!skipped) { return configurations; } List result = new ArrayList<>(candidates.length); for (int i = 0; i < candidates.length; i++) { if (!skip[i]) { result.add(candidates[i]); } } if (logger.isTraceEnabled()) { int numberFiltered = configurations.size() - result.size(); logger.trace("Filtered " + numberFiltered + " auto configuration class in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms"); } return new ArrayList<>(result); }
此方法大致就是首先獲取配置的AutoConfigurationImportFilter ,然后對(duì)之前獲取的所有配置類進(jìn)行過濾,最后返回過濾之后的配置類;AutoConfigurationImportFilter同樣也是通過SpringFactoriesLoader類進(jìn)行加載類路徑下META-INF/spring.factories,只不過當(dāng)前的key是:org.springframework.boot.autoconfigure.AutoConfigurationImportFilter,可以看一下SpringBoot默認(rèn)配置的filter:
# Auto Configuration Import Filters org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\ org.springframework.boot.autoconfigure.condition.OnBeanCondition,\ org.springframework.boot.autoconfigure.condition.OnClassCondition,\ org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
可以看到Filter其實(shí)就是上文介紹的條件類,這里默認(rèn)了OnBeanCondition,OnClassCondition以及OnWebApplicationCondition,已這里使用的Mybatis為例看一下MybatisAutoConfiguration的注解:
@org.springframework.context.annotation.Configuration @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class }) @ConditionalOnSingleCandidate(DataSource.class) @EnableConfigurationProperties(MybatisProperties.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class) public class MybatisAutoConfiguration implements InitializingBean { //...以下省略... }
可以看到其中有用到@ConditionalOnClass,表示必須提供SqlSessionFactory和SqlSessionFactoryBean類的情況下才加載此配置類,而整兩個(gè)是正式Mybatis基礎(chǔ)包中提供的;有了基礎(chǔ)包還不行,還需要DataSource,而且DataSource必須在MybatisAutoConfiguration實(shí)例化之前初始化好,SpringBoot是如何實(shí)現(xiàn),繼續(xù)看另外一個(gè)核心方法selectImports():
@Override public IterableselectImports() { if (this.autoConfigurationEntries.isEmpty()) { return Collections.emptyList(); } Set allExclusions = this.autoConfigurationEntries.stream() .map(AutoConfigurationEntry::getExclusions).flatMap(Collection::stream).collect(Collectors.toSet()); Set processedConfigurations = this.autoConfigurationEntries.stream() .map(AutoConfigurationEntry::getConfigurations).flatMap(Collection::stream) .collect(Collectors.toCollection(LinkedHashSet::new)); processedConfigurations.removeAll(allExclusions); return sortAutoConfigurations(processedConfigurations, getAutoConfigurationMetadata()).stream() .map((importClassName) -> new Entry(this.entries.get(importClassName), importClassName)) .collect(Collectors.toList()); } private List sortAutoConfigurations(Set configurations, AutoConfigurationMetadata autoConfigurationMetadata) { return new AutoConfigurationSorter(getMetadataReaderFactory(), autoConfigurationMetadata) .getInPriorityOrder(configurations); }
首先是對(duì)被排除類的一個(gè)過濾,然后接下來重點(diǎn)看一下對(duì)配置類進(jìn)行排序的一個(gè)方法,具體操作在類AutoConfigurationSorter中進(jìn)行的,具體方法為getInPriorityOrder():
public ListgetInPriorityOrder(Collection classNames) { AutoConfigurationClasses classes = new AutoConfigurationClasses(this.metadataReaderFactory, this.autoConfigurationMetadata, classNames); List orderedClassNames = new ArrayList<>(classNames); // Initially sort alphabetically Collections.sort(orderedClassNames); // Then sort by order orderedClassNames.sort((o1, o2) -> { int i1 = classes.get(o1).getOrder(); int i2 = classes.get(o2).getOrder(); return Integer.compare(i1, i2); }); // Then respect @AutoConfigureBefore @AutoConfigureAfter orderedClassNames = sortByAnnotation(classes, orderedClassNames); return orderedClassNames; }
首先使用order進(jìn)行排序,然后使用@AutoConfigureBefore和@AutoConfigureAfter就行排序;order其實(shí)就是通過注解@AutoConfigureOrder進(jìn)行排序的,值是一個(gè)整數(shù),結(jié)構(gòu)類似如下:
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureBefore和@AutoConfigureAfter字面意思也很好理解,指定在其他配置類之前和之后,所以可以看到在MybatisAutoConfiguration中有如下配置:
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
表示在DataSourceAutoConfiguration配置類加載之后才會(huì)加載Mybatis配置類,這樣就解決了依賴關(guān)系;還有上文提到的Mybatis操作數(shù)據(jù)庫依賴的SqlSessionFactory和SqlSession,都在MybatisAutoConfiguration進(jìn)行了初始化操作;SpringBoot本身其實(shí)以及提供了大量常用組件的自動(dòng)配置類,我們只需要提供滿足的特定條件,SpringBoot自動(dòng)會(huì)幫我加載初始化等操作,但是肯定也有自定義配置類的需求,下面用一個(gè)簡單的實(shí)例來看看如何自定義一個(gè)自動(dòng)配置類;
自定義配置類
接下來我們用很簡單的實(shí)例來看一下自定義的流程,一個(gè)格式化大寫消息的實(shí)例;
1.pom文件引入依賴
com.format format-spring-boot-starter 0.0.1-SNAPSHOT jar format-spring-boot-starter http://maven.apache.org 1.8 UTF-8 org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-dependencies 1.5.2.RELEASE pom import
Spring 官方 Starter通常命名為spring-boot-starter-{name}如 spring-boot-starter-web,Spring官方建議非官方Starter命名應(yīng)遵循{name}-spring-boot-starter的格式;
2.服務(wù)類和屬性配置類
@ConfigurationProperties("format.service") public class FormatServiceProperties { private String type; //...get/set省略... } public class FormatService { private String type; public FormatService(String type) { this.type = type; } public String wrap(String word) { if(type.equalsIgnoreCase("Upper")){//大寫 return word.toUpperCase(); }else if(type.equalsIgnoreCase("Lower")){//小寫 return word.toLowerCase(); } return word; } }
屬性類提供了type參數(shù)可以在application.properties中配置,可配置值包括:upper,lower;
3.自動(dòng)配置類和創(chuàng)建spring.factories文件
@Configuration @ConditionalOnClass(FormatService.class) @EnableConfigurationProperties(FormatServiceProperties.class) public class FormatAutoConfigure { @Autowired private FormatServiceProperties properties; @Bean @ConditionalOnMissingBean FormatService formatService() { return new FormatService(properties.getType()); } }
這個(gè)就是自定義的自動(dòng)配置類,SpringBoot啟動(dòng)的時(shí)候會(huì)根據(jù)條件自動(dòng)初始化;最后在resources/META-INF/下創(chuàng)建spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.format.FormatAutoConfigure
4.測(cè)試
在其他SpringBoot中可以引入上面創(chuàng)建的項(xiàng)目,引入方式也很簡單:
com.format format-spring-boot-starter 0.0.1-SNAPSHOT
同時(shí)在application.properties配置格式化類型:
format.service.type=upper
啟動(dòng)應(yīng)用,瀏覽器訪問http://localhost:8888/format?word=hello,結(jié)果為:HELLO
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。
上述內(nèi)容就是怎么在Spring Boot中配置Mybatis,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。