2017-08-24 14 views
2

如果我有一個Java Date,並且使用JDBCTEmplate,我想在準備好的語句中使用它,這是正確的語法嗎?如何在Spring Prepared Statement中傳遞Java日期

Date x = new Date(); 

... 

String SQL = "select a.1, a.2 from b JOIN a ON b.id = a.b_id where b.name = ? and a.type = ? and a.date = ?"; 

A istance = jdbcTemplate.queryForObject(SQL, new Object[]{variable_1, variable_2, x}, new aAndbMapper()); 

或者我應該寫:

String SQL = "select a.1, a.2 from b JOIN a ON b.id = a.b_id where b.name = ? and a.type = ? and DATE(a.date) = ?"; 
+2

第一個是我的更好的解決方案 – Jens

+0

使用'java.sql.Date'類的對象作爲參數 – Zico

回答

1

第一個是一個不錯的選擇,但我會做沒有自定義映射:

String SQL = "select a.1, a.2 from b JOIN a ON b.id = a.b_id where b.name = ? and a.type = ? and a.date = ?"; 

A istance = getJdbcTemplate().queryForObject(SQL, new Object[]{variable_1, variable_2, x}, new BeanPropertyRowMapper(A.class)); 

更多beanPropertymapper: Documentation

相關問題