您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Spring Boot中配置Mybatis,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
SpringBoot整合Mybatis
提供SpringBoot整合Mybatis的實例,通過Mybatis實現簡單的增刪改查功能;
1.表數據
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
提供創建role表相關的sql,對表進行增刪改查操作;
2.整合Mybatis的依賴
主要是mybatis-spring-boot-starter和使用的mysql驅動:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency>
3.配置application.properties
提供連接mysql相關的信息:url,驅動,用戶名,密碼;
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
分別提供表對應的bean類和操作數據庫的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(); } }
啟動服務,進行簡單的測試:http://localhost:8888/role?id=111
結果如下:
Role [id=111, roleName=zhaohui, note=hello]
6.提出問題
如上實例中,我們使用了很少的配置,就通過mybatis實現了操作數據庫;正常使用mybatis需要的SqlSessionFactory和SqlSession沒有看到被實例化,同時mybatis依賴的數據源也沒有看到被引用,那SpringBoot是如何幫我們自動配置的,下面重點分析一下;
SpringBoot自動配置
1.自動配置注解
要想使用自動配置功能,SpringBoot提供了注解@EnableAutoConfiguration,當然不需要我們配置因為在@SpringBootApplication注解中默認以及啟用了;
@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中重點看一下@Import注解中使用的AutoConfigurationImportSelector類,此類是自動注解的核心類,會有條件的加載我們默認指定的配置類;這里有兩個概念一個是有條件,一個是配置類,分別簡單介紹一下:配置類可以簡單理解就是相關組件對接SpringBoot的對接類,此類可以做一些初始化的工作;有條件表示并不是有配置類就能被對接上,是有條件的,SpringBoot默認提供了大量配置類,但并不是所有配置類都能被加載初始化的,是有條件的,比如mybatis在沒有數據源的情況下,沒有mybatis基礎包的情況下是不能被對接的;下面首先看一下SpringBoot提供的哪些條件類;
2.條件類
SpringBoot提供了很多條件類,可以在配置中上配置注解條件類,相關條件類可以在spring-boot-autoconfigure包下的org.springframework.boot.autoconfigure.condition下找到,主要包含如下:
ConditionalOnBean:當前容器有指定Bean的條件下;
ConditionalOnClass:當前類路徑下有指定類的條件下;
ConditionalOnCloudPlatform:當指定了云平臺的時候;
ConditionalOnExpression:SpEL表達式作為判斷條件;
ConditionalOnJava:JVM版本作為判斷條件;
ConditionalOnJndi:在JNDI存在的條件下查找指定的位置;
ConditionalOnMissingBean:當容器里沒有指定Bean的情況下;
ConditionalOnMissingClass:當類路徑下沒有指定的類的條件下;
ConditionalOnNotWebApplication:當前項目不是WEB項目的條件下;
ConditionalOnProperty:當前應用是否配置了指定屬性指定的值;
ConditionalOnResource:只有當指定的資源位于類路徑下;
ConditionalOnSingleCandidate:bean工廠中只有一個或者有多個情況下是主要的候選bean;
ConditionalOnWebApplication:當前項目是WEB項目的條件下。
以上是注解類,注解本身沒有功能,只是提供標記的功能,具體功能在@Conditional中指定的,比如ConditionalOnBean注解如下所示:
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnBeanCondition.class) public @interface ConditionalOnBean { //...省略... }
相關功能的實現就在OnBeanCondition類中,同樣其他注解類的實現類也在包org.springframework.boot.autoconfigure.condition下找到;
3.自動配置過程
Springboot應用啟動過程中使用ConfigurationClassParser分析配置類,此類中有一個processImports方法,此方法用來處理@Import注解,在@EnableAutoConfiguration注解存在@Import注解,這時候會實例化注解中的AutoConfigurationImportSelector,在其內部有一個AutoConfigurationGroup內部類,內部類有兩個核心方法分別是: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); } }
此方法主要獲取經過條件過濾之后可用的自動配置類,主要調用AutoConfigurationImportSelector中的getAutoConfigurationEntry完成的:
protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } AnnotationAttributes attributes = getAttributes(annotationMetadata); List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); configurations = removeDuplicates(configurations); Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return new AutoConfigurationEntry(configurations, exclusions); }
首先獲取了所有備選的自動配置類,然后刪除了重復和被排除的類,最后通過條件進行篩選出可用的配置類,下面分別看一下,首先看一下如何獲取所有備選的配置類:
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> 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內容:
# 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,\ //...以下省略...
當然這里只是截取了其中一個類路徑jar下的部分配置,獲取所有配置類之后進行去重,去被排除的類,然后進行條件過濾,下面重點看一下:
private List<String> filter(List<String> 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<String> 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 ,然后對之前獲取的所有配置類進行過濾,最后返回過濾之后的配置類;AutoConfigurationImportFilter同樣也是通過SpringFactoriesLoader類進行加載類路徑下META-INF/spring.factories,只不過當前的key是:org.springframework.boot.autoconfigure.AutoConfigurationImportFilter,可以看一下SpringBoot默認配置的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其實就是上文介紹的條件類,這里默認了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類的情況下才加載此配置類,而整兩個是正式Mybatis基礎包中提供的;有了基礎包還不行,還需要DataSource,而且DataSource必須在MybatisAutoConfiguration實例化之前初始化好,SpringBoot是如何實現,繼續看另外一個核心方法selectImports():
@Override public Iterable<Entry> selectImports() { if (this.autoConfigurationEntries.isEmpty()) { return Collections.emptyList(); } Set<String> allExclusions = this.autoConfigurationEntries.stream() .map(AutoConfigurationEntry::getExclusions).flatMap(Collection::stream).collect(Collectors.toSet()); Set<String> 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<String> sortAutoConfigurations(Set<String> configurations, AutoConfigurationMetadata autoConfigurationMetadata) { return new AutoConfigurationSorter(getMetadataReaderFactory(), autoConfigurationMetadata) .getInPriorityOrder(configurations); }
首先是對被排除類的一個過濾,然后接下來重點看一下對配置類進行排序的一個方法,具體操作在類AutoConfigurationSorter中進行的,具體方法為getInPriorityOrder():
public List<String> getInPriorityOrder(Collection<String> classNames) { AutoConfigurationClasses classes = new AutoConfigurationClasses(this.metadataReaderFactory, this.autoConfigurationMetadata, classNames); List<String> 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進行排序,然后使用@AutoConfigureBefore和@AutoConfigureAfter就行排序;order其實就是通過注解@AutoConfigureOrder進行排序的,值是一個整數,結構類似如下:
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureBefore和@AutoConfigureAfter字面意思也很好理解,指定在其他配置類之前和之后,所以可以看到在MybatisAutoConfiguration中有如下配置:
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
表示在DataSourceAutoConfiguration配置類加載之后才會加載Mybatis配置類,這樣就解決了依賴關系;還有上文提到的Mybatis操作數據庫依賴的SqlSessionFactory和SqlSession,都在MybatisAutoConfiguration進行了初始化操作;SpringBoot本身其實以及提供了大量常用組件的自動配置類,我們只需要提供滿足的特定條件,SpringBoot自動會幫我加載初始化等操作,但是肯定也有自定義配置類的需求,下面用一個簡單的實例來看看如何自定義一個自動配置類;
自定義配置類
接下來我們用很簡單的實例來看一下自定義的流程,一個格式化大寫消息的實例;
1.pom文件引入依賴
<groupId>com.format</groupId> <artifactId>format-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>format-spring-boot-starter</name> <url>http://maven.apache.org</url> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Spring 官方 Starter通常命名為spring-boot-starter-{name}如 spring-boot-starter-web,Spring官方建議非官方Starter命名應遵循{name}-spring-boot-starter的格式;
2.服務類和屬性配置類
@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參數可以在application.properties中配置,可配置值包括:upper,lower;
3.自動配置類和創建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()); } }
這個就是自定義的自動配置類,SpringBoot啟動的時候會根據條件自動初始化;最后在resources/META-INF/下創建spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.format.FormatAutoConfigure
4.測試
在其他SpringBoot中可以引入上面創建的項目,引入方式也很簡單:
<dependency> <groupId>com.format</groupId> <artifactId>format-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
同時在application.properties配置格式化類型:
format.service.type=upper
啟動應用,瀏覽器訪問http://localhost:8888/format?word=hello,結果為:HELLO
springboot一種全新的編程規范,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。
上述內容就是怎么在Spring Boot中配置Mybatis,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。