/** * Provide parameter values in an {@link BeanPropertySqlParameterSource} based on values from * the provided item. * @param item the item to use for parameter values */ @Override public SqlParameterSource createSqlParameterSource(T item) { return new BeanPropertySqlParameterSource(item); }
/** * Create an array of {@link SqlParameterSource} objects populated with data * from the values passed in (either a {@link Map} or a bean object). * This will define what is included in a batch operation. * @param candidates collection of objects containing the values to be used * @return an array of {@link SqlParameterSource} * @since 5.0.2 * @see MapSqlParameterSource * @see BeanPropertySqlParameterSource * @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]) */ @SuppressWarnings("unchecked") public static SqlParameterSource[] createBatch(Collection<?> candidates) { SqlParameterSource[] batch = new SqlParameterSource[candidates.size()]; int i = 0; for (Object candidate : candidates) { batch[i] = (candidate instanceof Map ? new MapSqlParameterSource((Map<String, ?>) candidate) : new BeanPropertySqlParameterSource(candidate)); i++; } return batch; }
@Override public void savePlayer(Player player) { namedParameterJdbcTemplate.update(INSERT_PLAYER, new BeanPropertySqlParameterSource(player)); }
@Override public void savePlayer(Player player) { namedParameterJdbcTemplate.update(INSERT_PLAYER, new BeanPropertySqlParameterSource(player)); }
@Test(expected = IllegalArgumentException.class) public void withNullBeanPassedToCtor() throws Exception { new BeanPropertySqlParameterSource(null); }
@Test(expected = IllegalArgumentException.class) public void getValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception { BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties()); source.getValue("noOp"); }
@Test public void testQueryForLongWithParamBean() throws Exception { given(resultSet.getMetaData()).willReturn(resultSetMetaData); given(resultSet.next()).willReturn(true, false); given(resultSet.getLong(1)).willReturn(87L); BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(new ParameterBean(3)); long l = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID = :id", params, Long.class).longValue(); assertEquals("Return of a long", 87, l); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); verify(preparedStatement).setObject(1, 3, Types.INTEGER); }
@Test public void testQueryForLongWithParamBeanWithCollection() throws Exception { given(resultSet.getMetaData()).willReturn(resultSetMetaData); given(resultSet.next()).willReturn(true, false); given(resultSet.getLong(1)).willReturn(87L); BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(new ParameterCollectionBean(3, 5)); long l = template.queryForObject("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)", params, Long.class).longValue(); assertEquals("Return of a long", 87, l); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)"); verify(preparedStatement).setObject(1, 3); verify(preparedStatement).setObject(2, 5); }
@Test(expected = IllegalArgumentException.class) public void getValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception { BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean()); source.getValue("thisPropertyDoesNotExist"); }
@Test public void hasValueWhereTheUnderlyingBeanPropertyIsNotReadable() throws Exception { BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties()); assertFalse(source.hasValue("noOp")); }
@Test public void hasValueWhereTheUnderlyingBeanHasNoSuchProperty() throws Exception { BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean()); assertFalse(source.hasValue("thisPropertyDoesNotExist")); }
@Test public void successfulPropertyAccess() { BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean("tb", 99)); assertTrue(Arrays.asList(source.getReadablePropertyNames()).contains("name")); assertTrue(Arrays.asList(source.getReadablePropertyNames()).contains("age")); assertEquals("tb", source.getValue("name")); assertEquals(99, source.getValue("age")); assertEquals(Types.VARCHAR, source.getSqlType("name")); assertEquals(Types.INTEGER, source.getSqlType("age")); }
@Test public void testWriteAndFlush() throws Exception { when(namedParameterJdbcOperations.batchUpdate(eq(sql), eqSqlParameterSourceArray(new SqlParameterSource[] {new BeanPropertySqlParameterSource(new Foo("bar"))}))) .thenReturn(new int[] {1}); writer.write(Collections.singletonList(new Foo("bar"))); }
@Test public void testWriteAndFlushWithFailure() throws Exception { final RuntimeException ex = new RuntimeException("ERROR"); when(namedParameterJdbcOperations.batchUpdate(eq(sql), eqSqlParameterSourceArray(new SqlParameterSource[] {new BeanPropertySqlParameterSource(new Foo("bar"))}))) .thenThrow(ex); try { writer.write(Collections.singletonList(new Foo("bar"))); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals("ERROR", e.getMessage()); } }
@Test public void testWriteAndFlushWithEmptyUpdate() throws Exception { when(namedParameterJdbcOperations.batchUpdate(eq(sql), eqSqlParameterSourceArray(new SqlParameterSource[] {new BeanPropertySqlParameterSource(new Foo("bar"))}))) .thenReturn(new int[] {0}); try { writer.write(Collections.singletonList(new Foo("bar"))); fail("Expected EmptyResultDataAccessException"); } catch (EmptyResultDataAccessException e) { // expected String message = e.getMessage(); assertTrue("Wrong message: " + message, message.contains("did not update")); } }
StaticBeanPropertySqlParameterSource(Object input, Map<String, Object> staticParameters) { this.input = new BeanPropertySqlParameterSource(input); this.staticParameters = staticParameters; }
@Test public void successfulPropertyAccessWithOverriddenSqlType() { BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean("tb", 99)); source.registerSqlType("age", Types.NUMERIC); assertEquals("tb", source.getValue("name")); assertEquals(99, source.getValue("age")); assertEquals(Types.VARCHAR, source.getSqlType("name")); assertEquals(Types.NUMERIC, source.getSqlType("age")); }
private SqlParameterSource createSqlParameterSource(E entity) { return new BeanPropertySqlParameterSource(entity); }
@Override public int insert(Message message) { /* Map<String, Object> values = new HashMap<>(); values.put("NAME", message.getName()); values.put("message", message.getMessage()); values.put("creationTime", new Timestamp(message.getCreationTime().getTime())); Number genKey = simpleInsert.executeAndReturnKey(values); */ BeanPropertySqlParameterSource paramSource = new BeanPropertySqlParameterSource(message); Number genKey = simpleInsert.executeAndReturnKey(paramSource); return genKey.intValue(); }
@Override public int insert(Message message) { SqlParameterSource paramSource = new BeanPropertySqlParameterSource(message); KeyHolder keyHolder = new GeneratedKeyHolder(); template.update("insert into guestmessage (name, message, creationTime) values (:name, :message,:creationTime)", paramSource, keyHolder); Number idNum = keyHolder.getKey(); return idNum.intValue(); }