Hibernate Query Language HQL and advanced query
Development of database operation implementation class
Import Java.util.list;import org.hibernate.query;import Org.hibernate.sqlquery;import Org.hibernate.Session;import Org.hibernate.transaction;import Org.sf.dao.idepartmentdao;import Org.sf.entity.department;import Org.sf.util.HibernateSessionFactory;
public class Departmentdaoimpl implements Idepartmentdao {/** * Get all the Faculties information collection * @return */@SuppressWarnings ("unchecked") pu Blic list<department> getalldepartmentlist () {list<department> List = null; Session session = Hibernatesessionfactory.getsession (); Query q=session.createquery ("from Department"); list = Q.list (); Session.close (); return list;} /** * Get a Department info by ID * @param ID * @return */public Department getdepartmentbyid (int id) {Department Department = null; Session session = Hibernatesessionfactory.getsession (); Query Q =session.createquery ("from Department where Id=:id"). Setparameter ("id", id);d epartment= (Department) Q.uniqueresult (); Session.close (); return department;} /** * Add Faculties Information * @param Department * @return */public Department adddepartment (Department department) {Session session = Hib Ernatesessionfactory.getsession (); Transaction tran = Session.begintransaction (); Session.save (department); Tran.commit (); Session.close (); return Department;} /** * Modify selected Faculty Information * @param Department */public void Editdepartment (Department Department) {Session session = Hibernatesessionfactory.getsession (); Transaction tran = session.begintransaction ();D epartment depedit = (Department) session.load (Department.class, Department.getid ());d EPEDIT.SETBM (DEPARTMENT.GETBM ());d EPEDIT.SETMC (DEPARTMENT.GETMC ());d Epedit.settell ( Department.gettell ());d Epedit.setleader (Department.getleader ()); Session.update (Depedit); Tran.commit (); Session.close ();} /** * Delete Department information by ID * @param ID * @return */public int Deldepartmentbyid (int id) {int i = 0; Session session = Hibernatesessionfactory.getsession (); Transaction tran = Session.begintransaction (); SQLQuery q = (sqlquery) session.createsqlquery ("Delete from Department whereid=:id"). Setparameter ("id", id); Q.executeupdate (); Tran.commit (); Session.close (); return i;}}
Fabrication of Istudentdao interface and Studentdaoimpl
Import java.util.list;import org.sf.entity.student;/** * Student Database Operation interface * @author Vincent Song * @date 2013-04-25 */public interface is Tudentdao {/** * obtain all student information * @return */public list<student> getallstudentlist ();/** * Get a student's information by ID * @param ID * @return */public Student getstuentbyid (int id);/** * Obtain student information by learning number and password for login * @param stunumber * @param stupwd * @return */ Public Student getstudentbystunumberandstupwd (string stunumber, String stupwd),/** * Total number of students obtained * @return */public int get Studentcount ();/** * Get student Paging information * @param pagenum * @param pageSize * @return */public list<student> Getstudentpagelis T (int pagenum, int pageSize);/** * Get student information by Department ID * @param depid * @return */public list<student> Getstudentlistby Depid (int depid);/** * Add student information * @param student * @return */public student addstudent (student student);/** * Modify selected Student information * @param student */public void editstudent (student student);/** * Delete Student information * @param ID * @return */public int Delstudentbyid ( int id);}
Development of database operation classes
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;/** * Student Database Operation Implementation class * @author Vincent Song * @date 2013-04-25 */public class Studentdaoimpl implements Istuden Tdao {/** * obtains all student information * @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;} /** * Get a student information by 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;} /** * Obtain student information by learning number and password for login * @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 ;} /** * Get the total number of students information * @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;} /** * Access to student paging information * @param pagenum * @param pageSize * @return */@SuppressWarnings ("unchecked") public list<student> ge tstudentpagelist (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;} /** * Obtain student information through the Department ID * @param depid * @return * * * * @SuppressWarnings ("unchecked") public list<student> Getstudentlistby Depid (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;} /** * Add student information * @param student * @return */public student addstudent (student student) {Session session = Hibernatesessionfa Ctory.getsession (); Transaction tran = Session.begintransaction (); Session.save (student); Tran.commit (); Session.close (); return student;} /** * Modify selected Student information * @param student */public void editstudent (student student) {Session session = Hibernatesessionfactory.get Session (); 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 ();} /** * Delete Student information * @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;}}
Modify the Many-to-one relationship in the Student.hbm.xml
<many-to-one name= "department" class= "org.sf.entity.Department" fetch= "select" Lazy= "false" ><column name= " Depid "><comment> Department id</comment></column></many-to-one>
Implementing the Students.jsp Page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><%@ taglib uri= "http://java.sun.com/jsp/ Jstl/core "prefix=" C "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Make the student's pagination display pagestudents.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><% @page import= " Org.sf.dao.impl.StudentDaoImpl "%><% @page import=" Org.sf.dao.IStudentDao "%><% @page import=" Org.sf.entity.Student "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Depstudents.jsp of making inquiries about students ' information through departments and colleges
Create a depstudent.jsp page under WebRoot and display all student information when you enter the page.
Click to select the Department of Information, display the student information in the Department,<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%><% @page import= "org.sf.entity.Student"% ><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >The following information about the database used, entity mapping please refer to the previous Article entity mapping
Hibernate Query Language HQL and advanced query