我编写了一个简单的待办事项应用程序(基于apress pro spring boot的内容,其中我们使用jdbc处理数据库,在存储库中,我们有以下代码处理h2数据库
package com.apress.todo.repository;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import com.apress.todo.domain.ToDo;
@Repository
public class ToDoRepository implements CommonRepository<ToDo> {
private static final String SQL_INSERT ="insert into todo (id,\r\n" +
"description, created, modified, completed) values (:id,:description,\r\n" +
":created,:modified,:completed)";
private static final String SQL_QUERY_FIND_ALL = "select id,"
+"description, created, modified, completed from todo";
private static final String SQL_QUERY_FIND_BY_ID = SQL_QUERY_FIND_ALL +
" where id = :id";
private static final String SQL_UPDATE = "update todo set description ="
+":description, modified = :modified, completed = :completed"
+"where id = :id";
private static final String SQL_DELETE = "delete from todo where id = :id";
private final NamedParameterJdbcTemplate jdbcTemplate;
public ToDoRepository(NamedParameterJdbcTemplate jdbcTemplate) {//HERE IS WHERE THE PROBLEM IS
System.out.println("hi masoud");
this.jdbcTemplate = jdbcTemplate;
}
private RowMapper<ToDo> toDoRowMapper = (ResultSet rs, int rowNum) -> {
ToDo toDo = new ToDo();
toDo.setId(rs.getString("id"));
toDo.setDescription(rs.getString("description"));
toDo.setModified(rs.getTimestamp("modified").toLocalDateTime());
toDo.setCreated(rs.getTimestamp("created").toLocalDateTime());
toDo.setCompleted(rs.getBoolean("completed"));
return toDo;
};
以下是我的主要Spring Boot应用程序运行程序
package com.apress.todo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TodoInMemoryApplication {
public static void main(String[] args) {
SpringApplication.run(TodoInMemoryApplication.class, args);
}
}
但是当我使用spring boot命令行界面运行应用程序时,将出现以下错误:说明:
Parameter 0 of constructor in com.apress.todo.repository.ToDoRepository required a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' in your configuration.
书中没有更多解释。
private final NamedParameterJdbcTemplate jdbcTemplate;
。您希望以某种方式将其初始化。为此,必须在@Bean
类中用NamedParameterJdbcTemplate
类型声明@Configuration
或创建一个扩展@Component
的NamedParameterJdbcTemplate
类。或扩展NamedParameterJdbcDaoSupport
并通过getNamedParameterJdbcTemplate();
实际上是您的示例,加上spring-boot-starter-jdbc
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
并添加属性,例如
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=
应根据tutorial解决此问题
private final NamedParameterJdbcTemplate jdbcTemplate;
。您希望以某种方式将其初始化。为此,必须在@Bean
类中用NamedParameterJdbcTemplate
类型声明@Configuration
或创建一个扩展@Component
的NamedParameterJdbcTemplate
类。或扩展NamedParameterJdbcDaoSupport
并通过getNamedParameterJdbcTemplate();
实际上是您的示例,加上spring-boot-starter-jdbc
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
并添加属性,例如
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=
应根据tutorial解决此问题