Hibernate 的查询语言 HQL 与高级查询

Hibernate  的查询语言 HQL  与高级查询

数据库操作实现类的开发
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 {
	/**
	 *  取得所有的系院信息集合
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getAllDepartmentList(){
		List list = null;
		Session session = HibernateSessionFactory.getSession();
		Query q=session.createQuery("from Department");
		list = q.list();
		session.close();
		return list;
	}
	/**
	 *  通过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);
		department=(Department)q.uniqueResult();
		session.close();
		return department;
	}
	/**
	 *  添加系院信息
	 * @param department
	 * @return
	 */
	public Department addDepartment(Department department){
		Session session = HibernateSessionFactory.getSession();
		Transaction tran = session.beginTransaction();
		session.save(department);
		tran.commit();
		session.close();
		return department;
	}
	/**
	 *  修改选定的系院信息
	 * @param department
	 */
	public void editDepartment(Department department){
		Session session = HibernateSessionFactory.getSession();	
		Transaction tran = session.beginTransaction();
		Department depEdit = (Department) session.load(Department.class,
				department.getId());
		depEdit.setBm(department.getBm());
		depEdit.setMc(department.getMc());
		depEdit.setTell(department.getTell());
		depEdit.setLeader(department.getLeader());
		session.update(depEdit);
		tran.commit();
		session.close();
	}
	/**
	 *  通过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 where
				id=:id").setParameter("id", id);
				q.executeUpdate();
				tran.commit();
				session.close();
				return i;
	}
}
  制作 IStudentDao  接口及 及 StudentDaoImpl 

import java.util.List;
import org.sf.entity.Student;
/**
 *  学生数据库操作接口
 * @author  宋锋
 * @date 2013-04-25
 */
public interface IStudentDao {
	/**
	 *  取得所有的学生信息
	 * @return
	 */
	public List getAllStudentList();
	/**
	 *  通过 id  取得某个学生信息
	 * @param id
	 * @return
	 */
	public Student getStuentById(int id);
	/**
	 *  通过学号和密码取得学生信息,用于登录
	 * @param stunumber
	 * @param stupwd
	 * @return
	 */
	public Student getStudentByStunumberAndStupwd(String stunumber, String stupwd);
	/**
	 *  取得学生的总数信息
	 * @return
	 */
	public int getStudentCount();
	/**
	 *  取得学生分页信息
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List getStudentPageList(int pageNum, int pageSize);
	/**
	 *  通过系院 id  取得学生信息
	 * @param depid
	 * @return
	 */
	public List getStudentListByDepid(int depid);
	/**
	 *  添加学生信息
	 * @param student
	 * @return
	 */
	public Student addStudent(Student student);
	/**
	 *  修改选定的学生信息
	 * @param student
	 */
	public void editStudent(Student student);
	/**
	 *  删除学生信息
	 * @param id
	 * @return
	 */
	public int delStudentById(int id);
}
数据库操作类的开发
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 getAllStudentList(){
		List 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 getStudentPageList(int pageNum, int pageSize){
		List 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 getStudentListByDepid(int depid){
		List 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;
	}
}
修改 Student.hbm.xml 中 中的 的 many-to-one 关系


 系别id

实现 students.jsp  页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>



 取得并显示所有的学生




学号 姓名 系院
${stu.stunumber} ${stu.stuname} ${stu.department.mc}
Hibernate 的查询语言 HQL 与高级查询_第1张图片

  制作学生的分页显示 pagestudents.jsp

Hibernate 的查询语言 HQL 与高级查询_第2张图片
<%@ 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"%>



 分页显示学生信息



<%
String strPageNum = request.getParameter("pageNum");
int pageNum = 1; // 当前 页面
int pageSize = 3; // 每页的记录条数
int pageSum = 0; // 总页面数
if(strPageNum!=null) // 如果取到了数据,则为调用而来
{
pageNum = Integer.parseInt(strPageNum);
}
IStudentDao stuDao = new StudentDaoImpl();
List list = stuDao.getStudentPageList(pageNum, pageSize);
 iCount = stuDao.getStudentCount(); // 记录总数
// 计算总页面数
pageSum = iCount/pageSize;
if(iCount%pageSize!=0){
pageSum+=1;
}
if(list.size()>0){
for(Student stu:list){
%>

<%
}
}
%>
学号 姓名 系院
<%=stu.getStunumber() %> <%=stu.getStuname() %> <%=stu.getDepartment().getMc() %>
<% out.print(" 第["+pageNum+"] 页,共["+pageSum+"] 页"); if(pageNum<2){ %> 上 上1 页 <% }else{ %> 上1 页 <% } %>    <% if(pageNum>=pageSum){ %> 上 上1 页 <% }else{ %> 下1 页 <% } %>
  制作通过系院查询学生信息的 depstudents.jsp
Hibernate 的查询语言 HQL 与高级查询_第3张图片

在 WebRoot 下创建 depstudent.jsp 页面, 当进入页面时显示全部的学生信息,
点击选择系院信息后,显示该系院中的学生信息,
<%@ 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" %>



 取得并显示所有的学生




<%
String strDepid = request.getParameter("depid");
List list= null;
if(strDepid==null || strDepid.equals("") || strDepid.equals("0")){ // 查询所有的数据
list = stuDao.getAllStudentList();
}else{
int depid = Integer.parseInt(strDepid);
request.setAttribute("depid", depid);
list = stuDao.getStudentListByDepid(depid);
}
%>

<%
if(list!=null){
for(Student stu:list){
%>

<%
}
}
%>
系院信息:
学号 姓名 系院
<%=stu.getStunumber() %> <%=stu.getStuname() %> <%=stu.getDepartment().getMc() %>
下面关于所用到的数据库,实体映射请参考上一篇文章  实体映射

Hibernate 的查询语言 HQL 与高级查询_第4张图片 Hibernate 的查询语言 HQL 与高级查询_第5张图片
Hibernate 的查询语言 HQL 与高级查询_第6张图片


Hibernate 的查询语言 HQL 与高级查询_第7张图片

你可能感兴趣的:(javaEE,java学习课程,Hibernate框架的学习)