原文地址:http://blog.csdn.net/i_chenjiahui/article/details/22323113
在开发中遇到了将一条数据插入到数据库的同时,又需要获得这个条数据的所自动生成的key值的场景。
Map map = new HashMap();
map.put("order_id", orderId);
map.put("scene_id", senceId);
map.put("device_type", deviceType);
map.put("device_company", company);
map.put("redo_info", message);
map.put("state", "当前");
map.put("order_flow_node_record_id", order_flow_node_record_id);
map.put("check_type", check_type);
this.simplejdbcinsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("test_table")
.usingColumns("order_id", "scene_id", "device_type",
"device_company", "redo_info", "state",
"order_flow_node_record_id", "check_type")
.usingGeneratedKeyColumns("id");
Number newKey = simplejdbcinsert
.executeAndReturnKey(map);
int newId = newKey.intValue();