Java Code Examples for org.springframework.jdbc.core.simple.SimpleJdbcInsert#executeAndReturnKey()

The following examples show how to use org.springframework.jdbc.core.simple.SimpleJdbcInsert#executeAndReturnKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RideRepositoryImpl.java    From Spring with Apache License 2.0 6 votes vote down vote up
/**
 * Alternative to RideRepositoryImpl#createRide(com.pluralsight.model.Ride)
 */
public Ride createRideSimpleJDBC(Ride ride) {
	final SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate);

	insert.setGeneratedKeyName("id");

	final Map<String, Object> data = new HashMap<>();
	data.put("name", ride.getName());
	data.put("duration", ride.getDuration());

	final List<String> columns = new ArrayList<>();
	columns.add("name");
	columns.add("duration");

	insert.setTableName("ride");
	insert.setColumnNames(columns);

	final Number id = insert.executeAndReturnKey(data);
	return getRide(id);
}
 
Example 2
Source File: SpringJDBCTemplateExample.java    From java-course-ee with MIT License 5 votes vote down vote up
private static void insertConstructor(JdbcTemplate jdbc) {
    separator("insertConstructor");
    SimpleJdbcInsert insertActor = new SimpleJdbcInsert(jdbc).withSchemaName("region").withTableName("jc_region").usingGeneratedKeyColumns("region_id");

    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("region_name", "new region name");
    Number number = insertActor.executeAndReturnKey(parameters);
    log.debug("Inserted region id: {}", number.longValue());
}