Package org.springframework.jdbc.core.namedparam

Examples of org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource


  public void batchCreateObject(List<User> userList) {
    int i = 0;
    BeanPropertySqlParameterSource[] sourceArray = new BeanPropertySqlParameterSource[userList.size()];

    for (User user : userList) {
      sourceArray[i++] = new BeanPropertySqlParameterSource(user);
    }

    jdbcTemplate.batchUpdate(INSERT_USER, sourceArray);
  }
View Full Code Here


   * 使用TransactionTemplate编程控制事务,一般在Manager/Service层
   * 无返回值的情形.
   */
  public void createUserInTransaction(User user) {

    final BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(user);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
      @Override
      public void doInTransactionWithoutResult(TransactionStatus status) {
        jdbcTemplate.update(INSERT_USER, source);
      }
View Full Code Here

   * 使用TransactionTemplate编程控制事务,一般在Manager/Service层
   * 有返回值的情形,并捕获异常进行处理不再抛出的情形.
   */
  public boolean createUserInTransaction2(User user) {

    final BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(user);
    return transactionTemplate.execute(new TransactionCallback<Boolean>() {
      public Boolean doInTransaction(TransactionStatus status) {
        try {
          jdbcTemplate.update(INSERT_USER, source);
          return true;
View Full Code Here

    private NamedParameterJdbcOperations namedParameterJdbcTemplate;

    @Override
  public void savePlayer(Player player) {
        namedParameterJdbcTemplate.update(INSERT_PLAYER, new BeanPropertySqlParameterSource(player));
  }
View Full Code Here

   * the provided item.
   * @param item the item to use for parameter values
   */
  @Override
  public SqlParameterSource createSqlParameterSource(T item) {
    return new BeanPropertySqlParameterSource(item);
  }
View Full Code Here

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
  public void savePlayer(Player player) {
        namedParameterJdbcTemplate.update(INSERT_PLAYER, new BeanPropertySqlParameterSource(player));
  }
View Full Code Here

  }

  @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")));
  }
View Full Code Here

  }

  @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");
    }
View Full Code Here

  @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");
    }
View Full Code Here

        //Note: Remember that spring doesn't generate ID. The database generates the next id. Make sure the database
        // has a trigger or autoincrement facility on the ID column.
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getDataSource()).withTableName(TABLE_NAME)
                .usingGeneratedKeyColumns("ID");

        SqlParameterSource parameters = new BeanPropertySqlParameterSource(episode);

        Number id = simpleJdbcInsert.executeAndReturnKey(parameters);
        episode.setId(id.intValue());
        return episode;
    }
View Full Code Here

TOP

Related Classes of org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.