springboot集合MySQL删除_SpringBoot整合JDBC数据库操作第五弹-批量添加/删除

修改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接口可用性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值