简体   繁体   English

如何返回生成的 UUID 主键

[英]How to return generated UUID primary key

I am working with mySQL database and the table has a primary key in UUID format, generated by the database engine.我正在使用 mySQL 数据库,并且该表有一个由数据库引擎生成的 UUID 格式的主键。 I am using Spring framework JdbcTemplate ( https://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/jdbc/core/JdbcTemplate.html ) to perform all operation.我正在使用 Spring 框架JdbcTemplate ( https://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/jdbc/core/JdbcTemplate.html ) 来执行所有操作。 But on inserts I do need to return primary key to the caller.但是在插入时,我确实需要将主键返回给调用者。 JdbcTemplate has method public int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder) but it returns integer keys only. JdbcTemplate有方法public int update(PreparedStatementCreator psc, KeyHoldergeneratedKeyHolder)但它只返回整数键。 KeyHolder interface also allow only numeric keys. KeyHolder接口也只允许数字键。 Does someone know how to overcome this deficiency?有人知道如何克服这个缺陷吗? Should I use something other than JdbcTemplate?我应该使用 JdbcTemplate 以外的其他东西吗? It is not desirable, but possible.这是不可取的,但可能。 Give me a hint, please.请给我一个提示。

This works for me!这对我有用!

public String save(SysUser sysUser) {
    String SQL = "INSERT INTO sys_user(id,account_number,telephone,email,real_name) VALUES (?,?,?,?,?);";
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(connection -> {
        PreparedStatement preparedStatement = connection.prepareStatement(SQL, new String[]{"id"});
        preparedStatement.setString(1, sysUser.getId());
        preparedStatement.setString(2, sysUser.getAccountNumber());
        preparedStatement.setString(3, sysUser.getTelephone());
        preparedStatement.setString(4, sysUser.getEmail());
        preparedStatement.setString(5, sysUser.getRealName());
        return preparedStatement;
    }, keyHolder);
    return String.valueOf(keyHolder.getKeyList().get(0).get("id"));
}

You can use it this way to get a non numeric key您可以通过这种方式使用它来获取非数字键

GeneratedKeyHolder gendKey = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
    @Override
    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
        PreparedStatement stmt = con.prepareStatement("INSERT INTO MY_TABLE(col1, col2) VALUES (?, ?) ", Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, "val1");
        stmt.setString(2, "val2");
        return stmt;
    }
}, gendKey);

// If multiple rows updated, use getKeys()
String uuid = (String) gendKey.getKey().get("UUID-COL-NAME");

One way to do that is to create UUID in your java in advance and then pass it on to (SQL/DB), so in this case, you already know whats going to be your Primary key.一种方法是提前在您的 Java 中创建 UUID,然后将其传递给 (SQL/DB),因此在这种情况下,您已经知道什么将成为您的主键。 since in theory UUID is unique there should be no collision.因为理论上 UUID 是唯一的,所以不应该发生冲突。

UUID uuid = UUID.randomUUID();
jdbcTemplate.update("INSERT INTO users ( user_uuid , first_name , last_name) VALUES (?,?,?)",
                "admin", uuid, user.getFirstName(), user.getLastName());
//uuid: you already have the PrimeryKey in advance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM