SpringBoot整合JDBC数据库操作第五弹-批量添加/删除/修改数据

articles) { String sql = "UPDATE article SET title = ?, description = ? WHERE id = ?"; // spring jdbc 帮我们生成了批量插入的 sql 语句, 我们也可以直接使用批量的插入 sql 语句进行批量数据插入 return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement preparedStatement, int i) throws SQLException { Article article = articles.get(i); preparedStatement.setString(1, article.getTitle()); preparedStatement.setString(2, article.getDescription()); preparedStatement.setInt(3, article.getId()); } @Override public int getBatchSize() { return articles.size(); } }).length;}/** * 批量删除数据 */public int batchDeleteArticle(final List<integer> ids) { String sql = "DELETE FROM article WHERE id = ?"; // spring jdbc 帮我们生成了批量插入的 sql 语句, 我们也可以直接使用批量的插入 sql 语句进行批量数据插入 return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement preparedStatement, int i) throws SQLException { preparedStatement.setInt(1, ids.get(i)); } @Override public int getBatchSize() { return ids.size(); } }).length;}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162/<integer>/<article>/<article>修改ArticleService类文件, 增加批量添加/删除/修改方法

int batchCreateArticle(final List<article> articles);int batchModfiyArticle(final List<article> articles);int batchDeleteArticle(final List<integer> ids);12345/<integer>/<article>/<article>修改ArticleServiceImpl类文件, 增加批量添加/删除/修改方法

@Overridepublic int batchCreateArticle(List<article> articles) { return articleRepository.batchCreateArticle(articles);}@Overridepublic int batchModfiyArticle(List<article> articles) { return articleRepository.batchModfiyArticle(articles);}@Overridepublic int batchDeleteArticle(List<integer> ids) { return articleRepository.batchDeleteArticle(ids);}1234567891011121314/<integer>/<article>/<article>修改API接口层ArticleController, 增加批量添加/删除/修改对外接口

@RequestMapping(value = "batch/create", method = RequestMethod.POST)Integer batchCreate(@RequestBody List<article> articles) { return articleService.batchCreateArticle(articles);}@RequestMapping(value = "batch/modify", method = RequestMethod.PUT)Integer batchModfiy(@RequestBody List<article> articles) { return articleService.batchModfiyArticle(articles);}@RequestMapping(value = "batch/delete", method = RequestMethod.DELETE)Integer batchDelete(@RequestBody List<integer> ids) { return articleService.batchDeleteArticle(ids);}1234567891011121314/<integer>/<article>/<article>使用RestAPI测试工具测试api接口可用性