首页 > 解决方案 > 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;


    }
}

标签: javaspringspring-bootjdbctemplate

解决方案


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/


推荐阅读