前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >友佳书屋实训项目(三)

友佳书屋实训项目(三)

作者头像
杨校
发布2021-10-19 09:51:05
3780
发布2021-10-19 09:51:05
举报
文章被收录于专栏:Java技术分享圈Java技术分享圈

三、 客户模块

1.分析实体

客户,单词为 Customer 实体的名称就定义下来了 定义实体类 |— 属性: 客户编号 customerId 类型是 int(用于确定的唯一性); 客户名称 customerName;类型是 String 【收件人的姓名】 客户用户名 username; 类型是 String 【客户用于登录网站的账号】 客户密码 password ; 类型是 String【客户用于登录网站的密码】 客户性别 sex ; 类型是 String 出书年月 birthday ; 类型是 String 客户地址 address 类型是 String 【收件人的地址】 电子邮箱 email{ 类型: String} 【邮箱邮件认证,确认邮箱真实性】 手机号码 telephone{ 类型: String} 【收件人的电话】 |— 方法: getter And Setter方法 toString 方法 构造方法 |— 有参数构造方法 |---- ① 带有customerId 及后续所有属性的构造方法 |---- ② 不带有customerId 及后续所有属性的构造方法 |— 无参数构造方法

演示代码如下:

代码语言:javascript
复制
package cn.javabs.entity;

public class Customer {
    private int  customerId  ;
    private String  customerName;
    private String  username ;
    private String  password ;
    private String  sex;
    private String  birthday;
    private String  address ;
    private String  email;
    private String  telephone;


    public Customer() {

    }
    public Customer(String customerName, String username, String password, String sex, String birthday, String address, String email, String telephone) {
        this.customerName = customerName;
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.birthday = birthday;
        this.address = address;
        this.email = email;
        this.telephone = telephone;
    }

    public Customer(int customerId, String customerName, String username, String password, String sex, String birthday, String address, String email, String telephone) {
        this.customerId = customerId;
        this.customerName = customerName;
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.birthday = birthday;
        this.address = address;
        this.email = email;
        this.telephone = telephone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "customerId='" + customerId + '\'' +
                ", customerName='" + customerName + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday='" + birthday + '\'' +
                ", address='" + address + '\'' +
                ", email='" + email + '\'' +
                ", telephone='" + telephone + '\'' +
                '}';
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
}

2 编写Service层

Service层有 接口和实现类 (1)先去编写接口,接口的命名规范为 [实体类的名称+ Service],本接口为BookService,实现类为BookServiceImpl (2)考虑该接口内哪些功能,功能对应的是方法。

2.1 编写Service接口

功能如下: ①客户注册 ②删除客户 ③修改客户 ④查询所有客户 ⑤根据客户名称查询模糊查询客户 ⑥客户登录 ⑦根据客户编号查询图书 对应的是七个方法: 客户注册方法, 参数列表是:Customer customer; 返回值类型为int 删除客户方法, 参数列表是:int customerId; 返回值类型为int 修改客户方法, 参数列表是Customer customer; 返回值类型为int 查询所有客户方法, 无参数 返回值类型为List; 【因为查询的所有类目,不再是单一的一个类目,需要有容器存储所有类目对象,该容器合适的是集合,便于查询的是ArrayList集合。{ArrayList特性是 查询快、增删慢 | LinkedList特性是 查询慢、增删快}】 根据客户编号查询客户, 参数列表是int customerId; 返回值类型为Customer; 根据客户名称或者是姓氏查询客户,属于模糊查询, 参数列表是String customerName; 返回值类型为List; 客户登录方法,参数列表是:String username , String password 返回值类型为Customer; 【根据用户名和密码进行查询,只能够是一个客户】

代码如下:

代码语言:javascript
复制
package cn.javabs.service;

import cn.javabs.entity.Customer;
import java.util.List;
public interface CustomerService {

    /**
     * 1.客户注册
     * @param customer
     * @return
     */
    int  customerRegister(Customer customer);

    /**
     * 2.客户登录
     * @param username
     * @param password
     * @return
     */
    Customer customerLogin(String username,String password);

    /**
     * 3.删除客户
     * @param customerId
     * @return
     */
    int  delCustomer(int customerId);

    /**
     * 4.修改客户
     * @param customer
     * @return
     */
    int editCustomer(Customer customer);

    /**
     * 5.查询所有客户
     * @return
     */
    List<Customer> findAllCustomer();

    /**
     * 6.根据客户的名称进行查询客户【模糊查询】
     * @param customerName
     * @return
     */
    Customer findCustomerByCustomerName(String customerName);

