開發環境介紹:IDEA + maven + springboot2.1.4
1、用IDEA搭建SpringBoot項目:File - New - Project - Spring Initializr,(在選引用功能界面時,什么都不選)再一直Next即可,最后生成的項目結構如下:(首先記得在File - Settings - 搜索maven,將maven路徑改成你本地配置的)
然后我們在SpringBoot啟動文件Sb001Application下,啟動項目,出現 Started Sb001Application in 0.602 seconds (JVM running for 1.117) 說明項目啟動成功!
當然,現在我們項目幾乎是什么功能都沒有的,我們和springMVC一樣,搭建service、serviceImpl、dao、xml文件等,也是可以正常啟動的,
但是:當你嘗試在比如在serviceImpl 用@Autowired 注入dao層接口時,就會報錯:
Field accountDao in com.example.demo.service.impl.AccountServiceImpl required a bean of type 'com.example.demo.dao.AccountDao' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
正常情況下,這時你的dao層都是接口interface,項目啟動時,它是沒有被實現的,被注入到別的bean時,自然就會報上面的錯,如果不在別的類里面用@Autowired 注入dao層有關接口時,啟動也不會報錯!
現在我們通過整合mybatis來實現dao層的接口,來實現數據庫的連接:
第一步:引入jia包
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
Tips:如果嘗試在引入mybatis后,不做任何配置,也就是不在yml文件配置數據庫連接信息,直接啟動項目,發現會報錯:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
后來通過百度終於找到答案:spring boot啟動時默認會加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類,DataSourceAutoConfiguration類使用了@Configuration注解向spring注入了dataSource bean。因為工程中沒有關於dataSource相關的配置信息,當spring創建dataSource bean因缺少相關的信息就會報錯。
解決辦法:
(1)將@SpringBootApplication注解改成@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}),這種辦法可以解決上面的報錯,但是會跟我們想配置mybatis的原意走遠,不推薦【但是,提一句,在配置多數據源時,也是這樣配置的】
(2)第二種辦法就是配置數據庫了,這里首先將系統resources下默認生成的properties文件刪掉,新建application.yml文件(別問我為什么要用yml,因為的確好用^_^),並且在里面加入數據源的配置
spring:
datasource:
url: jdbc:mysql://172.0.0.0:3306/你的數據庫名稱?serverTimezone=GMT%2B8
username: 賬號
password: 密碼
#driver-class-name: com.mysql.cj.jdbc.Driver #根據mysql版本選擇,一般都是可以的
driver-class-name: com.mysql.jdbc.Driver
配置了數據源,接下來就是連接數據庫了,那么就要引入連接數據庫的Jar包:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
配置到這個時候,項目是可以正常啟動的,但是當你再次嘗試在serviceImpl通過@Autowired 注入dao層的接口時,它又會報bean of type 'com.example.demo.dao.AccountDao' that could not be found.同樣的錯,原因上面也解釋過,接口無法實現,,,
那么如何配置讓mybatis實現我們dao層的接口呢?
那就是在啟動文件新加 @MapperScan("com.example.demo.dao"),里面是項目dao包的路徑,一定不能錯!!!,也可以@MapperScan("com.example.demo.*.dao")這種;現在我們在serviceImpl通過@Autowired 注入dao層的接口時,項目啟動就正常了!
現在我們去test目錄下,創建一個測試類,寫一個最簡單的方法getById,當然mybatis是需要在xml里面寫自己sql的,xml的存放路徑我們不會用系統默認的,現在我們指定放在resources下的mybatis包下,新建完mybatis包后,還要在yml文件配置
mybatis:
mapper-locations: classpath:mybatis/**/*.xml
到這里,測試類就可以正常運行,可以連接數據庫讀取數據了!自此,最簡單的配置spring boot + mybatis 就整合完成了!最后我們對照開始的圖對比一下
最后,附上項目完整的pom文件配置(是最簡版的,能保證連接數據庫):

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sb001</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 上面兩個是 零配置 springboot 都會自帶生成的 --> <!-- 整合mybatis的最簡配置,web都是可以不用的 --> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>--> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!-- 連接Mysql數據庫,別的數據庫的要對應改 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
和完整的最簡的yml文件配置:

spring:
datasource:
url: jdbc:mysql://127.0.0.0:3306/testdb?serverTimezone=GMT%2B8
username: admin
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mybatis/**/*.xml
雖然用SpringBoot項目很長時間了,當同事“乞丐式”配置一個最簡項目時,遇到各種報錯,解決的過程中還是可以學到很多點的,特意記錄下來,希望可以幫到一些新手,特別是里面兩個錯誤的主要原因,估計很多人都不一定清楚,如果筆者理解有不正確的地方,也請指正!
Tips:
(1)會的不難,不會就難
(2)學就會,不學就不會
筆於2019-04-24,月底將結束畢業后工作長達兩年的第一家公司WW,特此記錄