首先、創建項目;
在這裏選擇Maven項目也可以,但是IDEA爲我們提供了一種更方便快捷的創建方法,即Spring Initializr。可以在這裏選擇Spring Initializr,選擇後點擊Next
next後就是填寫項目信息,之後再next
next後跳轉到了選擇依賴的界面,我這邊以創建web項目爲例,數據庫用MySQL、持久層框架是mybatis,依賴選擇完成後點擊next
next跳轉到填寫項目名和項目本地地址的頁面,填寫完後,點擊finish 來進行創建項目,但是需要注意的是創建springboot項目需要保證本機網絡的暢通,因爲創建springboot項目需要下載大量的依賴
項目創建完成,如下目錄,
現在可以直接啓動項目了,點擊這個main方法就可以了
奧,漂亮,你會發現項目啓動報錯了
錯誤報文:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.5.RELEASE)
2019-05-22 20:58:47.981 INFO 17788 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-6MD93CN with PID 17788 (D:\java\springboot-project\demo\target\classes started by Mr.Yu in D:\java\springboot-project\demo)
2019-05-22 20:58:47.983 INFO 17788 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-05-22 20:58:48.509 WARN 17788 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.
2019-05-22 20:58:48.882 INFO 17788 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-05-22 20:58:48.898 INFO 17788 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-05-22 20:58:48.899 INFO 17788 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-05-22 20:58:48.990 INFO 17788 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-05-22 20:58:48.991 INFO 17788 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 977 ms
2019-05-22 20:58:49.153 INFO 17788 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-22 20:58:49.222 WARN 17788 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2019-05-22 20:58:49.222 INFO 17788 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-05-22 20:58:49.225 INFO 17788 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-05-22 20:58:49.235 INFO 17788 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-22 20:58:49.240 ERROR 17788 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
這是因爲新建的項目,選擇了數據源的依賴後,在項目啓動的時候 會去尋找項目的數據源配置,沒找到而報錯了,在你的這個啓動類上設置這行代碼,來排除數據源信息自動配置
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
然後我們再次啓動項目,發現歐克了