6

我正在使用 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 是否在某个地方开箱即用地提供了此功能?

谢谢杰

4

5 回答 5

7

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

适用于简单的 pojo:

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

生成:

更新员工 SET id = :id,name = :name,salary = :salary WHERE id = :id

于 2013-08-23T11:10:45.203 回答
6

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

于 2014-01-10T15:25:28.883 回答
4

你必须使用 JdbcTemplate

参见:13.2.1.1 JdbcTemplate 类使用示例

前任:

this.jdbcTemplate.update(
    "update t_actor set = ? where id = ?", 
    "Banjo", 5276L);
于 2012-05-23T21:49:03.947 回答
1

最简单的方法是:(来源

public void setName(int id, String name) {
    this.jdbcTemplate.update("update mytable set name = ? where id = ?", 
    new Object[] {name, new Integer(id)});
}
于 2012-10-12T20:41:41.757 回答
1

您可以通过使用 SimpleJdbcTemplate 而不是 JdbcTemplate 并通过扩展 SimpleJdbcDaoSupport 来获得更相似的效果,所有 DB 操作都可以放在一个 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));
    }
}
于 2012-12-05T16:08:11.050 回答