java web实训项目:西蒙购物网(上)

一.创建数据库
链接:https://pan.baidu.com/s/1ttTcYgyQ3D8pd3bwtnGtrg
提取码:cjvm
1.新建查询,运行得到四张表:用户表(t_user)、类别表(t_category)、商品表(t_product)和订单表(t_order)。
在这里插入图片描述
2.表结构如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二.创建创建Web项目simonshop
1.在src里创建net.xmh.shop.bean包,创建四个实体类:User、Category、Product与Order,与四张表t_user、t_category、t_product与t_order一一对应
在这里插入图片描述
Category类代码:

package net.xmh.shop.bean;

/**
 * 功能:商品类别实体类
 * 作者:xmh
 * 日期:2019年12月4日
 */
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 + '\'' +
                '}';
    }
}

Order类代码:

package net.xmh.shop.bean;

/**
 * 功能:订单实体类
 * 作者:xmh
 * 日期:2019年12月4日
 */
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 +
                '}';
    }
}

Product类代码:

package net.xmh.shop.bean;

/**
 * 功能:商品实体类
 * 作者:xmh
 * 日期:2019年12月4日
 */
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 +
                '}';
    }
}

User类代码:

package net.xmh.shop.bean;

/**
 * 功能:用户实体类
 * 作者:xmh
 * 日期:2019年12月4日
 */
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 +
                '}';
    }
}

三.创建数据库工具类ConnectionManager
1.在web\WEB-INF目录下创建lib子目录,添加MySQL驱动程序的jar包
链接:https://pan.baidu.com/s/18BH-AyIDwb6tBALVz3y1qQ
提取码:gcn3
复制这段内容后打开百度网盘手机App,操作更方便哦
在这里插入图片描述
2.在src下创建net.hw.shop.dbutil包,在里面创建ConnectionManager类
在这里插入图片描述
ConnectionManager类代码:

package net.xmh.shop.dbutil;

/**
 * 功能:数据库连接管理类
 * 作者:xmh
 * 日期:2019年12月4日
 */

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);
    }
}

运行得到:
在这里插入图片描述
修改密码为错误密码,运行得:
在这里插入图片描述
四.数据访问接口
1.在src里创建net.hw.shop.dao包,在里面创建UserDao、CategoryDao、ProductDao与OrderDao。
在这里插入图片描述
CategoryDao代码为:

package net.xmh.shop.dao;

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

import net.xmh.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();
}

OrderDao代码为:

package net.xmh.shop.dao;

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

import net.xmh.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();
}

ProductDao代码为:

package net.xmh.shop.dao;

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

import net.xmh.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();
}

UserDao代码为:

package net.xmh.shop.dao;

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

import net.xmh.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);
}

五.数据访问接口实现类
1.在src下创建net.hw.shop.dao.impl包,在里面创建UserDaoImpl、CategoryDaoImpl、ProductDaoImpl与OrderDaoImpl
在这里插入图片描述
CategoryDaoImpl代码为:

package net.xmh.shop.dao.impl;

