java - How to get ID created from Oracle trigger in MyBatis - Spring Boot
问题描述
I've seen a couple similar questions, however, I just can't get the correct ID value on my code. It's always returning 1 as the ID after insert.
ItemController.java
@Operation(description = "Insert Test")
@PostMapping("/item/add")
@ResponseBody
void upsertTest(
@RequestBody itemForm testForm
) {
Long itemId = itemService.getItemId();
itemForm.setItemId(itemId);
System.out.println("Item ID: " + itemId );
}
ItemService.java
@Service("ItemService")
public class ItemService {
private final ItemDao itemDao;
@Autowired
public ItemService(@Qualifier("itemDao") final ItemDao itemDao) {
this.itemDao = itemDao;
}
public Long getItemId(){
return itemDao.getItemId();
}
}
ItemDao.java
@Component("itemDao")
@Repository
public interface ItemDao {
Long getItemId();
}
ItemDao.xml
<insert id="getItemId" useGeneratedKeys="true" keyProperty="itemId" keyColumn="ITEM_ID">
INSERT INTO ITEM_ID_TABLE (DUMMY) VALUES (null)
</insert>
And my table is basically 2 columns (ITEM_ID, DUMMY), and has a trigger to insert an ID using a sequence on the ITEM_ID column.
Whenever I do an insert output is:
Item ID: 1
Even when I've done multiple inserts already
解决方案
在 ItemDao.xml 尝试这样的事情:
<insert id="getItemId" useGeneratedKeys="true" keyProperty="itemId" keyColumn="ITEM_ID">
INSERT INTO ITEM_ID_TABLE (DUMMY) VALUES (null)
<selectKey keyProperty="returnId" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID();
</selectKey>
</insert>
基于这个问题。
推荐阅读
- ios - 向下滚动时带有不需要的额外对象的 UITableViewCell
- git - git安装失败,Windows 10,CryptStringToBinaryW
- google-cloud-platform - 谷歌云:/Users/Me/.zshrc:8: unmatched '
- visual-studio - 为什么每次按下工具栏按钮时我的 Visual Studio 都会崩溃?
- powershell - PowerShell:如何只返回一个 [PSCustomObject] 而不是其中包含其他内容的数组?
- r - 如何在 Windows 10 上为 R 创建用户库?
- javascript - 点击按钮后如何调用函数并显示结果
- amazon-web-services - aws chime deploy 错误:无法创建/更新堆栈:堆栈名称服务员 StackCreateComplete 失败:服务员遇到终端故障状态
- python-3.x - 将时间序列数据剪裁到最近的小时或最近的一天
- python - 导入具有相同命名函数的多个模块而不会发生命名空间冲突