Hibernate实例——Customer表的展示

Hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- 必须的属性 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        
        <!-- 显示和格式化输出语句 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--auto属性
         create 表数据会丢失
         create-drop 自动创建,每次创建运行结束,都会把所有表删除
         update 不丢失
         validate 校验实体和数据库是否一致,不自动生成表
         -->
         <!-- 引入orm元数据 -->
         <mapping resource="com/littlepage/entity/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.littlepage.entity">
    <class name="Customer" table="t_customer">
        
        <id name="id" column="id">
            <generator class="identity">
            </generator>
        </id>
        <property name="name" column="name" ></property>
        <property name="age" column="age"></property>
    </class>
</hibernate-mapping>

entity

package com.littlepage.entity;

public class Customer {
    private Integer id;
    private String name;
    private Integer age;
    public Customer() {
        super();
    }
    public Customer(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
    
}    

servlet

package com.littlepage.servlet;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.littlepage.entity.Customer;
import com.littlepage.state.QueryCustomer;

/**
 * Servlet implementation class ShowCustomer
 */
@WebServlet("/ShowCustomer")
public class ShowCustomer extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Customer> li=QueryCustomer.getAll();
        request.setAttribute("customerlist", li);
        request.getRequestDispatcher("customerlist.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="com.littlepage.entity.Customer"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>列表</title>
</head>
<body>
<c:forEach var="customer" items="${customerlist}">
<table border="1px">
    <tr>
    <td><c:out value="${customer.id}"/></td>
    <td><c:out value="${customer.name}"/></td>
    <td><c:out value="${customer.age}"/></td>
    </tr>
</table>
</c:forEach>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/littlepage/p/10701858.html