java根据id批量修改/删除

前台

function update() {
var arr = $("input[name='id']");
var Arr = [];
for (var i = 0; i < arr.length; i++) {

//将需要批量修改的id放到一个数组里面去传到后台
if (arr[i].checked == true) {
Arr.push(arr[i].value)
}
}
var ids = Arr;
console.log(ids)
if(ids.length==0){
layer.alert("请选择要修改的信息");
}else{

layer.confirm('确定修改此信息吗?', {
btn: ['确定','取消'], //按钮
}, function(){
$.ajax({
//几个参数需要注意一下
type : "POST",//方法类型
dataType : "json",//预期服务器返回的数据类型
url : "/tsm/helpCenter/update.do",//url
data : {
ids:ids.join(","),

}         //数组转字符串

 

success : function(result) {
if (result.result == 0) {
layer.alert("操作成功!");
setTimeout(function () {
window.location.href = "/tsm/helpCenter/helpCenterView.do";
}, 1000);
} else if (result.result == -1) {
layer.alert("操作失败!");
setTimeout(function () {
window.location.href = "/tsm/helpCenter/helpCenterView.do";
}, 1000);
};
},
});
return false;
}); 

}

 

 

Controller

/**
*批量 修改

* @param ids(前台传一个id的数组的字符串


* @return
*/
@RequestMapping("/update.do")
@ResponseBody
public String delete(String ids) {
return carHelpCenterService.updateState(ids);
}

 

 

service

 

/**
* 批量修改(修改状态)

* @param ids
* @return
*/
public String updateState(String ids) {
int count = carHelpCenterDao.updateState(ids);
if (count == 0) {
return Result.backResultJsonArray(-1, "失败", null);
}

return Result.backResultJsonArray(0, "成功", null);
}

 

 

 

Dao

 

 

public int updateState(@Param("ids") String ids);

 

 

 

xml

 

<!--批量删除帮助中心信息(我这里只是做假删除--修改状态state=1) -->
<update id="updateState">
update car_help_center
set 
state = 1,update_time =CURRENT_TIMESTAMP
where id in(${ids})
</update>

 

或者(dao层传的是一个List<String> list)
    <delete id="delete" parameterType="List">
        delete from kfz_cidpid_product_price_template where shop_num=#{shopNum}
        <foreach collection="list" item="item" index="index" open="AND (" close=")" separator="or"> 
            cid=#{item.cid}
        </foreach>
    </delete>

 

注(批量删除一样的,只是调用一个删除的sql)

 

 

 

 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答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作为参数,然后后端就会批量删除对应的记录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值