Intellij Idea 使用lambda表达式声明RowMapper--参数数量错误:预期为% 1,但得到% 2

neskvpey  于 2023-08-03  发布在  其他
关注(0)|答案(1)|浏览(134)

我正在学习Spring,我一直在学习这个tutorial,但是突然我开始在这个RowMapper赋值中遇到一个错误。

RowMapper<Person> rowMapper = (resultSet, i) -> {
           UUID id = UUID.fromString(resultSet.getString("id"));
           String name = resultSet.getString("name");
           return new Person(id, name);
       };

字符串
它告诉我,我在lambda表达式中使用了错误数量的参数。我的代码和视频里的一模一样。我在StackOverflow上查找了其他几个类似的问题,它们都与我所拥有的一致。我做错了什么?

5kgi1eie

5kgi1eie1#

嘿,那里,所以我也跟着视频,发现这是问题

package com.example.demo.dao;

import com.example.demo.model.Person;
//import org.flywaydb.core.internal.jdbc.JdbcTemplate; <- this is the wrong import
import org.springframework.jdbc.core.JdbcTemplate; //<- use this import (on the second line when choosing to auto import)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository("postgres")
public class PersonDataAccessService implements PersonDao{

    private final JdbcTemplate jdbcTemplate;   //the import for this

字符串
视频中的主要代码将顺利运行。

@Override
    public List<Object> selectAllPeople() {
        try {
            final String sql = "SELECT id, name FROM person";
            return jdbcTemplate.query(sql, (resultSet, i) -> {
                UUID id = UUID.fromString(resultSet.getString("id"));
                String name = resultSet.getString("name");
                return new Person(id, name);
            });
        } catch (Exception e) {
            // Handle the exception appropriately (e.g., logging, error message, etc.)
            throw new RuntimeException("Error occurred while retrieving people from the database", e);
        }
    }

相关问题