批量删除本地文件
1、批量查询
<!-- 按Id批量查找 -->
<select id="batchQueryById" parameterType="Long[]" resultMap="BaseResultMap">
SELECT <include refid="BaseColumnList" />
FROM t_attachment WHERE f_attachment_id in (
<foreach collection="array" item="item" separator=",">
#{item,jdbcType=DECIMAL}
</foreach>
)
</select>
- select * from TABLE where id in (01,02,03,04)
2、批量删除xml
<!-- 按Id批量删除 -->
<delete id="batchDeleteById" parameterType="Long[]">
DELETE FROM t_attachment WHERE f_attachment_id in (
<foreach collection="array" item="item" separator=",">
#{item,jdbcType=DECIMAL}
</foreach>
)
</delete>3、批量删除本地文件
/**
* 批量删除本地文件
*
* @param id
* @return
*/
public BaseRespVO deleteAttachment(@Param("attachmentId") String attachmentId) {
List<AttachmentEntity> entity = attachmentBO.batchQueryById(attachmentId.split(","));
int deleteCount = attachmentBO.batchDeleteById(attachmentId.split(","));
if (deleteCount > 0 && entity != null) {
for(int i=0;i<entity.size();i++){
ResourceBundle resource = ResourceBundle.getBundle("conf");
File file = new File(resource.getString("upload.path") + entity.get(i).getPath());
if(file.exists()){
file.delete();
}}
return new BaseRespVO();
} else {
return new BaseRespVO(2, "没有要删除的记录!");
}
}
- 从前端传来的值为字符串(id逗号相连),后端接收后split(",")拆分成存入数组;
- 批量查询后,for循环来获取路径,resource.getString("upload.path") + entity.get(i).getPath();
- 如果存在则删除该文件。
本文介绍了一种批量处理文件及数据库记录的方法,包括批量查询与删除数据库记录,并结合实际业务需求,实现批量删除本地文件的功能。
8281

被折叠的 条评论
为什么被折叠?



