完成任务
一查询表所有记录,把记录显示页面中
1、servlet里面调用service,service调用dao
2、在dao里面使用hibernate实现操作
3、在页面中显示所有数据
3.1、在servlet里面把list集合放到域对象
3.2、在jsp中使用el表达式+foreach标签获取
dao层代码:
package cn.com.dao;
public class Customer {
//实体类
private long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_moblie;
public long getCust_id() {
return cust_id;
}
public void setCust_id(long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_moblie() {
return cust_moblie;
}
public void setCust_moblie(String cust_moblie) {
this.cust_moblie = cust_moblie;
}
public Customer(long cust_id, String cust_name, String cust_source,
String cust_industry, String cust_level, String cust_phone,
String cust_moblie) {
super();
this.cust_id = cust_id;
this.cust_name = cust_name;
this.cust_source = cust_source;
this.cust_industry = cust_industry;
this.cust_level = cust_level;
this.cust_phone = cust_phone;
this.cust_moblie = cust_moblie;
}
public Customer() {
super();
}
}
service层
package cn.com.service;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import cn.com.dao.Customer;
import cn.com.util.Hibernate_Utils;
public class Customer_Service {
/*author:命运的信徒
* time:
* 2018/11/23
* arm:连接hibernate操作数据库
*/
public static List<Customer> select(){
//1.连接数据库
Session session=Hibernate_Utils.openSession();
//2.使用核心查询技术Criteria
Criteria qbc=session.createCriteria(Customer.class);
//3.把结果放入集合里面
List<Customer> list=qbc.list();
session.close();
return list;
}
}
servlet层
package cn.com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.dao.Customer;
import cn.com.service.Customer_Service;
public class Customer_Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Customer> list=Customer_Service.select();
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
jsp页面
我没有做页面美化,单纯的只是显示出来
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>内容详情面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<table>
<tr><td>编号</td><td>姓名</td><td>分数</td><td>种族</td><td>等级</td><td>手机号码</td><td>电话号码</td></tr>
<c:forEach items="${list}" var="i" >
<tr><td>${i.cust_id}</td><td>${i.cust_name}</td><td>${i.cust_source}</td><td>${i.cust_industry}</td><td>${i.cust_level}</td><td>${i.cust_phone}</td><td>${i.cust_moblie}</td></tr>
</c:forEach>
</table>
</body>
</html>
我利用的是一个页面Demo.jsp页面的一个链接,通过连接访问servlet,然后跳转到index,jsp页面上也就是c标签显示的页面
<a href="/Hibernate/Customer_Servlet">显示出来</a>