/**
 * 功能:类别数据访问接口实现类
 * 作者:xmh
 * 日期:2019年12月4日
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import net.xmh.shop.bean.Category;
import net.xmh.shop.dao.CategoryDao;
import net.xmh.shop.dbutil.ConnectionManager;

public class CategoryDaoImpl implements CategoryDao {
    /**
     * 插入类别
     */
    @Override
    public int insert(Category category) {
        // 定义插入记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "INSERT INTO t_category (name) VALUES (?)";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, category.getName());
            // 执行更新操作,插入新录
            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_category 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(Category category) {
        // 定义更新记录数
        int count = 0;

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

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

    /**
     * 按标识符查询类别
     */
    @Override
    public Category findById(int id) {
        // 声明商品类别
        Category category = null;

        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_category WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
                // 实例化商品类别
                category = new Category();
                // 利用当前记录字段值去设置商品类别的属性
                category.setId(rs.getInt("id"));
                category.setName(rs.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回商品类别
        return category;
    }

    /**
     * 查询全部类别
     */
    @Override
    public List<Category> findAll() {
        // 声明类别列表
        List<Category> categories = new ArrayList<Category>();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_category";
        try {
            // 创建语句对象
            Statement stmt = conn.createStatement();
            // 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 遍历结果集
            while (rs.next()) {
                // 创建类别实体
                Category category = new Category();
                // 设置实体属性
                category.setId(rs.getInt("id"));
                category.setName(rs.getString("name"));
                // 将实体添加到类别列表
                categories.add(category);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回类别列表
        return categories;
    }
}

OrderDaoImpl代码为:

package net.xmh.shop.dao.impl;
/**
 * 功能:订单数据访问接口实现类
 * 作者:华卫
 * 日期:2019年12月2日
 */

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.xmh.shop.bean.Order;
import net.xmh.shop.dao.OrderDao;
import net.xmh.shop.dbutil.ConnectionManager;

public class OrderDaoImpl implements OrderDao {
    /**
     * 插入订单
     */
    @Override
    public int insert(Order order) {
        // 定义插入记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "INSERT INTO t_order (username, telephone, total_price, delivery_address, order_time)"
                + " VALUES (?, ?, ?, ?, ?)";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, order.getUsername());
            pstmt.setString(2, order.getTelephone());
            pstmt.setDouble(3, order.getTotalPrice());
            pstmt.setString(4, order.getDeliveryAddress());
            pstmt.setTimestamp(5, new Timestamp(order.getOrderTime().getTime()));
            // 执行更新操作,插入记录
            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_order 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(Order order) {
        // 定义更新记录数
        int count = 0;

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

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

    /**
     * 查询最后一个订单
     */
    @Override
    public Order findLast() {
        // 声明订单
        Order order = null;
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_order";
        try {
            // 创建语句对象
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 定位到最后一条记录
            if (rs.last()) {
                // 创建订单实体
                order = new Order();
                // 设置实体属性
                order.setId(rs.getInt("id"));
                order.setUsername(rs.getString("username"));
                order.setTelephone(rs.getString("telephone"));
                order.setTotalPrice(rs.getDouble("total_price"));
                order.setDeliveryAddress(rs.getString("delivery_address"));
                order.setOrderTime(rs.getTimestamp("order_time"));
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回订单对象
        return order;
    }

    /**
     * 按标识符查询订单
     */
    @Override
    public Order findById(int id) {
        // 声明订单
        Order order = null;

        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_order WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
                // 实例化订单
                order = new Order();
                // 利用当前记录字段值去设置订单的属性
                order.setId(rs.getInt("id"));
                order.setUsername(rs.getString("username"));
                order.setTelephone(rs.getString("telephone"));
                order.setDeliveryAddress(rs.getString("delivery_address"));
                order.setOrderTime(rs.getTimestamp("order_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回订单
        return order;
    }

    /**
     * 查询全部订单
     */
    @Override
    public List<Order> findAll() {
        // 声明订单列表
        List<Order> orders = new ArrayList<Order>();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_order";
        try {
            // 创建语句对象
            Statement stmt = conn.createStatement();
            // 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 遍历结果集
            while (rs.next()) {
                // 创建订单实体
                Order order = new Order();
                // 设置实体属性
                order.setId(rs.getInt("id"));
                order.setUsername(rs.getString("username"));
                order.setTelephone(rs.getString("telephone"));
                order.setDeliveryAddress(rs.getString("delivery_address"));
                order.setOrderTime(rs.getTimestamp("order_time"));
                // 将实体添加到订单列表
                orders.add(order);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回用户列表
        return orders;
    }
}

ProductDaoImpl代码为:

package net.xmh.shop.dao.impl;

/**
 * 功能:产品数据访问接口实现类
 * 作者:xmh
 * 日期:2019年12月4日
 */

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.xmh.shop.bean.Product;
import net.xmh.shop.dao.ProductDao;
import net.xmh.shop.dbutil.ConnectionManager;

public class ProductDaoImpl implements ProductDao {
    /**
     * 插入商品
     */
    @Override
    public int insert(Product product) {
        // 定义插入记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "INSERT INTO t_product (name, price, add_time, category_id)" + " VALUES (?, ?, ?, ?)";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, product.getName());
            pstmt.setDouble(2, product.getPrice());
            pstmt.setTimestamp(3, new Timestamp(product.getAddTime().getTime()));
            pstmt.setInt(4, product.getCategoryId());
            // 执行更新操作,插入新记录
            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_product 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(Product product) {
        // 定义更新记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "UPDATE t_product SET name = ?, price = ?, add_time = ?,"
                + " category_id = ? WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, product.getName());
            pstmt.setDouble(2, product.getPrice());
            pstmt.setTimestamp(3, new Timestamp(product.getAddTime().getTime()));
            pstmt.setInt(4, product.getCategoryId());
            pstmt.setInt(5, product.getId());
            // 执行更新操作,更新记录
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

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

    /**
     * 按标识符查找商品
     */
    @Override
    public Product findById(int id) {
        // 声明商品
        Product product = null;
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_product WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
                // 实例化商品
                product = new Product();
                // 利用当前记录字段值去设置商品类别的属性
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setAddTime(rs.getTimestamp("add_time"));
                product.setCategoryId(rs.getInt("category_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回商品
        return product;
    }

    /**
     * 按类别查询商品
     */
    @Override
    public List<Product> findByCategoryId(int categoryId) {
        // 定义商品列表
        List<Product> products = new ArrayList<Product>();

        // 获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_product WHERE category_id = ?";
        try {
            // 创建预备语句
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, categoryId);
            // 执行SQL语句,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 遍历结果集,将其中的每条记录生成商品对象,添加到商品列表
            while (rs.next()) {
                // 实例化商品对象
                Product product = new Product();
                // 利用当前记录字段值设置实体对应属性
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setAddTime(rs.getTimestamp("add_time"));
                product.setCategoryId(rs.getInt("category_id"));
                // 将商品添加到商品列表
                products.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        // 返回商品列表
        return products;
    }

    /**
     * 查询全部商品
     */
    @Override
    public List<Product> findAll() {
        // 声明商品列表
        List<Product> products = new ArrayList<Product>();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_product";
        try {
            // 创建语句对象
            Statement stmt = conn.createStatement();
            // 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 遍历结果集
            while (rs.next()) {
                // 创建商品实体
                Product product = new Product();
                // 设置实体属性
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setPrice(rs.getDouble("price"));
                product.setAddTime(rs.getTimestamp("add_time"));
                product.setCategoryId(rs.getInt("category_id"));
                // 将实体添加到商品列表
                products.add(product);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回商品列表
        return products;
    }
}

UserDaoImpl代码为:

package net.xmh.shop.dao.impl;
/**
 * 功能:用户数据访问接口实现类
 * 作者:xmh
 * 日期:2019年12月4日
 */
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.xmh.shop.bean.User;
import net.xmh.shop.dao.UserDao;
import net.xmh.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 = new User();
                // 利用当前记录字段值去设置商品类别的属性
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTelephone(rs.getString("telephone"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
                user.setPopedom(rs.getInt("popedom"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回用户
        return user;
    }

    @Override
    public List<User> findByUsername(String username) {
        // 声明用户列表
        List<User> users = new ArrayList<User>();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_user WHERE username = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, username);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 遍历结果集
            while (rs.next()) {
                // 创建类别实体
                User user = new User();
                // 设置实体属性
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTelephone(rs.getString("telephone"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
                user.setPopedom(rs.getInt("popedom"));
                // 将实体添加到用户列表
                users.add(user);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回用户列表
        return users;
    }

    @Override
    public List<User> findAll() {
        // 声明用户列表
        List<User> users = new ArrayList<User>();
        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_user";
        try {
            // 创建语句对象
            Statement stmt = conn.createStatement();
            // 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 遍历结果集
            while (rs.next()) {
                // 创建用户实体
                User user = new User();
                // 设置实体属性
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTelephone(rs.getString("telephone"));
                user.setRegisterTime(rs.getTimestamp("register_time"));
                user.setPopedom(rs.getInt("popedom"));
                // 将实体添加到用户列表
                users.add(user);
            }
            // 关闭结果集
            rs.close();
            // 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }
        // 返回用户列表
        return users;
    }

    /**
     * 登录方法
     */
    @Override
    public User login(String username, String password) {
        // 定义用户对象
        User user = null;
        // 获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?";
        try {
            // 创建预备语句对象
            PreparedStatement psmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            psmt.setString(1, username);
            psmt.setString(2, password);
            // 执行查询,返回结果集
            ResultSet rs = psmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
                // 实例化用户对象
                user = new User();
                // 用记录值设置用户属性
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTelephone(rs.getString("telephone"));
                user.setRegisterTime(rs.getDate("register_time"));
                user.setPopedom(rs.getInt("popedom"));
            }
            // 关闭结果集
            rs.close();
            // 关闭预备语句
            psmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回用户对象
        return user;
    }
}

六.创建测试类
在这里插入图片描述
TestCategoryDaoImpl代码为:

package net.xmh.shop.dao.impl;

import net.xmh.shop.bean.Category;
import net.xmh.shop.bean.Order;
import net.xmh.shop.dao.CategoryDao;
import net.xmh.shop.dao.OrderDao;
import org.junit.Test;

import java.util.List;

public class TestCategoryDaoImpl {
    @Test
    /**
     * 查询全部类别
     */
        public void testFindAll(){
            //创建类别数据访问对象
            CategoryDao categoryDao = new CategoryDaoImpl();
            //获取全部商品类别
            List<Category> categories = categoryDao.findAll();
            //判断是否有类别
            if (categories.size() > 0){
                for (Category category:categories){
                    System.out.println(category);
                }
            }else{
                System.out.println("没有商品类别!");
            }
        }
    @Test
    /**
     * 按照标识符删除订单
     */
    public void testDeleteById() {
        CategoryDao categoryDao = new CategoryDaoImpl();
        Category category = categoryDao.findById(1);
        category.setId(1);
        categoryDao.deleteById(1);
        int count = categoryDao.deleteById(1);
        if (count > 0) {
            System.out.println("遗憾,删除失败!");
        } else {
            System.out.println("恭喜,删除成功!");
        }
    }
    @Test
    /**
     * 更新类别
     */
    public  void  testUpdate(){
        CategoryDao categoryDao=new CategoryDaoImpl();
        Category category=categoryDao.findById(1);
        category.setName("商用电器");
        categoryDao.update(category);
        int count=categoryDao.update(category);
        if (count>0){
            System.out.println("恭喜,更新成功");
        }else {
            System.out.println("遗憾,更新失败");
        }
    }
    @Test
    /**
     * 插入类别
     */
    public void testInsert(){
        Category category=new Category();
        category.setName("水果海鲜");
        CategoryDao dao=new CategoryDaoImpl();
        int count = dao.insert(category);
        if (count > 0) {
            System.out.println("恭喜,商品类别插入成功!");
        } else {
            System.out.println("遗憾,商品类别插入失败!");
        }
    }
    @Test
    /**
     * 按标识符查询类别
     */
    public void testFindById(){
        CategoryDao dao = new CategoryDaoImpl();
        Category category = dao.findById(1);
        System.out.println(category);
    }
}

testFindAll运行结果为:
在这里插入图片描述
对应数据库:
在这里插入图片描述
testDeleteById运行结果为:
在这里插入图片描述
对应数据库:
在这里插入图片描述
testUpdate运行结果为:
在这里插入图片描述
对应数据库:
在这里插入图片描述
testInsert运行结果为:
在这里插入图片描述
对应数据库:
在这里插入图片描述
testFindById运行结果为:
在这里插入图片描述
因为在上面已经通过删除id=1删除了信息,所以查找信息为空,修改id,运行结果为:
在这里插入图片描述
TestOrderDaoImpl类代码为:

package net.xmh.shop.dao.impl;

import net.xmh.shop.bean.Order;
import net.xmh.shop.bean.Product;
import net.xmh.shop.dao.ProductDao;
import org.junit.Test;
import net.xmh.shop.dao.OrderDao;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

public class TestOrderDaoImpl {
    @Test
    /**
     * 查询全部订单
     */
    public void testFindAll() {
        OrderDao orderDao = new OrderDaoImpl();
        List<Order> orders = orderDao.findAll();
        if (orders.size() > 0) {
            for (Order order : orders) {
                System.out.println("没有订单!");
            }
        }
    }
    @Test
    /**
     * 插入订单
     */
    public void testInsert() {
        Order order = new Order();
        order.setId(3);
        order.setUsername("嘻嘻嘻嘻");
        order.setTelephone("18087902756");
        order.setTotalPrice(1000);
        order.setDeliveryAddress("泸职院信息工程系");
        order.setOrderTime(Timestamp.valueOf(LocalDateTime.now()));
        OrderDao dao = new OrderDaoImpl();
        int count = dao.insert(order);
        if (count > 0) {
            System.out.println("恭喜,订单记录插入成功!");
        } else {
            System.out.println("遗憾,订单记录插入失败!");
        }
    }
    @Test
    /**
     * 按照标识符删除订单
     */
    public void testDeleteById() {
        OrderDao orderDao = new OrderDaoImpl();
        Order order = orderDao.findById(1);
        order.setId(1);
        orderDao.deleteById(1);
        int count = orderDao.deleteById(1);
        if (count > 0) {
            System.out.println("遗憾,删除失败!");
        } else {
            System.out.println("恭喜,删除成功!");
        }
    }
    @Test
    /**
     * 更新订单
     */
    public void testUpdate() {
        OrderDao orderDao = new OrderDaoImpl();
        Order order = orderDao.findById(1);
        order.setUsername("向晓钰");
        order.setTelephone("17234823621");
        order.setTotalPrice(1000000);
        order.setDeliveryAddress("西南医科大护理");
        orderDao.update(order);
        int count = orderDao.update(order);
        if (count > 0) {
            System.out.println("恭喜,更新成功");
        } else {
            System.out.println("遗憾,更新失败");
        }
    }
    @Test
    /**
     * 查询最后一个订单
     */
    public void testFindLast() {
        OrderDao orderDao = new OrderDaoImpl();
        Order order = orderDao.findLast();
        System.out.println(order);
        if(order !=null){
            System.out.println("恭喜,最后一条查询成功!");
    }else {
            System.out.println("遗憾,最后一条查询失败!");
        }
    }
    @Test
    /**
     * 按标识符查询订单
     */
    public void testFindById(){
        OrderDao dao = new OrderDaoImpl();
        Order order = dao.findById(1);
        System.out.println(order);
    }
}

testFindAll运行为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testInsert运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testDeleteById运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testUpdate运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testFindLast运行结果为:
在这里插入图片描述
testFindById运行结果为:
在这里插入图片描述
TestProductDaoImpl类代码:

package net.xmh.shop.dao.impl;

import net.xmh.shop.bean.Product;
import net.xmh.shop.bean.User;
import net.xmh.shop.dao.CategoryDao;
import net.xmh.shop.dao.ProductDao;
import net.xmh.shop.dao.UserDao;
import net.xmh.shop.dao.impl.CategoryDaoImpl;
import net.xmh.shop.dao.impl.ProductDaoImpl;
import org.junit.Test;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

public class TestProductDaoImpl {
    @Test
    /**
     * 按类别查询商品
     */
    public void testFindCategoryId(){
        ProductDao productDao=new ProductDaoImpl();
        int catagoryId=2;
        CategoryDao categoryDao=new CategoryDaoImpl();
        if (categoryDao.findById(catagoryId)!=null){
            String categoryName=categoryDao.findById(catagoryId).getName();
            List<Product> products=productDao.findByCategoryId(catagoryId);
            if (!products.isEmpty()){
                for (Product product:products){
                    System.out.println(product);
                }
            }else {
                System.out.println("["+categoryName+"]类别没有商品");
            }
        }else {
            System.out.println("类别编号["+catagoryId+"]不存在");
        }
    }

    @Test
    /**
     * 按标识符删除商品
     */
    public void testDeleteById(){
        ProductDao productDao = new ProductDaoImpl();
        Product product = productDao.findById(3);
        product.setId(3);
        productDao.deleteById(3);
        int count = productDao.deleteById(3);
        if (count > 0) {
            System.out.println("遗憾,删除失败!");
        } else {
            System.out.println("恭喜,删除成功!");
        }
    }
    /**
     * 插入商品
     */
    @Test
    public void testInsert() {
        Product product = new Product();
        product.setId(1);
        product.setName("计算器");
        product.setPrice(78);
        product.setAddTime(Timestamp.valueOf(LocalDateTime.now()));
        product.setCategoryId(3);

        ProductDao dao=new ProductDaoImpl();
        int count = dao.insert(product);
        if (count > 0) {
            System.out.println("恭喜,商品记录插入成功!");
        } else {
            System.out.println("遗憾,商品记录插入失败!");
        }
    }
    @Test
    /**
     * 按标识符查询商品
     */
    public void testFindById(){
        ProductDao dao = new ProductDaoImpl();
        Product product = dao.findById(8);
        System.out.println(product);
    }
    @Test
    /**
     * 更新商品
     */
    public void testUpdate(){
        ProductDao productDao=new ProductDaoImpl();
        Product product=productDao.findById(11);
        product.setName("美的空调");
        product.setPrice(250);
        productDao.update(product);
        int count=productDao.update(product);
        if (count>0){
            System.out.println("恭喜,更新成功");
        }else {
            System.out.println("遗憾,更新失败");
        }
    }
    @Test
    /**
     * 查询全部商品
     */
    public void testFindAll() {
        ProductDao dao = new ProductDaoImpl();
        String all;
        List<Product> products = dao.findAll();
        for (Product product : products) {
            System.out.println(product);
        }
    }
}


testFindCategoryId运行结果为:
在这里插入图片描述
testDeleteById运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testInsert运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testFindById运行结果为:
在这里插入图片描述
testUpdate运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testFindAll运行结果为:
在这里插入图片描述
TestUserDaoImpl代码为:

package net.xmh.shop.dao.impl;

import net.xmh.shop.bean.User;
import net.xmh.shop.dao.UserDao;
import org.junit.Test;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

public class TestUserDaoImpl {
    @Test
    /**
     * 用户登录
     */
    public void TestLogin() {
        String username, password;
        username = "admin";
        password = "12345";
        //创建用户数据访问对象
        UserDao userDao = new UserDaoImpl();
        User user = userDao.login(username, password);
        //判断用户是否登录成功
        if (user != null) {
            System.out.println("恭喜,登录成功!");
        } else {
            System.out.println("遗憾,登录失败!");
        }
    }

    @Test
    /**
     *更新用户信息
     */
    public void testUpdate() {
        //创建用户数据访问对象
        UserDao userDao = new UserDaoImpl();
        //找到涂文艳用户,其id是4
        User user = userDao.findById(4);
        //修改密码与电话
        user.setPassword("903213");
        user.setTelephone("13945457890");
        //更新涂文艳用户
        int count = userDao.update(user);
        //判断更新用户是否成功
        if (count > 0){
            System.out.println("更新成功!");
        }else{
            System.out.println("更新失败!");
        }
    }
    @Test
    /**
     * 插入用户记录
     */
    public void testInsert() {
        //创建用户数据访问对象
        User user = new User();
        user.setUsername("向铭涵");
        user.setId(9);
        user.setTelephone("18086902766");
        user.setPassword("555");
        user.setRegisterTime(Timestamp.valueOf(LocalDateTime.now()));
        user.setPopedom(1);

        UserDao dao = new UserDaoImpl();
        int count = dao.insert(user);
        if (count > 0) {
            System.out.println("恭喜,学生记录插入成功!");
        } else {
            System.out.println("遗憾,学生记录插入失败!");
        }
    }

    @Test
    /**
     * 删除用户
     */
    public void testDeleteId() {
        UserDao userDao = new UserDaoImpl();
        User user = userDao.findById(1);
        user.setId(1);
        userDao.deleteById(1);
        int count = userDao.deleteById(1);
        if (count > 0) {
            System.out.println("恭喜,删除成功!");
        } else {
            System.out.println("遗憾,删除失败!");
        }
    }

    @Test
    /**
     * 查找全部用户
     */
    public void testFindAll() {
        UserDao dao = new UserDaoImpl();
        String all;
        List<User> students = dao.findAll();
        for (User student : students) {
            System.out.println(student);
        }
    }

    /**
     * 按姓名查询用户
     */
    @Test
    public void testFindByUsername() {
        UserDao dao = new UserDaoImpl();
        String username;
        List<User> students = dao.findByUsername("向铭涵");
        for (User student : students) {
            System.out.println(student);
        }
    }
    @Test
    /**
     * 按标识符查询用户
     */
        public void testFindById(){
        UserDao dao = new UserDaoImpl();
        User user = dao.findById(1);
        System.out.println(user);
    }
}





TestLogin运行结果为:
在这里插入图片描述
testUpdate运行结果为:
在这里插入图片描述
对应数据库为:
在这里插入图片描述
testInsert运行结果为:
在这里插入图片描述
testDeleteId运行结果为:
在这里插入图片描述
testFindAll运行结果为:
在这里插入图片描述
testFindByUsername运行结果为:
在这里插入图片描述
testFindById运行结果为:
在这里插入图片描述
七.创建测试服务类
在这里插入图片描述
TestCategoryService类的代码为:

package net.xmh.shop.service;

import net.xmh.shop.bean.Category;
import org.junit.Test;

import java.util.List;

public class TestCategoryService {
    @Test
    public void testAddCategory(){
        Category category=new Category();
        category.setName("水果海鲜");

        CategoryService service=new CategoryService();
        int count=service.addCategory(category);
        if (count>0){
            System.out.println("恭喜,添加信息成功");
        }else {
            System.out.println("遗憾,添加信息失败");
        }
    }
    @Test
    public void testDeletCategory(){
        CategoryService service=new CategoryService();
        int id=5;
        int count=service.deleteCategoryById(5);
        if (count>0){
            System.out.println("恭喜,删除成功");
        }else {
            System.out.println("遗憾,删除失败");
        }
    }
    @Test
    public void testUpdateCategory(){
        CategoryService service=new CategoryService();
        Category category=service.findCategoryById(3);
        category.setName("商用电器");
        int count=service.updateCategory(category);
        if (count>0){
            System.out.println("恭喜,更新记录成功");
            category=service.findCategoryById(1);
            System.out.println(category);
        }else {
            System.out.println("遗憾,记录更新失败");
        }
    }
    @Test
    public void testFindAllCategories(){
        CategoryService service=new CategoryService();
        List<Category> categories=service.findAllCategories();
        for (Category category:categories){
            System.out.println(category);
        }
    }
}

package net.xmh.shop.service;

import net.xmh.shop.bean.Category;
import org.junit.Test;

import java.util.List;

public class TestCategoryService {
@Test
public void testAddCategory(){
Category category=new Category();
category.setName(“水果海鲜”);

    CategoryService service=new CategoryService();
    int count=service.addCategory(category);
    if (count>0){
        System.out.println("恭喜,添加信息成功");
    }else {
        System.out.println("遗憾,添加信息失败");
    }
}
@Test
public void testDeletCategory(){
    CategoryService service=new CategoryService();
    int id=5;
    int count=service.deleteCategoryById(5);
    if (count>0){
        System.out.println("恭喜,删除成功");
    }else {
        System.out.println("遗憾,删除失败");
    }
}
@Test
public void testUpdateCategory(){
    CategoryService service=new CategoryService();
    Category category=service.findCategoryById(3);
    category.setName("商用电器");
    int count=service.updateCategory(category);
    if (count>0){
        System.out.println("恭喜,更新记录成功");
        category=service.findCategoryById(1);
        System.out.println(category);
    }else {
        System.out.println("遗憾,记录更新失败");
    }
}
@Test
public void testFindAllCategories(){
    CategoryService service=new CategoryService();
    List<Category> categories=service.findAllCategories();
    for (Category category:categories){
        System.out.println(category);
    }
}

}
testAddCategory运行结果为:
在这里插入图片描述
testDeletCategory运行结果为:
在这里插入图片描述
testUpdateCategory运行结果为:在这里插入图片描述
testFindAllCategories运行结果为:
在这里插入图片描述
testAddOrder运行结果为:
在这里插入图片描述
testDeleteOrderById运行结果为:
在这里插入图片描述
testUpdateOrder运行结果为:
在这里插入图片描述
testFindAllOrders运行结果为:
在这里插入图片描述
testFindLastOrders运行结果为:
在这里插入图片描述
TestProductService代码为:

package net.xmh.shop.service;

import net.xmh.shop.bean.Product;
import net.xmh.shop.bean.User;
import org.junit.Test;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

public class TestProductService {
    @Test
    /**
     * 插入用户信息
     */
    public void testProduct() {
        Product product = new Product();
        product.setId(16);
        product.setName("围巾");
        product.setAddTime(Timestamp.valueOf(LocalDateTime.now()));
        product.setCategoryId(3);

        ProductService service = new ProductService();
        int count = service.addProduct(product);
        if (count > 0) {
            System.out.println("恭喜,商品记录插入成功!");
        } else {
            System.out.println("遗憾,商品记录插入失败!");
        }
    }
    @Test
    /**
     * 通过商品id删除商品信息
     */
    public void testDeleteProductById() {
        ProductService service = new ProductService();
        int count = service.deleteProductById(12);
        if (count > 0) {
            System.out.println("恭喜,商品记录删除成功!");
        } else {
            System.out.println("遗憾,商品记录删除失败!");
        }
    }
    @Test
    /**
     * 更新商品信息
     */
    public void testUpdateProduct() {
        ProductService service = new ProductService();
        Product product = service.findProductById(10);

        int count = service.updateProduct(product);
        if (count > 0){
            System.out.println("恭喜,商品记录更新成功!");
        }else{
            System.out.println("遗憾,商品记录更新失败!");
        }
    }
    @Test
    /**
     * 通过商品id查询商品信息
     */
    public void testFindProductById() {
        ProductService service = new ProductService();
        Product product = service.findProductById(10);
        System.out.println(product);
    }
    @Test
    /**
     * 通过商品id查找产品信息
     */
    public void testFindProductsByCategoryId(){
        ProductService service = new ProductService();
        List<Product> products = service.findProductsByCategoryId(4);
        for (Product product:products){
            System.out.println(product);
        }
    }
    @Test
    /**
     * 查找所有产品信息
     */
    public void testFindAllProducts(){
        ProductService service = new ProductService();
        List<Product> products = service.findAllProducts();
        for (Product product:products){
            System.out.println(product);
        }
    }
}

testProduct运行结果为:
在这里插入图片描述
testDeleteProductById运行结果为:
在这里插入图片描述
testUpdateProduct运行结果为:
在这里插入图片描述
testFindProductById运行结果为:
在这里插入图片描述
testFindProductsByCategoryId运行结果为:
在这里插入图片描述
testFindAllProducts运行结果为:
在这里插入图片描述
TestUserService类代码为:

package net.xmh.shop.service;

import net.xmh.shop.bean.User;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class TestUserService {
        @Test
        /**
         *测试用户登录
         */
        public void testLogin() {
            UserService service = new UserService();
            String username, password;
            username = "admin";
            password = "12345";
            User user = service.login(username, password);
            if (user != null) {
                System.out.println("恭喜,用户名与密码正确,登录成功!");
            } else {
                System.out.println("遗憾,用户名或密码错误,登录失败!");
            }
        }
    @Test
    /**
     * 测试插入用户信息
     */
    public void testAddUser() {
        User user = new User();
        user.setId(10);
        user.setUsername("琳琳");
        user.setRegisterTime(new Timestamp(new Date().getTime()));
        user.setPassword("222");

        UserService service = new UserService();
        int count = service.addUser(user);
        if (count > 0) {
            System.out.println("恭喜,学生记录插入成功!");
        } else {
            System.out.println("遗憾,学生记录插入失败!");
        }
    }
    @Test
    /**
     * 测试通过ID删除用户信息
     */
    public void testDeleteUserById() {
        UserService service = new UserService();
        String id = "2";
        int count = service.deleteUserById(1);
        if (count > 0) {
            System.out.println("恭喜,学生记录删除成功!");
        } else {
            System.out.println("遗憾,学生记录删除失败!");
        }
    }
    @Test
    /**
     * 更新用户数据
     */
    public void testUpdateUser() {
        UserService service = new UserService();
        User user = service.findUserById(1);
        user.setUsername("小小");
        int count = service.updateUser(user);
        if (count > 0) {
            System.out.println("更新成功!");
        } else {
            System.out.println("更新失败!");
        }
    }
    @Test
    /**
     * 通过id查找用户信息
     */
    public void testFindUserById() {
        UserService service = new UserService();
        User user = service.findUserById(2);
        System.out.println(user);
    }
    @Test
    /**
     * 通过用户名查找用户信息
     */
    public void testFindUserByName(){
        UserService service=new UserService();
        String username="向铭涵";
        List<User> users=service.findUsersByUsername(username);
        for (User user:users){
            System.out.println(user);
        }
    }
    @Test
    /**
     * 测试查找所有用户信息
     */
    public void testFindAllUsers() {
        UserService service = new UserService();
        List<User> users = service.findAllUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

testLogin运行结果为:
在这里插入图片描述
testAddUser运行结果为:
在这里插入图片描述
testDeleteUserById运行结果为:
在这里插入图片描述
项目完整代码为:
链接:https://pan.baidu.com/s/1L00N9R3Qn_YNLpLlNopi3Q
提取码:76ns
复制这段内容后打开百度网盘手机App,操作更方便哦

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值