How to Sort DB records in DB table with JdbcTemplate?
What is the best solution, should execute method be used?
How to Sort DB records in DB table with JdbcTemplate?
What is the best solution, should execute method be used?
データベーステーブル内のデータは順序付けされていないと見なす必要があります。特定の順序でデータを選択できます。また、JdbcTemplateではなくSimpleJdbcTemplateを使用します。同じメソッドを使用できますSimpleJdbcTemplate.getJdbcOperations()
。
たとえば、このコードスニペットは、文字列であると仮定して、column1のすべての値の順序付きリストを提供します。
final SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(datasource);
final List<String> data = jdbcTemplate.query("SELECT column1 FROM MyTable ORDER BY column1 ASC", new ParameterizedSingleColumnRowMapper<String>());
JdbcTemplate
メソッドで指定した SQL を実行するだけexecute
なので、標準の SQL メソッドを使用します。ORDER BY
JdbcTemplate はそれらに付随していますが、いくつかの方法があります。1 つ目は、クエリに「order by」句を含めることです。それ以外の場合は、呼び出しで返されたコレクションの種類を並べ替えようとしています。