这篇文章主要讲解一下对数据的批量操作(批量增加,批量修改,批量删除).
修改ArticleRepository类文件, 增加批量添加/删除/修改方法
/**
* 批量添加数据
*/
public int batchCreateArticle(final List articles) {
String sql = "INSERT INTO article(title, description) VALUES(?, ?)";
// spring jdbc 帮我们生成了批量插入的 sql 语句, 我们也可以直接使用批量的插入 sql 语句进行批量数据插入
// return jdbcTemplate.batchUpdate(new String[]{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());
}
@Override
public int getBatchSize() {
return articles.size();
}
}).length;
}
/**
* 批量修改数据
*/
public int batchModfiyArticle(final List 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 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;
}
修改ArticleService类文件, 增加批量添加/删除/修改方法
int batchCreateArticle(final List articles);
int batchModfiyArticle(final List articles);
int batchDeleteArticle(final List ids);
修改ArticleServiceImpl类文件, 增加批量添加/删除/修改方法
@Override
public int batchCreateArticle(List articles) {
return articleRepository.batchCreateArticle(articles);
}
@Override
public int batchModfiyArticle(List articles) {
return articleRepository.batchModfiyArticle(articles);
}
@Override
public int batchDeleteArticle(List ids) {
return articleRepository.batchDeleteArticle(ids);
}
修改API接口层ArticleController, 增加批量添加/删除/修改对外接口
@RequestMapping(value = "batch/create", method = RequestMethod.POST)
Integer batchCreate(@RequestBody List articles) {
return articleService.batchCreateArticle(articles);
}
@RequestMapping(value = "batch/modify", method = RequestMethod.PUT)
Integer batchModfiy(@RequestBody List articles) {
return articleService.batchModfiyArticle(articles);
}
@RequestMapping(value = "batch/delete", method = RequestMethod.DELETE)
Integer batchDelete(@RequestBody List ids) {
return articleService.batchDeleteArticle(ids);
}
使用RestAPI测试工具测试api接口可用性
原文:https://blog.csdn.net/ShrCheng/article/details/80729529
版权声明:本文为博主原创文章,转载请附上博文链接!