一、单个删除
注意:此删除逻辑删除,只是把要删除的东西 状态改为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);
}
}