public int createRolesPO(final RolesPO rolesPO) {
try {
final String sql = "insert into roles(roleName,remark,createTime,updateTime) values(?,?,?,?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
getJdbcTemplate().update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(
Connection connection) throws SQLException {
PreparedStatement preparedStatement = getJdbcTemplate()
.getDataSource()
.getConnection()
.prepareStatement(
sql,
new String[] { "roleName","remark","createTime","updateTime"});
preparedStatement.setString(1,
rolesPO.getRoleName());
preparedStatement.setString(2, rolesPO.getRemark());
preparedStatement.setTimestamp(3,
rolesPO.getCreateTime());
preparedStatement.setTimestamp(4,
rolesPO.getUpdateTime());
return preparedStatement;
}
}, keyHolder);
return keyHolder.getKey().intValue();
} catch (DataAccessException e) {
e.printStackTrace();
return 0;
}
}
spring jdbc添加数据获取自动生成的主键
最新推荐文章于 2022-08-06 10:12:05 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)