我正在开发我的第一个Spring Boot应用程序,我遇到了数据库连接问题。
我正在使用maven 4.0.0,我想使用JdbcTemplate在数据库(H2)上运行。我尝试在application.properties文件中配置数据库访问,该文件是我在resources文件夹中创建的。
这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>obudb2</groupId>
<artifactId>boudb2</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <!--todo: change to starter-jdbc!-->
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.0.60</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我在以下dao类中访问数据库:
@Repository
public class CustomerDaoImpl implements CustomerDao {
private String selectAllStatement = "select * from Customer";
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Customer> findAllCustomers() {
return jdbcTemplate.query(selectAllStatement,new CustomerMapper());
}
}
我的application.properties文件如下所示:
spring.datasource.url=jdbc:h2:mem:sourceDb
spring.datasource.username=admin
spring.datasource.driver-class-name=org.h2.Driver
我收到以下错误:
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from CUSTOMER]; nested exception is org.h2.jdbc.JdbcSQLException: Tabelle CUSTOMER nicht gefunden
Table CUSTOMER not found; SQL statement:
select * from CUSTOMER [42S02-60]
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:388) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456) ~[spring-jdbc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at dao.CustomerDaoImpl.findAllCustomers(CustomerDaoImpl.java:25) ~[classes/:na]
at dao.CustomerDaoImpl$$FastClassBySpringCGLIB$$c0781898.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747) ~[spring-aop-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) ~[spring-aop-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at dao.CustomerDaoImpl$$EnhancerBySpringCGLIB$$da434872.findAllCustomers(<generated>) ~[classes/:na]
at service.TransferService.transferData(TransferService.java:26) ~[classes/:na]
at main.ObuDbTransfer.run(ObuDbTransfer.java:34) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
我认为数据源可能存在问题。也许它没有正确链接,但是根据我的理解,spring boot应该自动生成application.properties文件中的数据源。因此,我认为这里可能出现的问题。 我希望有人能指出我在这里错过的东西。
答案 0 :(得分:0)
spring.datasource.url = jdbc:h2:mem:testdb 将此条目添加到application.properties文件中。