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;
    }
全部评论

相关推荐

07-16 14:10
门头沟学院 Java
点赞 评论 收藏
分享
05-26 22:25
门头沟学院 Java
Java小肖:不会是想叫你过去把你打一顿吧,哈哈哈
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务