Controller层代码
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService productService;
//产品删除
@RequestMapping("/delete.do")
public String delete(@RequestParam(value = "ids", defaultValue = "") String ids) throws Exception {
productService.delete(ids);
return "redirect:findAll.do";
}
}
IProductService接口
import java.util.List;
import com.qingfeng.domain.Product;
public interface IProductService {
public void delete(String ids);
}
ProductServiceImpl 代码
@Service
@Transactional
public class ProductServiceImpl implements IProductService{
@Autowired
private IProductDao productDao;
public void delete(String ids) {
//判断ids是否包含,
if (ids.contains(",")) {
//根据,拆分得到一个字符串数组
String[] split = ids.split(",");
//创建一个集合
List<Integer> list = new ArrayList<Integer>();
//遍历split字符串数组
for (String s : split) {
int i = 0;
try {
//将String字符类型数据转换为Integer整型数据
i = Integer.parseInt(s);
} catch (NumberFormatException e) {
}
//将转换的整形数据添加到集合里
list.add(i);
}
//遍历list集合,删除id所在的集合
for (Integer id : list) {
productDao.deleteId(id);
}
}else {
//删除只有一个id
productDao.deleteId(Integer.parseInt(ids));
}
}
}
IProductDao接口
public interface IProductDao {
//根据id删除
@Delete("delete from product where id=#{id}")
public void deleteId(Integer id);
}