| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
ScaNIV
9年前发布

spring jdbcTemplate 插入对象返回主键值

public int addClifford(Clifford clifford) {      String sql = "insert into t_user_clifford(mobile,last_activity_date,clifford_count,is_share)values(?,?,?,?)";      int idResult = 0;      KeyHolder keyHolder = new GeneratedKeyHolder();      jdbcTemplateActivityWrite.update(new PreparedStatementCreator() {          @Override          public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {              PreparedStatement ps = connection.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);              ps.setObject(1,clifford.getMobile());              ps.setObject(2,clifford.getLastActivityDate());              ps.setObject(3,clifford.getCliffordCount());              ps.setObject(4,clifford.getIsShare());              return ps;          }      },keyHolder);      idResult = keyHolder.getKey().intValue();      return idResult;  }