Created
May 22, 2014 06:58
-
-
Save shautzin/29250a93847b5e69a814 to your computer and use it in GitHub Desktop.
JdbcTemplate generic Mysql Primary key value by update method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 插入一条记录 | |
* | |
* @param columns | |
* @param values | |
* @return | |
*/ | |
public long insert(String[] columns, Object[] values) { | |
String sql = insertSql(columns); | |
KeyHolder keyHolder = new GeneratedKeyHolder(); | |
getJdbcTemplate().update(x -> { | |
PreparedStatement ps = getJdbcTemplate().getDataSource().getConnection() | |
.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); | |
for (int i = 0; i < values.length; i++) { | |
ps.setObject(i+1, values[i]); | |
} | |
return ps; | |
}, keyHolder); | |
return keyHolder.getKey().longValue(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment