Hibernate 的查询语言 HQL 与高级查询
程序员文章站
2022-06-02 16:20:58
...
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<Department> getAllDepartmentList(){
List<Department> 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<Student> 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<Student> getStudentPageList(int pageNum, int pageSize);
/**
* 通过系院 id 取得学生信息
* @param depid
* @return
*/
public List<Student> 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<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;
}
}
修改 Student.hbm.xml 中 中的 的 many-to-one 关系
<many-to-one name="department" class="org.sf.entity.Department" fetch="select" lazy="false">
<column name="depid">
<comment> 系别id</comment>
</column>
</many-to-one>
实现 students.jsp 页面
<%@ 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">
<html>
<head>
<title> 取得并显示所有的学生</title>
</head>
<body>
<jsp:useBean id="stuDao" class="org.sf.dao.impl.StudentDaoImpl"></jsp:useBean>
<table align="center" border="1">
<tr>
<th> 学号</th>
<th> 姓名</th>
<th> 系院</th>
</tr>
<c:forEach var="stu" items="${stuDao.allStudentList}">
<tr>
<td>${stu.stunumber}</td>
<td>${stu.stuname}</td>
<td>${stu.department.mc}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
制作学生的分页显示 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">
<html>
<head>
<title> 分页显示学生信息</title>
</head>
<body>
<table align="center" border="1">
<tr>
<th> 学号</th>
<th> 姓名</th>
<th> 系院</th>
</tr>
<%
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<Student> 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){
%>
<tr>
<td><%=stu.getStunumber() %></td>
<td><%=stu.getStuname() %></td>
<td><%=stu.getDepartment().getMc() %></td>
</tr>
<%
}
}
%>
<tr><td colspan="3" align="center">
<%
out.print(" 第["+pageNum+"] 页,共["+pageSum+"] 页");
if(pageNum<2){
%>
上 上1 页
<%
}else{
%>
<a href="pagestudents.jsp?pageNum=<%=pageNum-1 %>" > 上1 页</a>
<%
}
%>
<%
if(pageNum>=pageSum){
%>
上 上1 页
<%
}else{
%>
<a href="pagestudents.jsp?pageNum=<%=pageNum+1 %>" > 下1 页</a>
<%
}
%>
</td></tr>
</table>
</body>
</html>
制作通过系院查询学生信息的 depstudents.jsp
在 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" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> 取得并显示所有的学生</title>
</head>
<body>
<jsp:useBean id="stuDao" class="org.sf.dao.impl.StudentDaoImpl"></jsp:useBean>
<jsp:useBean id="depDao" class="org.sf.dao.impl.DepartmentDaoImpl"></jsp:useBean>
<%
String strDepid = request.getParameter("depid");
List<Student> 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);
}
%>
<table align="center" border="1">
<tr>
<td colspan="3" align="center">
<form action="depstudents.jsp">
系院信息:
<select id="depid" name="depid">
<option value="0">-- 请选择--</option>
<c:forEach var="dep" items="${depDao.allDepartmentList}">
<c:choose>
<c:when test="${depid==dep.id}">
<option value="${dep.id}" selected="selected">${dep.mc}</option>
</c:when>
<c:otherwise>
<option value="${dep.id}">${dep.mc}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
<input type="submit" value=" 查询学生 " />
</form>
</td>
</tr>
<tr>
<th> 学号</th>
<th> 姓名</th>
<th> 系院</th>
</tr>
<%
if(list!=null){
for(Student stu:list){
%>
<tr>
<td><%=stu.getStunumber() %></td>
<td><%=stu.getStuname() %></td>
<td><%=stu.getDepartment().getMc() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
下面关于所用到的数据库,实体映射请参考上一篇文章 实体映射
上一篇: 浅谈网站密码输入设计该注意哪些方面