hibernate 查询表所有记录,把记录显示页面中

完成任务

一查询表所有记录,把记录显示页面中

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 select(){
		//1.连接数据库
		Session session=Hibernate_Utils.openSession();
		//2.使用核心查询技术Criteria
		Criteria qbc=session.createCriteria(Customer.class);
		//3.把结果放入集合里面
		List 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 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+"/";
%>


  
    
    内容详情面
	
	
	    
	
	
   
  
  
  
编号姓名分数种族等级手机号码电话号码
${i.cust_id}${i.cust_name}${i.cust_source}${i.cust_industry}${i.cust_level}${i.cust_phone}${i.cust_moblie}

我利用的是一个页面Demo.jsp页面的一个链接,通过连接访问servlet,然后跳转到index,jsp页面上也就是c标签显示的页面

  显示出来

你可能感兴趣的:(c标签,hibernate)