java – simpleJdbcTemplate. – Insert and retrieve IDS

I use simplejdbctemplate to put data into the database

simpleJdbcTemplate.update("insert into TABLE values(default)");

I don't want to put any data because I don't need it for my unit testing purposes

How do I get the ID from the inserted row? I can retrieve the current sequence value, but if someone else will perform the insertion, I will get the next sequence value

Is there any way to use simplejdbctemplate to insert rows and get IDS? The update method returns the number of inserted rows. I want to have ID. thank you for your help

Solution

Have you found the answer? If not, try simplejdbcinsert

SimpleJdbcInsert sji = new SimpleJdbcInsert(dataSource)
    .withTableName(TableName)
    .usingColumns(new String[]{your columns})
    .usingGeneratedKeyColumns(you auto-increment id colums);

Then retrieve

sji.executeAndReturnKey(args).longValue();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>