使用RESTful Jersey框架搭建WebService,Hibernate框架訪問資料庫,MySQL儲存資料
阿新 • • 發佈:2019-01-03
這個類也是通過上面樣例的方式新增進去的,本文主要的特色是在DAO中添加了Hibernate的方法呼叫資料庫,一般本地訪問時,只需要在DAO部分呼叫對應的方法就可以了,因為要釋出成服務,所以可以在伺服器端呼叫資料庫。package com.hnu.hibernate.resources; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import org.hibernate.Session; import javax.ws.rs.PUT; import com.hnu.hibernate.dao.BaseDAO; import com.hnu.hibernate.util.HibernateUtil; import com.hnu.hibernate.bean.*; @Consumes("application/xml") @Path("students") public class StudentsResource { private static BaseDAO<Student> baseDAO = new BaseDAO<Student>(); @POST @Path("create") @Produces("text/html") @Consumes("application/xml") public String create(Student model) { return String.format("%d", baseDAO.create(model)); } @POST @Path("remove") @Produces("text/html") @Consumes("application/xml") public String remove(Student model) { return String.format("%d", baseDAO.delete(model)); } @POST @Path("modify") @Produces("text/html") @Consumes("application/xml") public String modify(Student model) { return String.format("%d", baseDAO.update(model)); } @GET @Path("findAll") public List<Student> findAll() { return baseDAO.list(" from Student "); } @POST @Path("findById") @Consumes("application/xml") public Student findById(Student model) { return baseDAO.find(Student.class, model.getUserid()); } @POST @Path("findByUseridAndPassword") @Consumes("application/xml") @Produces("application/xml") public Student findByUseridAndPassword(Student model) { Session session = HibernateUtil.getSessionFactory().openSession(); Student student=null; try { session.beginTransaction(); student= (Student)session.createQuery("select s from Student s where s.username='"+model.getUsername()+"' and s.password='"+model.getPassword()+"'").uniqueResult(); session.getTransaction().commit(); }catch (Exception e) { session.getTransaction().rollback(); } finally { session.close(); } return student; } @POST @Path("findByUsernameAndRealnameAndStudyabroadschool") @Consumes("application/xml") @Produces("application/xml") public Student findByUsernameAndRealnameAndStudyabroadschool(Student model) { Session session = HibernateUtil.getSessionFactory().openSession(); Student student=null; try { session.beginTransaction(); student= (Student)session.createQuery("select s from Student s where s.username='"+model.getUsername()+"' and s.realname='"+model.getRealname()+"' and s.studyabroadschool='"+model.getStudyabroadschool()+"'").uniqueResult(); session.getTransaction().commit(); }catch (Exception e) { session.getTransaction().rollback(); return null; } finally { session.close(); } return student; } @POST @Path("modifyUserPasswordByUserid") @Produces("application/xml") @Consumes("application/xml") public String modifyUserPasswordByUserid(Student model) { Session session = HibernateUtil.getSessionFactory().openSession(); try { session.beginTransaction(); //先查詢到該物件 Student student=(Student)session.get(Student.class, model.getUserid()); student.setPassword(model.getPassword()); session.update(student); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); return "1"; } finally { session.close(); } return "0"; } }