简体   繁体   English

如何在不指定列名的情况下使用jdbcTemplate更新整个对象?

[英]How to update an entire object using jdbcTemplate without specifying column names?

I was looking at the reference here . 我在这里看参考。 We can do this - 我们做得到 -

String orderSql = "select * from order where id = ?"; jdbcTemplate.query(orderSql, new BeanPropertyRowMapper<>(Order.class), orderId);

This reads from the database and can directly deserialise into the Order object. 这将从数据库中读取,并且可以直接反序列化到Order对象中。 This is well and good. 这很好。

What I want to do is - 我想做的是-

String updateSql = "update order ? where id = ?"; jdbcTemplate.save(updateSql, new BeanPropertyRowMapper<>(Order.class), order, orderId);

Is there a way of doing this? 有办法吗?

No, it's not possible. 不,不可能。 JdbcTemplate comes with the ability to map an object using query as you've said without having to define columns/attributes, but there is no equivalent usage for update . JdbcTemplate能够像您所说的那样使用query映射对象,而不必定义列/属性,但是没有等效的update用法。 You will have to pass in your SQL and the relevant parameters. 您将必须传递您的SQL和相关参数。 As someone has mentioned if you really want to do this you could consider using hibernate. 正如有人提到的那样,如果您真的要执行此操作,则可以考虑使用休眠模式。

No ! 不行 You can only do this ! 您只能这样做!

jdbcTemplate.update("update ordertable set order = ? where id = ?", order, orderId);

Use this query for update :- 使用此查询进行更新:-

String updateSql = "update <table_name> set order= ? where id = ?";

jdbcTemplate.update(updateSql, new Object[]{order, orderId});

It will return affected row update count. 它将返回受影响的行更新计数。

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

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