SpringBoot配置mysql数据源错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-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).
 [main] 
Disconnected from the target VM, address: '127.0.0.1:50832', transport: 'socket'

Process finished with exit code 1

Mysql数据源

@Configuration
@MapperScan(basePackages = {"com.znv.dao.mapper"},sqlSessionFactoryRef = "MySqlSessionFactory")
public class MysqlDatasource {

    /**
     * 数据源
     * @return
     */
    @Bean(name = "primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.mysql")
    public javax.sql.DataSource dataSource() {
        //考虑如何使用druid的数据源
        return new org.apache.tomcat.jdbc.pool.DataSource();
        //return DataSourceBuilder.create().build();
        //return new DruidDataSource();
    }

    /**
     * sessionFactory
     * @return
     * @throws Exception
     */
    @Bean(name = "MySqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver
        .getResources("classpath:/mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 事务管理
     * @return
     */
    @Bean(name = "mysqlPlatformTransactionManager")
    @Primary
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

springBoot版本2.0.0.Release

如果使用@SpringBootApplication注解的方式会报上面的错误。

@SpringBootApplication
public class Application {
    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
}

解决方法1:换注解方式

@ComponentScan("com.znv")
@EnableAutoConfiguration()
@EnableScheduling
//@SpringBootApplication
public class Application {
    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
}


解决方法2:排除数据源类

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class Application {
    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
}

运行成功 

2018-08-13 17:03:56 INFO  com.znv.application.Application - Starting Application on LAPTOP-4NKCBKCL with PID 35640 (D:\01-znv-r\svn_project\ZNVR_ShangHai\R_AccessServer\AlarmSimulator\target\classes started by 28956 in D:\01-znv-r\svn_project\ZNVR_ShangHai\R_AccessServer\AlarmSimulator) [main] 
2018-08-13 17:03:56 INFO  com.znv.application.Application - No active profile set, falling back to default profiles: default [main] 
2018-08-13 17:03:57 INFO  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2812b107: startup date [Mon Aug 13 17:03:57 SGT 2018]; root of context hierarchy [main] 
2018-08-13 17:04:03 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8088 (http) [main] 
2018-08-13 17:04:03 INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8088"] [main] 
2018-08-13 17:04:03 INFO  o.a.catalina.core.StandardService - Starting service [Tomcat] [main] 
2018-08-13 17:04:03 INFO  o.a.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.28 [main] 
2018-08-13 17:04:03 INFO  o.a.c.core.AprLifecycleListener - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_91\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Java\jdk1.8.0_91\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;D:\software\webserver\apache-maven-3.5.0\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\nodejs\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Users\28956\AppData\Local\Microsoft\WindowsApps;C:\Users\28956\AppData\Roaming\npm;;.] [localhost-startStop-1] 
2018-08-13 17:04:03 INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext [localhost-startStop-1] 
2018-08-13 17:04:03 INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 6753 ms [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.ServletRegistrationBean - Servlet dispatcherServlet mapped to [/] [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*] [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*] [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*] [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*] [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'httpTraceFilter' to: [/*] [localhost-startStop-1] 
2018-08-13 17:04:05 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'webMvcMetricsFilter' to: [/*] [localhost-startStop-1] 
2018-08-13 17:04:06 INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2812b107: startup date [Mon Aug 13 17:03:57 SGT 2018]; root of context hierarchy [main] 
2018-08-13 17:04:06 INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) [main] 
2018-08-13 17:04:06 INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) [main] 
2018-08-13 17:04:07 INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] [main] 
2018-08-13 17:04:07 INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] [main] 
2018-08-13 17:04:07 INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] [main] 
2018-08-13 17:04:08 INFO  o.s.b.a.e.w.s.WebMvcEndpointHandlerMapping - Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>) [main] 
2018-08-13 17:04:08 INFO  o.s.b.a.e.w.s.WebMvcEndpointHandlerMapping - Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>) [main] 
2018-08-13 17:04:08 INFO  o.s.b.a.e.w.s.WebMvcEndpointHandlerMapping - Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) [main] 
2018-08-13 17:04:08 INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup [main] 
2018-08-13 17:04:08 INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8088"] [main] 
2018-08-13 17:04:09 INFO  o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read [main] 
2018-08-13 17:04:09 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8088 (http) with context path '' [main] 
2018-08-13 17:04:09 INFO  com.znv.application.Application - Started Application in 15.373 seconds (JVM running for 17.208) [main] 

猜你喜欢

转载自blog.csdn.net/mhm52368/article/details/81633081