java - JdbcTemplate 不适用于 Spring Boot 项目
问题描述
我正在使用 jdk8 并从事 SpringBoot 项目。我得到“JdbcTemplate 无法解析为一种类型”,我尝试 maven->update 项目,更新项目后,@Repository 注释中出现错误并要求更新 JDKto1.5。JdbcTemplate 中的错误已经消失,但无法使用任何方法使用其参考。请帮助!
package com.boot.rest.repository;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.boot.rest.model.User;
@Repository
public class UserService {
@Autowired
JdbcTemplate jdbcTemplate;
public List<String> getUSerNames(){
List<String> userlist = new ArrayList<String>();
userlist.addAll(jdbcTemplate.queryForList("Select username from user"));
return userlist;
}
}
解决方案
QueryForListList<Map<String,Object>>
不返回List<String>
。Spring 抛出异常,因为您处理了错误的类型。这里的例子:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#queryForList-java.lang.String-
和https://www.mkyong.com/spring/spring-jdbctemplate-querying-examples/
推荐阅读
- c++ - 如何将 int 和 int* 传递给函数来定义变量
- amazon-web-services - AWS 通过公共 IP 查找实例
- java - 如何实现 Java 应用程序数据/状态的保存/加载功能?
- javascript - 如何将 javascript 中的 2 个值解析为 2 个输入框?
- python - 如何知道字符串是数字还是浮点数
- javascript - 如何计算日期和更改日期格式
- node.js - 将订单与产品关联
- azure-language-understanding - 无法使用“:”在 LUIS 中添加新意图
- oracle-adf - 如何在 Chrome 73.0 中的 af:inputText 中设置边框?
- r - R - 如何从“扩展”矩阵中获取每列的系数〜时间线?