友佳书屋实训项目(三)

三、 客户模块

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 及后续所有属性的构造方法
|— 无参数构造方法

演示代码如下:

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;
【根据用户名和密码进行查询,只能够是一个客户】

代码如下:

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实现类具体操作与上篇雷同,不再截图展示具体过程。

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接口
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繁琐的环节。
具体代码如下:

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

代码如下:`

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
<%--
  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语句

-- 创建数据库
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软件进行操作,首先随便打开一个数据库:
在这里插入图片描述
然后点击 【查询】
在这里插入图片描述
然后点击【新建查询】:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
最后:
在这里插入图片描述
在这里插入图片描述


登录功能
1. 设计网页的页面
1.1 登录页面 :login.jsp
<%--
  Created by IntelliJ IDEA.
  User: Mryang
  Date: 2021/10/19
  Time: 8:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>客户登录</title>
</head>
<body>
    <form action="/customerServlet?op=login" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td>
                    <input type="text" name="username" id="username">
                </td>
            </tr>
            <tr>
                <td>密  码:</td>
                <td>
                    <input type="text" name="password" id="password">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="快速登录">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

1.2 主页页面 :main.jsp
<%--
  Created by IntelliJ IDEA.
  User: Mryang
  Date: 2021/10/19
  Time: 8:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网站后台主页</title>
</head>
<body>
        ${sessionScope.CUST.customerName},您好!欢迎您登录到友佳书屋后台管理系统!
        <hr/>

</body>
</html>

1.3 首页页面 :index.jsp
<%-- Created by IntelliJ IDEA. --%>
<%@ 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>

      <c:if test="${sessionScope.CUST != null}">
          <a href="/categoryServlet?op=categoryList">查询类目列表</a>
          <a href="addCategory.jsp">添加类目</a>
          <hr/>
          <a href="bookServlet?op=toAddBook">添加图书</a>
          <a href="bookServlet?op=bookList">查询图书列表</a>
          <hr/>
          <a href="customerServlet?op=customerList">查询客户列表</a>
      </c:if>
      <c:if test="${sessionScope.CUST == null}">
        <a href="login.jsp">登录页面</a>
      </c:if>

  </body>
</html>

2. 编写Servlet
2.1 登录功能的方法代码

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Customer customer = customerService.customerLogin(username, password);//此时, 存储在customer【查到了,用户名和密码 放在了customer | null】
        if (customer != null){//  有这个数据
            HttpSession session = request.getSession();
            session.setAttribute("CUST",customer);
            request.getRequestDispatcher("main.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","您填写的用户名和密码不正确,请查证后登录");
            request.getRequestDispatcher("message.jsp").forward(request,response);
        }
    }

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨校

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值