0

I have this series of code that takes data from database and then mapping it to a class.

The class is as follows (I didn't put the getter/setter to save space. Pretend that we already added the getter/setter).

public class MyClass{
    private int id;
    private String className;
    private String classGroup;
}

And how I call my query is like this:

public MyClass getMyClass(String cn, String cg){
    String query = "Select className, classGroup from MyClass where className = :className AND classGroup = :classGroup AND RowNum = 1"; //Imagine we're using Oracle SQL. If we use SQL Server/MySQL, then just replace AND RowNum = 1' with 'LIMIT 1'
    return jdbcTemplate.query(query, newObject[]{cn,cg}, this::mapRow);
}

public MyClass mapRow (ResultSet rs, int i) throws SQLException{
    MyClass mc = new MyClass();
    mc.setClassName(rs.getString("className"));
    mc.setClassGroup(rs.getString("groupName"));
    return mc;
}

In general, it works. However, the jdbcTemplate.query command is deprecated. I do not know what's the alternative for scenarios like this: multiple variable within the query, returning a class/entity, sends the ResultSet to a function. So, can anyone help with the replacement for this so it's no longer deprecated?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kristian -
  • 53
  • 3
  • Have you read the [documentation](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#query-java.lang.String-java.lang.Object:A-org.springframework.jdbc.core.RowMapper-)? This directs you to the equivalent method in `JdbcOperations.query`, which specifies: _Deprecated. as of 5.3, in favor of [`query(String, RowMapper, Object...)`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcOperations.html#query-java.lang.String-org.springframework.jdbc.core.RowMapper-java.lang.Object...-)_ – Mark Rotteveel Sep 02 '22 at 12:52

0 Answers0