问题描述
我正在尝试使用Spring的JdbcTemplate类连接到基于教程。事实上,我使用了他们的项目设置:
pom.xml
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
< groupId> test< / groupId>
< artifactId> jdbc-test< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< parent>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-parent< / artifactId>
< version> 1.2.2.RELEASE< / version>
< / parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter< / artifactId>
< / dependency>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-jdbc< / artifactId>
< / dependency>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< / dependency>
< dependency>
< groupId> org.projectlombok< / groupId>
< artifactId> lombok< / artifactId>
< version> 1.14.8< / version>
< scope>提供< / scope>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-maven-plugin< / artifactId>
< / plugin>
< / plugins>
< / build>
< / project>
Lombok依赖项适用于getter和setter。
然后是Application类:
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootApplication
公共类应用程序实现CommandLineRunner {
public static void main(String args []){
SpringApplication.run(Application.class,args);
}
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(String ... arg0)throws Exception {
System.out.println(Creating tables);
jdbcTemplate.execute(如果存在则删除表客户);
jdbcTemplate.execute(create table customers(+
id serial,first_name varchar(255),last_name varchar(255)));
}
}
最后是客户pojo:
包裹测试;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Customer {
private long id;
private String firstName,lastName;
}
对于数据源配置,我的资源文件夹中有一个application.properties文件: / p>
spring.datasource.url = jdbc:mysql:// localhost:3306 / test
spring.datasource.username = test
spring.datasource.password = test
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show -sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate。 dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
数据库已启动并正在运行,但仍然无法使用autowirig。
以下是我尝试运行时遇到的异常:
org.springframework.beans.factory.BeanCreationException :创建名为'application'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory。
BeanCreationException:无法自动装配字段:org.springframework.jdbc.core.JdbcTemplate test.Application.jdbcTemplate;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:
找不到类型为[org.springframework.jdbc.core.JdbcTemplate]的限定bean依赖:预期至少有1个bean符合此依赖关系的autowire候选资格。依赖注解:
{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
。在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334 )
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537 )美元,org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476 b $ b)
在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java: 303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org。 springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication。运行(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java: 946)
at test.Application.main(Application.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source )
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner。 run(MainMethodRunner.java:53)
at java.lang.Thread.run(Unknown Source)
引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework。 jdbc.core.JdbcTemplate test.Application.jdbcTemplate;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:
找不到类型为[org.springframework.jdbc.core.JdbcTemplate]的限定bean依赖:预期至少有1个bean符合autowire
这种依赖的候选人。依赖注解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
。在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)$在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)b
$ b。在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
...省略了21个常见帧
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型[org.springframework.jdbc.core.JdbcTemplate]的限定bean用于依赖:预期至少
的1个bean有资格作为此依赖项的autowire候选者。依赖注解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
。在org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
。在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
...省略了23个常见帧
我觉得奇怪的是,如果我用com.h2数据库替换mysql-connector-java dependeny,它就可以正常工作(如在spring示例中那样)。 / p>
我是否需要配置mysql数据源以一种不同的方式?
有什么建议吗?
添加以下依赖项:
<依赖性>
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-jdbc< / artifactId>
< / dependency>
哪个会自动配置数据源
I am trying to use Spring's JdbcTemplate Class to connect to a simple MySql database based on this tutorial. In fact, I used their project setup:
pom.xml
<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>
<groupId>test</groupId>
<artifactId>jdbc-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The Lombok dependency is for getters and setters.
Then there is the Application class:
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(String... arg0) throws Exception {
System.out.println("Creating tables");
jdbcTemplate.execute("drop table customers if exists");
jdbcTemplate.execute("create table customers(" +
"id serial, first_name varchar(255), last_name varchar(255))");
}
}
And finally a Customer pojo:
package test;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Customer {
private long id;
private String firstName, lastName;
}
For datasource configuration I have an application.properties file in my resources folder:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
The database is up and running and still the autowirig dosen't seem to work.Here is the exception I get when I try to run it:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.
BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate test.Application.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at test.Application.main(Application.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate test.Application.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 21 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which
qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 23 common frames omitted
What I find curious is the fact that if I replace the mysql-connector-java dependeny with a com.h2database, it just works (as in the spring example).
Do I need to configure the mysql datasource in a dirfferent way?Any suggestions?
Add the dependency below:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Which will autoconfigure the Datasource
这篇关于如何使用Spring的JdbcTemplate连接到一个简单的MySql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!