SimpleJdbcInsert等效于更新

时间:2012-05-23 15:14:11

标签: spring jdbc sql-update simplejdbcinsert

我使用Spring的SimpleJdbcInsert类来创建实体 - 例如:

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

insert.execute(new BeanPropertySqlParameterSource(abc));

是否有一些相当于此课程的更新?例如,假设我们正在处理单列主键,那么类似下面的内容将是一个方便的接口:

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

update.execute(new BeanPropertySqlParameterSource(abc));

Spring是否提供了开箱即用的功能?

由于 杰

5 个答案:

答案 0 :(得分:5)

Spring JIRA中存在一个关于缺少SimpleJdbcUpdate类:https://jira.springsource.org/browse/SPR-4691的问题。你可能想在那里投票。

答案 1 :(得分:4)

您必须使用JdbcTemplate

请参阅:13.2.1.1 Examples of JdbcTemplate class usage

E.X:

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

答案 2 :(得分:4)

对于任何未来的读者 - 我想出了一个使用反射的便利功能;

适用于简单的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));
    }

使用示例:

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

生成:

UPDATE员工SET id =:id,name =:name,salary =:salary WHERE id =:id

答案 3 :(得分:1)

使用SimpleJdbcTemplate而不是JdbcTemplate可以获得更多类似的效果,并且通过扩展SimpleJdbcDaoSupport,所有数据库操作都可以放在一个DAO类中:

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));
    }
}

答案 4 :(得分:0)

执行此操作的简便方法是:(source

public void setName(int id, String name) {
    this.jdbcTemplate.update("update mytable set name = ? where id = ?", 
    new Object[] {name, new Integer(id)});
}