【发布时间】:2012-03-10 04:30:30
【问题描述】:
我正在尝试在 MySQL 表中插入一行并获取它的插入 ID。我知道 MySQL last_insert_id() 函数,但我似乎无法让它工作。目前,我正在尝试使用一个注释为事务的函数,但我只返回 0。我正在使用 Spring 3.1。
@Transactional (propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
private long insertTransactionRecord
(
int custID,
int porID,
String date,
short crvID
) {
m_template.update ("INSERT INTO " +
" transaction " +
"( " +
" por_id, " +
" cust_id, " +
" trans_date, " +
" crv_id " +
") " +
"VALUES " +
"( " +
" ?, " +
" ?, " +
" ?, " +
" ? " +
")",
new Object[] {
porID,
custID,
date,
crvID
});
return m_template.queryForLong ("SELECT " +
" last_insert_id() " +
"FROM " +
" transaction " +
"LIMIT 1");
}
【问题讨论】: