/**
- 根据id查询
- @param clazz
- @param id
- @return */
public static Object get(Class clazz,Serializable id){
Session s=null;
try{
s=HibernateUtil.getSession();
Object obj=s.get(clazz, id);
return obj;
}finally{
if(s!=null){
s.close();
}
}
}
servlet 如下
public class CollectDelete extends HttpServlet {
private static final long serialVersionUID = 1L;
Class clazz=Collect.class;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//String username = request.getParameter("username");
int shopid = Integer.parseInt(request.getParameter("shopid"));
Collect collect=new Collect();
//Object obj=HibernateUtil.get(clazz, shopid);
collect=(Collect) HibernateUtil.get(clazz, shopid);
response.setContentType("text/html;charset=UTF-8");
if(collect!= null){
System.out.println(collect.getUsername());
HibernateUtil.delete(collect);
response.getWriter().println("删除成功");
}else{
response.getWriter().println("卧槽失败了");
}
}
}
为啥shopid传进去了,查到的collect为空呢