新增数据 返回自增主键ID
public int add(Map<String,Object> paramMap) { long nowTime = System.currentTimeMillis(); // 当前时间 String sql = "insert into " + tableName + " (device_no,device_key,device_type,channel_type_id,phone_type,is_attent,serial_no,member_id,sub_member_id,cashier_id,update_time,c_date,state,source_id,ios_type) " + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplateNotify.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement preparedStatement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); preparedStatement.setString(1, ComMapUtils.getString(paramMap,"deviceNo")); preparedStatement.setString(2, ComMapUtils.getString(paramMap,"deviceKey")); preparedStatement.setString(3, ComMapUtils.getString(paramMap,"deviceType")); preparedStatement.setInt(4, ComMapUtils.getInteger(paramMap,"channelTypeId")); preparedStatement.setInt(5, ComMapUtils.getInteger(paramMap,"phoneType")); preparedStatement.setInt(6, ComMapUtils.getInteger(paramMap,"isAttent")); preparedStatement.setInt(7, ComMapUtils.getInteger(paramMap,"serialNo")); preparedStatement.setInt(8, ComMapUtils.getInteger(paramMap,"memberId")); preparedStatement.setInt(9, ComMapUtils.getInteger(paramMap,"subMemberId")); preparedStatement.setInt(10, ComMapUtils.getInteger(paramMap,"cashierId")); preparedStatement.setLong(11, nowTime); preparedStatement.setLong(12, nowTime); preparedStatement.setInt(13, ComMapUtils.getInteger(paramMap,"state")); preparedStatement.setInt(14, ComMapUtils.getInteger(paramMap,"sourcId")); preparedStatement.setInt(15, ComMapUtils.getInteger(paramMap,"iosType")); return preparedStatement; } }, keyHolder); int id = keyHolder.getKey().intValue(); return id; }
con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
原文:https://www.cnblogs.com/gjq1126-web/p/13959029.html