JAVA Web 实训项目 西蒙购物网(上)

这篇博客介绍了JAVA Web 实训项目——西蒙购物网的实现过程,包括创建数据库、建立Web项目、创建实体类、数据库工具类ConnectionManager、数据访问接口(Dao层)及其实现类,以及各 Dao 层接口的测试。内容详细涵盖了从数据库设计到 Dao 层服务的测试方法。
摘要由CSDN通过智能技术生成

(一)创建数据库

创建MySQL数据库simonshop,包含四张表:用户表(t_user)、类别表(t_category)、商品表(t_product)和订单表(t_order)。
在这里插入图片描述
(二)创建Web项目simonshop

1、创建Web项目simonshop
2、在项目结构窗口里设置Artifacts名称:simonshop
3、配置服务器(Server)
4、设置部署(Deployment)
(三)创建实体类

在src里创建net.lfh.shop.bean包,创建四个实体类:User、Category、Product与Order,与四张表t_user、t_category、t_product与t_order一一对应。
在这里插入图片描述
1、用户实体类User

package net.lfh.shop.bean;

/**
 * 功能:用户实体类
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.Date;

public class User {
   
    /**
     * 用户标识符
     */
    private int id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 电话号码
     */
    private String telephone;
    /**
     * 注册时间
     */
    private Date registerTime;
    /**
     * 权限(0:管理员;1:普通用户)
     */
    private int popedom;

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    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 getTelephone() {
   
        return telephone;
    }

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

    public Date getRegisterTime() {
   
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
   
        this.registerTime = registerTime;
    }

    public int getPopedom() {
   
        return popedom;
    }

    public void setPopedom(int popedom) {
   
        this.popedom = popedom;
    }

    @Override
    public String toString() {
   
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", telephone='" + telephone + '\'' +
                ", registerTime=" + registerTime +
                ", popedom=" + popedom +
                '}';
    }
}

2、类别实体类Category

package net.lfh.shop.bean;

/**
 * 功能:商品类别实体类
 * 作者:李福华
 * 日期:2019年12月5日
 */
public class Category {
   
    /**
     * 类别标识符
     */
    private int id;
    /**
     * 类别名称
     */
    private String name;

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    @Override
    public String toString() {
   
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

3、商品实体类Product

package net.lfh.shop.bean;

/**
 * 功能:商品实体类
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.Date;

public class Product {
   
    /**
     * 商品标识符
     */
    private int id;
    /**
     * 商品名称
     */
    private String name;
    /**
     * 商品单价
     */
    private double price;
    /**
     * 商品上架时间
     */
    private Date addTime;
    /**
     * 商品所属类别标识符
     */
    private int categoryId;

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public double getPrice() {
   
        return price;
    }

    public void setPrice(double price) {
   
        this.price = price;
    }

    public Date getAddTime() {
   
        return addTime;
    }

    public void setAddTime(Date addTime) {
   
        this.addTime = addTime;
    }

    public int getCategoryId() {
   
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
   
        this.categoryId = categoryId;
    }

    @Override
    public String toString() {
   
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", addTime=" + addTime +
                ", categoryId=" + categoryId +
                '}';
    }
}

4、订单实体类Order

package net.lfh.shop.bean;

/**
 * 功能:订单实体类
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.Date;

public class Order {
   
    /**
     * 订单标识符
     */
    private int id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 联系电话
     */
    private String telephone;
    /**
     * 订单总金额
     */
    private double totalPrice;
    /**
     * 送货地址
     */
    private String deliveryAddress;
    /**
     * 下单时间
     */
    private Date orderTime;

    public int getId() {
   
        return id;
    }

    public void setId(int id) {
   
        this.id = id;
    }

    public String getUsername() {
   
        return username;
    }

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

    public String getTelephone() {
   
        return telephone;
    }

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

    public double getTotalPrice() {
   
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
   
        this.totalPrice = totalPrice;
    }

    public String getDeliveryAddress() {
   
        return deliveryAddress;
    }

    public void setDeliveryAddress(String deliveryAddress) {
   
        this.deliveryAddress = deliveryAddress;
    }

    public Date getOrderTime() {
   
        return orderTime;
    }

    public void setOrderTime(Date orderTime) {
   
        this.orderTime = orderTime;
    }

    @Override
    public String toString() {
   
        return "Order{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", telephone='" + telephone + '\'' +
                ", totalPrice=" + totalPrice +
                ", deliveryAddress='" + deliveryAddress + '\'' +
                ", orderTime=" + orderTime +
                '}';
    }
}

(四)创建数据库工具类ConnectionManager

1、在web\WEB-INF目录下创建lib子目录,添加MySQL驱动程序的jar包

在这里插入图片描述
2、在src下创建net.lfh.shop.dbutil包,在里面创建ConnectionManager类
在这里插入图片描述

package net.lfh.shop.dbutil;

