Spring JdbcTemplate Advanced Usage

Spring JdbcTemplate 的進階用法,這裡留個記錄方便之後要用可以 copy,有時也被 ORM 搞得挺煩的,小功能專案其實用 jdbc 就夠了,不用大費周章弄一堆設定…。

  1. SimpleJdbcInsert,可回傳 ID:
    JdbcTemplate template = SpringContextHolder.getBean("sysJdbcTemplate");
    impleJdbcInsert jdbcInsert = new SimpleJdbcInsert(template)
                    .withTableName("fin_payment_schedule_collection_history")
                    .usingColumns("CREATED", "SOURCEAMOUNT", "STATUS", "UPDATED", "CreatedBy", "GL_JournalLine_SID", "FinPaymentScheduleDetailSID", "sourceCurrency", "UpdatedBy")
                    .usingGeneratedKeyColumns("PaymentScheduleCollectionHistorySID");
    Map<String,Object> paramMap = Maps.newHashMap();
    paramMap.put("CREATED", created);
    paramMap.put("SOURCEAMOUNT", sourceAmount);
    paramMap.put("STATUS", status.ordinal());
    paramMap.put("UPDATED", null);
    paramMap.put("CreatedBy", createdBy.getUserSID());
    paramMap.put("GL_JournalLine_SID", gLine.getJournalLineSID());
    paramMap.put("FinPaymentScheduleDetailSID", scheduleDetail.getSid());
    paramMap.put("sourceCurrency", sourceCurrency.getSid());
    paramMap.put("UpdatedBy", null);
    Integer sid = jdbcInsert.executeAndReturnKey(paramMap).intValue();
    EMF.evictCache(getMyType());
    return findBySID(sid);
    
  2. update:
    JdbcTemplate template = SpringContextHolder.getBean("sysJdbcTemplate");
    int result = template.update("UPDATE gl_journalline SET ("
                    + "Amt2 = ?, "
                    + "Updated = ?, "
                    + "UpdatedBy = ?) "
                    + "WHERE GL_JournalLine_SID = ?", amt2, new Date(), loginUser.getUserSID(), journalLineSID);
    EMF.evictCache(GLJournalLine.class, journalLineSID);
    return result;
    

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料