0

我需要执行以下查询并获取列表对象。各位大佬知道怎么取回最好spring jdbc吗?

这是 SQL 查询:

SELECT app_name, error_code, error_message FROM error_message
WHERE ( 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) 
)
4

1 回答 1

1

这是我的示例代码:

String sql = yoursql;

Object args[] = new Object[3];
args[0] = yourparam1;          
args[1] = yourparam2;          
args[2] = yourparam3;

getJdbcTemplate().query(sql, args, yourRowMapper);

您应该为 rowmapper 添加以下代码。

private RowMapper yourRowMapper= new YourRowMapperClass ();

public final class YourRowMapperClass implements RowMapper {
   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

      YourResponse model = new YourResponse ();
      model.setresp1(rs.getDate(1));
      model.setresp2(rs.getDate(2));
      model.setresp3(rs.getString(3));

      return model;
    }
  }

顺序对于指定参数很重要。例如,如果我们在 sql 中两次使用 searchType 参数,我们应该定义如下参数:

Object args[] = new Object[4];
args[0] = searchType;
args[1] = param1;
args[2] = searchType;
args[3] = param2;

如果我们为您定制示例,您可以使用以下代码:

Object args[] = new Object[10];
args[0] = appname;
args[1] = errorcode;
args[2] = appname;
args[3] = errorcode;
args[4] = appname;
args[5] = errorcode;
args[6] = appname;
args[7] = errorcode;
args[8] = appname;
args[9] = errorcode;
于 2013-03-01T18:19:42.493 回答