/**
 * 功能:数据库连接管理类
 * 作者:李福华
 * 日期:2019年12月5日
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class ConnectionManager {
   
    /**
     * 数据库驱动程序
     */
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    /**
     * 数据库统一资源标识符
     */
    private static final String URL = "jdbc:mysql://localhost:3306/simonshop";
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "123456";

    /**
     * 私有化构造方法,拒绝实例化
     */
    private ConnectionManager() {
   
    }

    /**
     * 获取数据库连接静态方法
     *
     * @return 数据库连接对象
     */
    public static Connection getConnection() {
   
        // 定义数据库连接
        Connection conn = null;
        try {
   
            // 安装数据库驱动程序
            Class.forName(DRIVER);
            // 获得数据库连接
            conn = DriverManager.getConnection(URL
                    + "?useUnicode=true&characterEncoding=UTF8", USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
   
            e.printStackTrace();
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        // 返回数据库连接
        return conn;
    }

    /**
     * 关闭数据库连接静态方法
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) {
   
        // 判断数据库连接是否为空
        if (conn != null) {
   
            // 判断数据库连接是否关闭
            try {
   
                if (!conn.isClosed()) {
   
                    // 关闭数据库连接
                    conn.close();
                }
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试数据库连接是否成功
     *
     * @param args
     */
    public static void main(String[] args) {
   
        // 获得数据库连接
        Connection conn = getConnection();
        // 判断是否连接成功
        if (conn != null) {
   
            JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
        } else {
   
            JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
        }

        // 关闭数据库连接
        closeConnection(conn);
    }
}

运行程序,查看结果:
在这里插入图片描述
(五)数据访问接口 (Dao层)
在src里创建net.lfh.shop.dao包,在里面创建UserDao、CategoryDao、ProductDao与OrderDao。
在这里插入图片描述
1、用户数据访问接口UserDao

package net.lfh.shop.dao;

/**
 * 功能:用户数据访问接口
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.List;

import net.lfh.shop.bean.User;

public interface UserDao {
   
    // 插入用户
    int insert(User user);
    // 按标识符删除用户
    int deleteById(int id);
    // 更新用户
    int update(User user);
    // 按标识符查询用户
    User findById(int id);
    // 按用户名查询用户
    List<User> findByUsername(String username);
    // 查询全部用户
    List<User> findAll();
    // 用户登录
    User login(String username, String password);
}

2、类别数据访问接口CategoryDao

package net.lfh.shop.dao;

/**
 * 功能:类别数据访问接口
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.List;

import net.lfh.shop.bean.Category;

public interface CategoryDao {
   
    // 插入类别
    int insert(Category category);
    // 按标识符删除类别
    int deleteById(int id);
    // 更新类别
    int update(Category category);
    // 按标识符查询类别
    Category findById(int id);
    // 查询全部类别
    List<Category> findAll();
}

3、商品数据访问接口ProductDao

package net.lfh.shop.dao;

/**
 * 功能:商品数据访问接口
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.List;

import net.lfh.shop.bean.Product;

public interface ProductDao {
   
    // 插入商品
    int insert(Product product);
    // 按标识符删除商品
    int deleteById(int id);
    // 更新商品
    int update(Product product);
    // 按标识符查询商品
    Product findById(int id);
    // 按类别查询商品
    List<Product> findByCategoryId(int categoryId);
    // 查询全部商品
    List<Product> findAll();
}

4、订单数据访问接口OrderDao

package net.lfh.shop.dao;

/**
 * 功能:订单数据访问接口
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.util.List;

import net.lfh.shop.bean.Order;

public interface OrderDao {
   
    // 插入订单
    int insert(Order order);
    // 按标识符删除订单
    int deleteById(int id);
    // 更新订单
    int update(Order order);
    // 按标识符查询订单
    Order findById(int id);
    // 查询最后一个订单
    Order findLast();
    // 查询全部订单
    List<Order> findAll();
}

(六)数据访问接口实现类(XXXDaoImpl)
在src下创建net.lfh.shop.dao.impl包,在里面创建UserDaoImpl、CategoryDaoImpl、ProductDaoImpl与OrderDaoImpl。
在这里插入图片描述
1、用户数据访问接口实现类UserDaoImpl

package net.lfh.shop.dao.impl;

/**
 * 功能:用户数据访问接口实现类
 * 作者:李福华
 * 日期:2019年12月5日
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.lfh.shop.bean.User;
import net.lfh.shop.dao.UserDao;
import net.lfh.shop.dbutil.ConnectionManager;

public class UserDaoImpl implements UserDao {
   
    /**
     * 插入用户
     */
    @Override
    public int insert(User user) {
   
        // 定义插入记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "INSERT INTO t_user (username, password, telephone, register_time, popedom)"
                + " VALUES (?, ?, ?, ?, ?)";
        try {
   
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getTelephone());
            pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
            pstmt.setInt(5, user.getPopedom());
            // 执行更新操作,插入新记录
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
   
            e.printStackTrace();
        } finally {
   
            ConnectionManager.closeConnection(conn);
        }

        // 返回插入记录数
        return count;
    }

    /**
     * 删除用户记录
     */
    @Override
    public int deleteById(int id) {
   
        // 定义删除记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "DELETE FROM t_user WHERE id = ?";
        try {
   
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行更新操作,删除记录
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
   
            e.printStackTrace();
        } finally {
   
            ConnectionManager.closeConnection(conn);
        }

        // 返回删除记录数
        return count;
    }

    /**
     * 更新用户
     */
    @Override
    public int update(User user) {
   
        // 定义更新记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "UPDATE t_user SET username = ?, password = ?, telephone = ?,"
                + " register_time = ?, popedom = ? WHERE id = ?";
        try {
   
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getTelephone());
            pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
            pstmt.setInt(5, user.getPopedom());
            pstmt.setInt(6, user.getId());
            // 执行更新操作,更新记录
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
   
            e.printStackTrace();
        } finally {
   
            ConnectionManager.closeConnection(conn);
        }

        // 返回更新记录数
        return count;
    }

    /**
     * 按标识符查询用户
     */
    @Override
    public User findById(int id) {
   
        // 声明用户
        User user = null;

        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_user WHERE id = ?";
        try {
   
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
   
                // 实例化用户
                user = n
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值