通过idea创建spring-boot、mybatis、MySQL的web项目

    xiaoxiao2022-07-07  184

    首先、创建项目;

    在这里选择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)

    然后我们再次启动项目,发现欧克了

    最新回复(0)