2017-09-02 85 views
1

我正在關注訪問數據庫的Java SpringMVC tutorial。按照指示建立的一切後,我正在運行的命令mvn spring-boot:runJava SpringMVC如何訪問MySQL數據庫

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

Description: 

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found. 
     - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name' 
     - Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans 


Action: 

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration. 

[WARNING] 
java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:497) 
     at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) 
     at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 
     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 
     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
     at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 
     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) 
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) 
     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) 
     at hello.Application.main(Application.java:15) 
     ... 6 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 
     at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) 
     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) 
     ... 34 more 

的代碼從教程頁面匹配我的項目的代碼後發現了以下錯誤。以下是我的src/main/resources/application.properties文件中的設置。

spring.jpa.hibernate.ddl-auto=update 
spring.datasource.url=jdbc:mysql://localhost:3306/java-accessing-db 
spring.datasource.username=root 
spring.datasource.password= 

我對SpringMVC框架和Java很新穎。所以我不知道是什麼導致了這個問題。我認爲我的驅動程序設置不正確,或者我錯過了一個依賴關係。

+0

你有沒有安裝mysql服務器? –

+0

是的,我安裝了MySQL服務器。我在安裝了XAMPP apache的本地Windows機器上運行該程序。 – LogicDev

+0

看起來你缺少一個DAO。你使用哪個IDE? –

回答

1

首先添加屬性文件中的方言

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 

我不知道現在該問題是否this.I認爲你只是錯過設置bean.So檢查DAO類和資源類,也許你忘了@Autowired其中一些。 另外請記住,春季資料使用存儲庫檢查這裏Working with SQL databases 檢查那些3我相信你會找到它,我會很樂意提供幫助。 這是您在JEE透視圖中獲得的Spring-Elements樹enter image description here如果您可以發佈此信息,它會有所幫助。

+0

嗨,謝謝!這實際上工作!雖然我不是這樣,它是否會一直工作,爲什麼它工作,爲什麼spring.io不包括在他們的教程中。我正在使用Eclipse Java Neon.2 Release(4.6.2)希望我能理解一個bean是什麼以及如何在不久的將來設置它。我也會看看@Autowired。類,資源和存儲庫我從其他語言的工作中瞭解到, 我是否應該將Eclipse JEE Neon用於MVC項目? – LogicDev

+0

我也在使用Eclipse。我認爲這是與intelliJ最好的工具。我也相信春季教程幾乎完美,非常直接。可能他們提到這個地方,你沒有檢查。雖然很樂意幫助雖然。Goodnight ! –