基于springMVC的业务删除

一、单个删除

注意:此删除逻辑删除,只是把要删除的东西 状态改为1,1代表删除,需要在数据库中加入字段delete表示状态

1.1 controller

@Controller
@RequestMapping("/ads")
public class AdController {

    @Resource
    private IAdService adService;
    
    @RequestMapping("/deleteById.action")
    @ResponseBody
    public R deleteById(Integer id) throws Exception{
        adService.deleteById(id);
        return R.ok();
    }
   }

1.2 service

@Service
public class AdServiceImpl implements IAdService {

    @Resource
    private IAdDAO adDAO;
    @Override
    public void deleteById(Integer id) throws Exception {
        if(null==id){
            throw new BusinessException(ResponceCode.PARAMETER_EXCEPTION.getCode(),
                    ResponceCode.PARAMETER_EXCEPTION.getMessage());
        }
        adDAO.deleteById(id);
    }
   }

1.3 dao

public class AdDAO implements IAdDAO{

    private JdbcUtils jdbcUtils;

    @Override
    public void deleteById(Integer id) throws Exception {
        jdbcUtils.queryRunner().update(ConnectionThreadLocal.getThreadLocal().get(),
                "update t_ad set deleted=1 where id=?",id);
    }
}

二、批量删除

批量删除是基于单个删除的,不用再次操作数据库了,直接调用单次删除即可

2.1 controller

@Controller
@RequestMapping("/ads")
public class AdController {

    @Resource
    private IAdService adService;
	
	/**
	*单次删除
	*/
    @RequestMapping("/deleteById.action")
    @ResponseBody
    public R deleteById(Integer id) throws Exception{
        adService.deleteById(id);
        return R.ok();
    }

    /**
     * 批量删除
     * @param adId
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteByIds.action")
    @ResponseBody
    public R deleteByIds(String adId) throws Exception{
        adService.deleteByIds(adId);
       return R.ok();
    }
}

2.2 service

@Service
public class AdServiceImpl implements IAdService {

    @Resource
    private IAdDAO adDAO;
    @Override
    public void deleteById(Integer id) throws Exception {
        if(null==id){
            throw new BusinessException(ResponceCode.PARAMETER_EXCEPTION.getCode(),
                    ResponceCode.PARAMETER_EXCEPTION.getMessage());
        }
        adDAO.deleteById(id);
    }

    @Override
    public void deleteByIds(String adId) throws Exception {
        if(StringUtils.isEmpty(adId)){
            throw new BusinessException(ResponceCode.PARAMETER_EXCEPTION.getCode(),
                    ResponceCode.PARAMETER_EXCEPTION.getMessage());
        }
        if (adId.endsWith(",")){
            adId=adId.substring(0,adId.length()-1);
        }
        if (!adId.contains(",")){
            //说明只有一个adId
            deleteById(Integer.parseInt(adId));
        }else{
            String[] adsId = adId.split(",");
            for (String id : adsId){
                int i = Integer.parseInt(id);
                deleteById(i);
            }
        }
    }
}

2.3 dao

这里只需要单次删除的即可

@Compent
public class AdDAO implements IAdDAO{

    private JdbcUtils jdbcUtils;

	/**
	单次删除
	*/
    @Override
    public void deleteById(Integer id) throws Exception {
        jdbcUtils.queryRunner().update(ConnectionThreadLocal.getThreadLocal().get(),
                "update t_ad set deleted=1 where id=?",id);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值