Simple equivalent of SimpleJdbcInsert for upgrade

I am using the Spring SimpleJdbcInsert class to create objects - for example:

final SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("abc");

insert.execute(new BeanPropertySqlParameterSource(abc));

Is there any equivalent of this class for performing updates? As an example, something like below will be a convenient interface, assuming we are dealing with a single key column key:

final SimpleJdbcUpdate update = new SimpleJdbcUpdate(dataSource).withTableName("abc").withIdColumn("abcId");

update.execute(new BeanPropertySqlParameterSource(abc));

Does Spring provide this functionality out of the box?

Thanks jay

+3
source share
5 answers

You need to use JdbcTemplate

See: 13.2.1.1 Examples of using the JdbcTemplate class

EX:

this.jdbcTemplate.update(
    "update t_actor set = ? where id = ?", 
    "Banjo", 5276L);
+3
source

Spring JIRA SimpleJdbcUpdate: https://jira.springsource.org/browse/SPR-4691. , .

+4

For future readers - I came up with a convenience function using reflection;

Works for simple pojos:

public void dao_update(NamedParameterJdbcTemplate database, String table, Object pojo, String[] keys) {

        StringBuilder sqlBuilder = new StringBuilder("UPDATE ");
        sqlBuilder.append(table);
        sqlBuilder.append(" SET ");
        boolean first = true;
        for (Field field : pojo.getClass().getDeclaredFields()) {
            if (!first) {
                sqlBuilder.append(",");
            }
            first = false;
            sqlBuilder.append(field.getName());
            sqlBuilder.append(" = :");
            sqlBuilder.append(field.getName());
        }


        first = true;
        for (String key : keys) {
            if (first) {
                sqlBuilder.append(" WHERE ");
            } else {
                sqlBuilder.append(" AND ");
            }
            first = false;
            sqlBuilder.append(key);
            sqlBuilder.append("= :");
            sqlBuilder.append(key);
        }
        database.getJdbcOperations().update(sqlBuilder.toString(), new BeanPropertySqlParameterSource(pojo));
    }

Usage example:

dao_update(database, "employee", my_employee, "id");

Forms:

UPDATE employee SET id =: id, name =: name, salary =: salary WHERE id =: id

+3
source

You can get a more similar effect using SimpleJdbcTemplate instead of JdbcTemplate and expanding SimpleJdbcDaoSupport, all database operations can be placed in one DAO class:

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class BankDaoImpl extends SimpleJdbcDaoSupport implements BankDao {


    @Autowired
    public BankDaoImpl(@Qualifier("dataSource") DataSource dataSource) {
        setDataSource(dataSource);
    }

    @Override
    public void insert(Bank bank) {
        String sql = "INSERT INTO BANK (id, oib, short_name, name, street, town, postal_code, homepage_url, last_change) VALUES (NEXT VALUE FOR bank_seq, :oib, :shortName, :name, :street, :town, :postalCode, :homepageUrl, CURRENT_TIMESTAMP)";
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(
                bank);

        getSimpleJdbcTemplate().update(sql, parameterSource);
    }

    @Override
    public void update(Bank bank) {
        String sql = "UPDATE BANK SET oib=:oib, short_name=:shortName, name=:name, street=:street, town=:town, postal_code=:postalCode, homepage_url=:homepageUrl, last_change=CURRENT_TIMESTAMP WHERE id=:id";
        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(
                bank);

        getSimpleJdbcTemplate().update(sql, parameterSource);
    }

    @Override
    public void delete(String id) {
        String sql = "DELETE FROM BANK WHERE id=:id";

        getSimpleJdbcTemplate().update(sql,
                new MapSqlParameterSource("id", id));
    }

    @Override
    public Bank findById(String id) {
        String sql = "select b.ID, b.OIB, b.SHORT_NAME, b.NAME, b.STREET, b.TOWN, b.POSTAL_CODE, b.HOMEPAGE_URL, b.LAST_CHANGE, CASE WHEN count(f.id) = 0 THEN 0 ELSE 1 END AS ready " +
                "from BANK WHERE b.ID = :id"; 

        return getSimpleJdbcTemplate().queryForObject(sql,
                BeanPropertyRowMapper.newInstance(Bank.class),
                new MapSqlParameterSource("id", id));
    }
}
+2
source

Easy way to do this:

    String SQL = "UPDATE some_schema.some_table "
    +"SET  your_column_1=? , your_column_2=? "
    +" where  your_column_1 =? and your_column_4=? ";
    getJdbcTemplate().update(SQL, new Object[]{"someValue_1","someValue_2","someValue_3","someValue_4"});
+1
source

All Articles