原來我接近,但忘了一步。
我不得不更改查詢本身:
BEGIN
UPDATE jasper_report SET Uri = ? WHERE ReportId = ?
IF @@ROWCOUNT = 0 AND EXISTS(SELECT 1 FROM report WHERE Id = ?)
BEGIN
INSERT INTO jasper_report (ReportId, Uri) VALUES (?, ?)
END
END
然後在我的道只需要使用Spring's JdbcTemplate update method。它看起來是這樣的:
@Repository("jasperReportsDao")
public class JasperReportsDaoImpl extends JdbcTemplate implements JasperReportsDao {
@Override
public void saveJasperReport(JasperReport report) {
// If a record already exists, do an update, otherwise, do an insert
int rowsAffected = this.update(UPSERT_JASPER_REPORT, new Object[] { report.getUri(), report.getId(),
report.getId(), report.getId(), report.getUri()});
if(log.isDebugEnabled()) { log.debug("Rows affected: " + rowsAffected); }
}
}
完全相同的副本 - http://stackoverflow.com/questions/2104211/spring-jdbc-template-query-execution – 2010-01-20 19:23:35
不留神發佈兩次,DUP已被刪除 – 2010-01-20 20:20:49