You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RowMapper非泛型类无法参数化<User> 代码报错咨询

Fix: "RowMapper is not generic; cannot be parameterized with " Error

Hey there! Let's get this error sorted out—it's usually a simple fix tied to imports or class references. I’ve dealt with this exact issue when working with Spring JDBC, so let’s break down what’s going on and how to fix it.

Why This Happens

The error pops up because you’re trying to use a non-generic version of RowMapper (most likely the old JDBC java.sql.RowMapper interface) instead of the generic Spring JDBC RowMapper that supports type parameters like <User>.

Step-by-Step Solution

1. Verify Your Import Statement

First, double-check the import at the top of your class. You need the Spring JDBC version of RowMapper, not the standard JDBC one:

// Correct import (supports generics)
import org.springframework.jdbc.core.RowMapper;

// Wrong import (non-generic, causes the error)
// import java.sql.RowMapper;

2. Fix Your UserMapper Implementation

Once you’ve got the right import, make sure your internal class correctly implements the generic RowMapper interface. Here’s the corrected version of your code snippet:

private static final class UserMapper implements RowMapper<User> {
    @Override
    public User mapRow(ResultSet rs, int rowNum) throws SQLException {
        User user = new User();
        // Don't forget to populate the user object with ResultSet data!
        // Example: user.setId(rs.getInt("id"));
        // user.setName(rs.getString("name"));
        return user;
    }
}

Note: I added the @Override annotation to ensure the method signature matches the generic interface—this helps catch typos or mismatches early.

3. Double-Check Spring JDBC Dependency (If Needed)

If you’re still seeing the error after fixing the import, confirm your project uses a recent enough Spring JDBC version. All modern Spring releases (3.0+) include the generic RowMapper interface, so this is rarely an issue—but it’s worth checking if you’re working with an older codebase.

Quick Recap

The core issue is almost always using the wrong RowMapper class. Switching to the Spring JDBC version and ensuring your implementation aligns with the generic interface will resolve the error.

内容的提问来源于stack exchange,提问作者user9689727

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:57:02