Avue实现批量删除等功能(附Demo)

前言

由于近期慢慢转全栈,后续会以前后端的形式讲解

  1. 对应的Avue相关知识推荐阅读:【vue】avue-crud表单属性配置(表格以及列)
  2. 对应后端知识推荐阅读:java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)

对于单个删除的功能比较好删除,如果多选的形式删除需要获取多个id列表,对应在后端进行删除

在讲述批量删前,先补充单一的删除逻辑

1. 公共逻辑

前端对应的逻辑如下:

<avue-crud :option="option"
           :table-loading="loading"
           :data="data"
           :page="page"
           :permission="permissionList"
           :before-open="beforeOpen"
           v-model="form"
           ref="crud"
           @row-update="rowUpdate"
           @row-save="rowSave"
           @row-del="rowDel"
           @search-change="searchChange"
           @search-reset="searchReset"
           @selection-change="selectionChange"
           @current-change="currentChange"
           @size-change="sizeChange"
           @refresh-change="refreshChange"
           @on-load="onLoad"
           @current-row-change="handleCurrentRowChange"
           @row-click="handleRowClick"
>
  <template slot="menuLeft">

    <el-button type="danger"
               size="small"
               icon="el-icon-delete"
               plain
               @click="handleDelete">删 除
    </el-button>

  </template>

</avue-crud>

对于单个删除或者批量删除,接口也可做成一样的

export const remove = (ids) => {
  return request({
    url: '/api/xxx/yyy/remove',
    method: 'post',
    params: {
      ids,
    }
  })
}

后端的接口如下:

@PostMapping("/remove")
@ApiOperationSupport(order = 7)
@ApiOperation(value = "逻辑删除", notes = "传入ids")
public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
	return R.status(tyreRepareOrderService.deleteLogic(Func.toLongList(ids)));
}

对于Func工具类,内部其实是Arrays.asList(toLongArray(str));

2. 单个删除

对应前端界面的method逻辑方法:

handleDelete() {
	  if (this.currentRow== null) {
		this.$message.warning("请选择一条数据");
	    return;
	  }
	  this.$confirm("确定将选择数据删除?", {
	    confirmButtonText: "确定",
	    cancelButtonText: "取消",
	    type: "warning"
	  })
	    .then(() => {
	      return remove(this.currentRow.id);
	    })
	    .then(() => {
	      this.onLoad(this.page);
	      this.$message({
	        type: "success",
	        message: "操作成功!"
	      });
	      // this.$refs.crud.toggleSelection();
	    });
},

3. 批量删除

与单个删除不同的点在于需要有按钮框选择!

// 新增的按钮选项
option: {
  menu:true,
  align:'center',
  stripe:true,
  tip: false,
  border: true,
  index: false,
  viewBtn: false,
  highlightCurrentRow:true,
  searchShow:false,
  selection:true, 
  searchMenuSpan: 6,
  calcHeight: 30,
  height:'auto',
  addBtn:true,
  delBtn:true,
  column: [
    {

只需要将selection改为true即可
在这里插入图片描述

对应的前端逻辑如下:(主要参照逻辑)

computed: {
  
      // 批量ids ,为了删除等操作
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
      newModel(){
        return this.formInline.model;
      }
    },

对应删除的方法如下:

handleDelete() {
	  if (this.selectionList.length === 0) {
	    this.$message.warning("请选择至少一条数据");
	    return;
	  }
	  this.$confirm("确定将选择数据删除?", {
	    confirmButtonText: "确定",
	    cancelButtonText: "取消",
	    type: "warning"
	  })
	    .then(() => {
	      return remove(this.ids);
	    })
	    .then(() => {
	      this.onLoad(this.page);
	      this.$message({
	        type: "success",
	        message: "操作成功!"
	      });
	      // this.$refs.crud.toggleSelection();
	    });
},
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用JPA提供的@Modifying和@Query注解,编写一个自定义的删除方法,如下所示: ``` @Modifying @Query("delete from User u where u.id in :ids") void deleteByIds(@Param("ids") List<Long> ids); ``` 其中,@Modifying注解表示该方法会修改数据,@Query注解表示该方法使用JPQL语句进行查询,@Param注解表示方法参数与JPQL语句中的参数对应。 在方法中,我们可以使用in关键字来批量删除指定id的数据,ids参数为一个Long类型的List,表示要删除的id列表。 使用该方法时,只需要传入要删除的id列表即可,如下所示: ``` List<Long> ids = Arrays.asList(1L, 2L, 3L); userRepository.deleteByIds(ids); ``` 其中,userRepository为JPA提供的Repository接口,可以直接调用我们自定义的删除方法。 ### 回答2: Java Spring Boot可以通过使用JpaRepository接口中的"deleteAllInBatch"方法来实现批量删除。 首先,需要在Repository接口中定义一个扩展自JpaRepository的自定义方法,用于批量删除数据。例如: ```java import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.demo.model.Entity; @Repository public interface EntityRepository extends JpaRepository<Entity, Long> { void deleteAllInBatch(Iterable<Entity> entities); } ``` 然后,在Service层或Controller层中引入这个Repository,并调用"deleteAllInBatch"方法来实现批量删除。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.repository.EntityRepository; @Service public class EntityService { private final EntityRepository entityRepository; @Autowired public EntityService(EntityRepository entityRepository) { this.entityRepository = entityRepository; } public void deleteEntitiesInBatch(Iterable<Entity> entities) { entityRepository.deleteAllInBatch(entities); } } ``` 最后,在使用这个Service的地方,传入要删除的实体对象集合,即可实现批量删除。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.example.demo.model.Entity; import com.example.demo.service.EntityService; @RestController public class EntityController { private final EntityService entityService; @Autowired public EntityController(EntityService entityService) { this.entityService = entityService; } @DeleteMapping("/entities") public void deleteEntities(@RequestBody Iterable<Entity> entities) { entityService.deleteEntitiesInBatch(entities); } } ``` 以上就是使用Java Spring Boot实现批量删除的方法。 ### 回答3: 在Java Spring Boot中实现批量删除可以通过以下步骤: 1. 首先,确保你已经创建了一个Spring Boot项目并配置好数据库连接。 2. 创建一个实体类,例如Student,用于表示需要删除的对象。添加必要的字段和注解,使其与数据库表映射。 3. 创建一个Repository接口,例如StudentRepository,继承自JpaRepository。这个接口将提供基本的CRUD操作方法。 4. 在Service层创建一个实现类,例如StudentService。注入StudentRepository来访问数据库。 5. 在Service中添加一个批量删除的方法,例如deleteStudents,接收一个包含要删除对象id的List作为参数。 6. 在deleteStudents方法中,使用JpaRepository的deleteAllByIdInBatch方法,将传入的id列表作为参数。这个方法会一次性批量删除给定的id对应的记录。 7. 在Controller层创建一个REST接口,例如StudentController,注入StudentService。 8. 在Controller中添加一个处理批量删除请求的方法,例如handleBatchDelete,接收一个包含要删除对象id的List作为参数。 9. 在handleBatchDelete方法中,调用StudentService的deleteStudents方法,并返回适当的响应。 这样,当客户端发送批量删除请求时,可以调用这个接口并传入一个包含要删除对象id的List作为参数,然后后端就会批量删除对应的记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值