Spring-JDBC的更改操作
1、更改指定的账户-(根据主键进行更改)
public int updateAccount(Account account) {
String sql="update tb_account set account_name=?,account_type=?,money=?,remark=?," +
"update_time=now(),user_id=? where account_id=?";
Object[] objs={account.getAccountName(),account.getAccountType(),account.getMoney(),
account.getRemark(),account.getUserId(),account.getAccountId()};
int row=jdbcTemplate.update(sql,objs);
return row;
}
2、批量更改指定的账户
public int updateAccountBatch(List<Account> accounts) {
String sql="update tb_account set account_name=?,account_type=?,money=?,remark=?," +
"update_time=now(),user_id=? where account_id=?";
int rows=jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Account account=accounts.get(i);
//设置参数
ps.setString(1,account.getAccountName());
ps.setString(2,account.getAccountType());
ps.setDouble(3,account.getMoney());
ps.setString(4,account.getRemark());
ps.setInt(5,account.getUserId());
ps.setInt(6,account.getAccountId());
}
@Override
public int getBatchSize() {
return accounts.size();
}
}).length;
return rows;
}