    /**
     * 7.根据客户的编号进行查询客户
     * @param customerId
     * @return
     */
    Customer findCustomerByCustomerId(int customerId);


}
2.2 编写Service实现类

CustomerServiceImpl实现类具体操作与上篇雷同,不再截图展示具体过程。

代码语言:javascript
复制
package cn.javabs.service.impl;

import cn.javabs.dao.CustomerDao;
import cn.javabs.dao.impl.CustomerDaoImpl;
import cn.javabs.entity.Customer;
import cn.javabs.service.CustomerService;

import java.util.List;

public class CustomerServiceImpl implements CustomerService {

    CustomerDao customerDao =  new CustomerDaoImpl();
    
    /**
     * 1.客户注册
     *
     * @param customer
     * @return
     */
    @Override
    public int customerRegister(Customer customer) {
        return customerDao.register(customer);
    }

    /**
     * 2.客户登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Customer customerLogin(String username, String password) {
        return customerDao.login(username,password);
    }

    /**
     * 3.删除客户
     *
     * @param customerId
     * @return
     */
    @Override
    public int delCustomer(int customerId) {
        return customerDao.del(customerId);
    }

    /**
     * 4.修改客户
     *
     * @param customer
     * @return
     */
    @Override
    public int editCustomer(Customer customer) {
        return customerDao.edit(customer);
    }

    /**
     * 5.查询所有客户
     *
     * @return
     */
    @Override
    public List<Customer> findAllCustomer() {
        return customerDao.getAllCustomer();
    }

    /**
     * 6.根据客户的名称进行查询客户【模糊查询】
     *
     * @param customerName
     * @return
     */
    @Override
    public Customer findCustomerByCustomerName(String customerName) {
        return customerDao.getCustomerByCustomerName(customerName);
    }

    /**
     * 7.根据客户的编号进行查询客户
     *
     * @param customerId
     * @
     * return
     */
    @Override
    public Customer findCustomerByCustomerId(int customerId) {
        return customerDao.getCustomerByCustomerId(customerId);
    }
}

3 编写DAO层

Dao层有 接口和实现类

3.1 编写Dao接口
代码语言:javascript
复制
package cn.javabs.dao;

import cn.javabs.entity.Customer;

import java.util.List;

public interface CustomerDao {
    int register(Customer customer);

    Customer login(String username, String password);

    int del(int customerId);

    int edit(Customer customer);

    List<Customer> getAllCustomer();

    Customer getCustomerByCustomerName(String customerName);

    Customer getCustomerByCustomerId(int customerId);
}
3.1 编写Dao实现类

命名为: CustomerDaoImpl 该实现类实现CustomerDao接口内所有的抽象方法,此处借助了Apache开源提供的数据库工具类 Dbutils包、利用该包内的QueryRunner进行完成增删改查的语句执行操作,省去了Jdbc繁琐的环节。 具体代码如下:

代码语言:javascript
复制
package cn.javabs.dao.impl;

import cn.javabs.dao.CustomerDao;
import cn.javabs.entity.Customer;
import cn.javabs.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.xml.namespace.QName;
import java.sql.SQLException;
import java.util.List;

public class CustomerDaoImpl implements CustomerDao {

    QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public int register(Customer customer) {
        try {
            return qr.update("insert into customer(customerName,username,password,sex,birthday,address,email,telephone)",
                        customer.getCustomerName(),
                        customer.getUsername(),
                        customer.getPassword(),
                        customer.getSex(),
                        customer.getBirthday(),
                        customer.getAddress(),
                        customer.getEmail(),
                        customer.getTelephone()
                    );
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Customer login(String username, String password) {
        try {
            return qr.query("select * from customer where username = ? and password = ?",new BeanHandler<Customer>(Customer.class),username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int del(int customerId) {
        try {
            return qr.update("delete from customer where customerId = ?",customerId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int edit(Customer customer) {
         try {
            return qr.update("update customer set customerName = ? , username = ? , password = ? , sex = ? , birthday = ? , address = ? , email = ?, telephone = ? where customerId = ?",
                    customer.getCustomerName(),
                    customer.getUsername(),
                    customer.getPassword(),
                    customer.getSex(),
                    customer.getBirthday(),
                    customer.getAddress(),
                    customer.getEmail(),
                    customer.getTelephone(),
                    customer.getCustomerId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Customer> getAllCustomer() {
        try {
            return qr.query("select * from customer ",new BeanListHandler<Customer>(Customer.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据客户名称【模糊查询】 重点在  sql语句内 不再是 =,而是 like
     * @param customerName
     * @return
     */
    @Override
    public Customer getCustomerByCustomerName(String customerName) {
        try {
            return qr.query("select * from customer where customerName like  ? ",new BeanHandler<Customer>(Customer.class),customerName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Customer getCustomerByCustomerId(int customerId)   {
        try {
            return qr.query("select * from customer where customerId = ? ",new BeanHandler<Customer>(Customer.class),customerId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

4. 编写CustomerServlet

代码如下:`

代码语言:javascript
复制
package cn.javabs.web.servlet;

import cn.javabs.entity.Customer;
import cn.javabs.service.CustomerService;
import cn.javabs.service.impl.CustomerServiceImpl;

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 java.io.IOException;
import java.util.List;

@WebServlet("/customerServlet")
public class CustomerServlet extends HttpServlet {

    CustomerService customerService = new CustomerServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String op = request.getParameter("op");
        switch (op){
            case "register":
                register(request,response);
            break;
            case "login":
                login(request,response);
                break;
            case "editCustomer":
                editCustomer(request,response);
                break;
            case "delCustomer":
                delCustomer(request,response);
                break;
            case "updateCustomer":
                updateCustomer(request,response);
                break;
            case "customerList":
                customerList(request,response);
                break;
            default:
                System.out.println("没有找到对应的参数!请查证");
                break;
        }
    }

    /**
     * 查询所有客户列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void customerList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        List<Customer> customerList = customerService.findAllCustomer();
        if(customerList.size()>0 && customerList != null){
            request.setAttribute("list",customerList);
            request.getRequestDispatcher("customerList.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","尚未查询到客户信息");
            request.getRequestDispatcher("message.jsp").forward(request,response);
        }
    }

    private void updateCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    }

    /**
     * 删除客户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void delCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        String sid = request.getParameter("id");
        int id = Integer.parseInt(sid);

        int row = customerService.delCustomer(id);
        if(row > 0){
            request.setAttribute("msg","删除客户成功!");
            request.getRequestDispatcher("message.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","删除客户失败!");
            request.getRequestDispatcher("message.jsp").forward(request,response);
        }
    }

    private void editCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{


    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    }

    private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    }
}

5.页面
5.1. customerList.jsp
代码语言:javascript
复制
<%--
  Created by IntelliJ IDEA.
  User: Mryang
  Date: 2021/10/18
  Time: 11:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>客户列表</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" align="center" width="900px">
    <tr>
        <th>序号</th>
        <th>客户名称</th>
        <th>客户账号</th>
        <th>客户密码</th>
        <th>客户电话</th>
        <th>客户邮箱</th>
        <th>客户性别</th>
        <th>客户生日</th>
        <th>客户地址</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${list}" var="item">
        <tr>
            <td>${item.customerId}</td>
            <td>${item.customerName}</td>
            <td>${item.username}</td>
            <td>${item.password}</td>
            <td>${item.telephone}</td>
            <td>${item.email}</td>
            <td>${item.sex}</td>
            <td>${item.birthday}</td>
            <td>${item.address}</td>
            <td>
                <a href="JavaScript:alert('略')">修改</a>
                <a href="JavaScript:delCustomer(${item.customerId})">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>
<script>
    function delCustomer(id) {
        console.log("id是" + id);
        var sure = confirm("您确定要删除该客户吗?");
        if(sure){
            location.href="customerServlet?op=delCustomer&id=" + id;
        }

    }
</script>

6. 编写Sql语句
代码语言:javascript
复制
-- 创建数据库
create  database bookmarket;

-- 选中数据库
use bookmarket;

-- 1. 创建  类目 数据表
CREATE  table  category(
   categoryId int primary key auto_increment, --  auto_increment  自动递增
   categoryName varchar(50),
   categoryDescription varchar(250)
);


-- 2. 创建  图书 数据表
CREATE  table  book(
   bookId int primary key auto_increment, --  auto_increment  自动递增
   bookName varchar(50),
   publish varchar(100),
   price double,
   author varchar (50),
   count int ,
   bookPic varchar (500),
   categoryId int,
   --  外键 categoryId  指向  category主键categoryId
   foreign  key(categoryId) references category(categoryId)
);

-- 3. 创建  客户 数据表
CREATE  table  customer(
    customerId    int primary key auto_increment, --  auto_increment  自动递增
    customerName  varchar(50),
    username   varchar(50),
    password   varchar(50),
    sex  varchar(10),
    birthday  varchar(50),
    address   varchar(500),
    email  varchar(50),
    telephone  varchar(50)
);

利用可视化操作台Navicat软件进行操作,首先随便打开一个数据库:

在这里插入图片描述
在这里插入图片描述

然后点击 【查询】

在这里插入图片描述
在这里插入图片描述

然后点击【新建查询】:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

最后:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-10-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 三、 客户模块
    • 1.分析实体
      • 2 编写Service层
        • 2.1 编写Service接口
        • 2.2 编写Service实现类
      • 3 编写DAO层
        • 3.1 编写Dao接口
        • 3.1 编写Dao实现类
      • 4. 编写CustomerServlet
        • 5.页面
          • 6. 编写Sql语句
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档