Implementing a Common Type Interface

I am trying to implement the Spring RowMapper interface, however my IDE offers me to return the return object to "T", and I do not understand why. Can anyone explain what I am missing?

public class UserMapper<T> implements RowMapper<T> {
    public T mapRow(ResultSet rs, int row) throws SQLException {
        User user = new User();
        user.firstName(rs.getInt("fname"));
        user.lastName(rs.getFloat("lname"));
        return user; // Why am I being prompted to cast this to "T", should this be fine?
    }
}
+3
source share
4 answers

If the string maps to the user, then it should be RowMapper<User>

t


public class UserMapper implements RowMapper<User> {
    public User mapRow(ResultSet rs, int row) throws SQLException {
        User user = new User();
        user.firstName(rs.getInt("fname"));
        user.lastName(rs.getFloat("lname"));
        return user;
    }
}
+10
source

Try

public class UserMapper implements RowMapper<User> {
+3
source

T. User T. T User, , .

public class UserMapper<T extends User> implements RowMapper<T>
...

, User, T. User, T.

+2

T!= .

0

Source: https://habr.com/ru/post/1768480/


All Articles