Hibernate 的查詢語言 HQL 與高階查詢
阿新 • • 發佈:2019-02-10
修改 Student.hbm.xml 中 中的 的 many-to-one 關係import java.util.List; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.Transaction; import org.sf.entity.Student; import org.sf.util.HibernateSessionFactory; import org.sf.dao.IStudentDao; /** * 學生資料庫操作實現類 * @author 宋鋒 * @date 2013-04-25 */ public class StudentDaoImpl implements IStudentDao { /** * 取得所有的學生資訊 * @return */ @SuppressWarnings("unchecked") public List<Student> getAllStudentList(){ List<Student> list = null; Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("from Student"); list = q.list(); session.close(); return list; } /** * 通過id 取得某個學生資訊 * @param id * @return */ public Student getStuentById(int id){ Student student = null; Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("from Student where id=?"); q.setInteger(0, id); student = (Student)q.uniqueResult(); session.close(); return student; } /** * 通過學號和密碼取得學生資訊,用於登入 * @param stunumber * @param stupwd * @return */ public Student getStudentByStunumberAndStupwd(String stunumber, String stupwd){ Student student = null; Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("from Student where stunumber=? and stupwd=?"); q.setString(0, stunumber); q.setString(1, stupwd); student = (Student)q.uniqueResult(); session.close(); return student; } /** * 取得學生的總數資訊 * @return */ public int getStudentCount(){ int count = 0; Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("select count(s) from Student s"); Number number = (Number)q.uniqueResult(); count = number.intValue(); session.close(); return count; } /** * 取得學生分頁資訊 * @param pageNum * @param pageSize * @return */ @SuppressWarnings("unchecked") public List<Student> getStudentPageList(int pageNum, int pageSize){ List<Student> list = null; Session session = HibernateSessionFactory.getSession(); Query q = session.createQuery("from Student"); q.setFirstResult((pageNum-1)*pageSize); q.setMaxResults(pageSize); list = q.list(); session.close(); return list; } /** * 通過 系院id 取得學生資訊 * @param depid * @return */ @SuppressWarnings("unchecked") public List<Student> getStudentListByDepid(int depid){ List<Student> list = null; Session session = HibernateSessionFactory.getSession(); Query q= session.createQuery("from Student where department.id=?"); q.setInteger(0, depid); list = q.list(); session.close(); return list; } /** * 新增學生資訊 * @param student * @return */ public Student addStudent(Student student){ Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); session.save(student); tran.commit(); session.close(); return student; } /** * 修改選定的學生資訊 * @param student */ public void editStudent(Student student){ Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); Student stuEdit = (Student)session.load(Student.class, student.getId()); stuEdit.setStunumber(student.getStunumber()); stuEdit.setStuname(student.getStuname()); stuEdit.setStupwd(student.getStupwd()); stuEdit.setDepartment(student.getDepartment()); session.update(stuEdit); tran.commit(); session.close(); } /** * 刪除學生資訊 * @param id * @return */ public int delStudentById(int id){ int i = 0; Session session = HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); SQLQuery sq= session.createSQLQuery("delete from student where id=:id"); sq.setParameter("id", id); i = sq.executeUpdate(); tran.commit(); session.close(); return i; } }