Java Web应用案例 购物网(一)

Java Web实训项目:购物网

Java Web应用案例 购物网(一)

Java Web应用案例 购物网(二)

利用该项目的开发,采用MVC模式整合JSP + Servlet + DB(DAO),了解Web开发的一般流程。

一、功能需求

1、只有注册用户成功登录之后才可查看商品类别,查看商品,选购商品,生成订单、查看订单。

2、只有管理员才有权限进入购物网后台管理,进行用户管理、类别管理、商品管理与订单管理。

二、设计思路

1、采用MVC设计模式

分层架构:展现层(JSP)<——>控制层(Servlet)<——>业务层(Service)<——>模型层(Dao)<——>数据库(DB)

2、前台

(1)登录——显示商品类别——显示某类商品信息——查看购物车——生成订单——支付

(2)注册<——>登录

3、后台

(1)用户管理:用户的增删改查

(2)类别管理:商品类别的增删改查

(3)商品管理:商品的增删改查

(4)订单管理:订单的查看与删除

说明:只完成了用户管理中的查看用户功能。其他功能,有兴趣的不妨拿来操练一下。

4、西蒙购物网业务流程图

在这里插入图片描述

三、实现步骤

1、创建数据库

在这里插入图片描述
SQL代码:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `t_category`
-- ----------------------------
DROP TABLE IF EXISTS `t_category`;
CREATE TABLE `t_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品类别标识符',
  `name` varchar(100) NOT NULL COMMENT '商品类别名称',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_category
-- ----------------------------
INSERT INTO `t_category` VALUES ('1', '家用电器');
INSERT INTO `t_category` VALUES ('2', '床上用品');
INSERT INTO `t_category` VALUES ('3', '文具用品');
INSERT INTO `t_category` VALUES ('4', '休闲食品');

-- ----------------------------
-- Table structure for `t_order`
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单标识符',
  `username` varchar(20) DEFAULT NULL COMMENT '用户名',
  `telephone` varchar(11) DEFAULT NULL COMMENT '电话号码',
  `total_price` double DEFAULT NULL COMMENT '总金额',
  `delivery_address` varchar(50) DEFAULT NULL COMMENT '送货地址',
  `order_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '下单时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_order
-- ----------------------------
INSERT INTO `t_order` VALUES ('1', '郑晓红', '13956567889', '2000', '泸职院信息工程系', '2016-12-25 17:12:36');
INSERT INTO `t_order` VALUES ('2', '温志军', '13956678907', '1000', '泸职院机械工程系', '2016-12-02 17:12:17');

-- ----------------------------
-- Table structure for `t_product`
-- ----------------------------
DROP TABLE IF EXISTS `t_product`;
CREATE TABLE `t_product` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品标识符',
  `name` varchar(200) NOT NULL COMMENT '商品名称',
  `price` double DEFAULT NULL COMMENT '商品单价',
  `add_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `category_id` int(11) DEFAULT NULL COMMENT '商品类别标识符',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_product
-- ----------------------------
INSERT INTO `t_product` VALUES ('1', '容声电冰箱', '2000', '2016-12-20 09:54:41', '1');
INSERT INTO `t_product` VALUES ('2', '松下电视', '5000', '2016-12-20 09:54:35', '1');
INSERT INTO `t_product` VALUES ('3', '红岩墨水', '3', '2016-12-20 09:56:05', '3');
INSERT INTO `t_product` VALUES ('4', '海尔洗衣机', '1000', '2016-11-30 08:58:09', '1');
INSERT INTO `t_product` VALUES ('5', '新宇电饭煲', '1200', '2016-12-20 09:55:11', '1');
INSERT INTO `t_product` VALUES ('6', '英雄微波炉', '600', '2016-12-20 09:55:39', '1');
INSERT INTO `t_product` VALUES ('7', '红双喜席梦思', '700', '2016-11-28 08:59:38', '2');
INSERT INTO `t_product` VALUES ('8', '旺仔牛奶糖', '24.4', '2016-12-20 10:00:11', '4');
INSERT INTO `t_product` VALUES ('9', '西蒙枕头', '100', '2016-12-20 09:56:57', '2');
INSERT INTO `t_product` VALUES ('10', '甜甜毛毯', '400', '2016-12-20 09:57:26', '2');
INSERT INTO `t_product` VALUES ('11', '永久钢笔', '50', '2016-12-20 09:57:30', '3');
INSERT INTO `t_product` VALUES ('12', '硬面抄笔记本', '5', '2016-12-20 09:57:53', '3');
INSERT INTO `t_product` VALUES ('13', '晨光橡皮擦', '0.5', '2016-11-30 09:02:40', '3');
INSERT INTO `t_product` VALUES ('14', '美的空调', '3000', '2016-11-03 09:03:02', '1');
INSERT INTO `t_product` VALUES ('15', '迷你深海鱼肠', '14.4', '2016-12-02 10:01:14', '4');

-- ----------------------------
-- Table structure for `t_user`
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) DEFAULT NULL,
  `telephone` varchar(11) DEFAULT NULL,
  `register_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `popedom` int(11) DEFAULT NULL COMMENT '0:管理员;1:普通用户',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', 'admin', '12345', '15734345678', '2016-12-02 08:40:35', '0');
INSERT INTO `t_user` VALUES ('2', '郑晓红', '11111', '13956567889', '2016-12-20 09:51:43', '1');
INSERT INTO `t_user` VALUES ('3', '温志军', '22222', '13956678907', '2016-12-20 09:52:36', '1');
INSERT INTO `t_user` VALUES ('4', '涂文艳', '33333', '15890905678', '2016-12-05 09:52:56', '1');

2、创建实体类

在这里插入图片描述

User实体
package net.syp.shop.bean;

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 +
                '}';
    }
}

Category实体
package net.syp.shop.bean;

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.syp.shop.bean;

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.syp.shop.bean;

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 +
                '}';
    }
}

2、创建Dao层接口与接口实现类

在这里插入图片描述

接口
package net.syp.shop.dao;

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

import net.syp.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();
}
package net.syp.shop.dao;

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

import net.syp.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();
}
package net.syp.shop.dao;

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

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

package net.syp.shop.dao;

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

import net.syp.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);
}
接口实现类
package net.syp.shop.dao.impl;

/**
 * 功能:类别数据访问接口实现类
 * 作者:宋云鹏
 * 日期: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.syp.shop.bean.Category;
import net.syp.shop.dao.CategoryDao;
import net.syp.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;
    }
}
测试
package net.syp.shop.dao.impl;

import net.syp.shop.bean.Category;
import net.syp.shop.dao.CategoryDao;
import org.junit.Test;

public class TestCategoryDaoImpl {
    @Test
    public void testInsert(){
        CategoryDao categoryDao = new CategoryDaoImpl();
        Category category = new Category();
        category.setId(5);
        category.setName("自行车");
        int count = categoryDao.insert(category);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testDeleteById(){
        CategoryDao categoryDao = new CategoryDaoImpl();
        int count = categoryDao.deleteById(5);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testUpdate(){
        CategoryDao categoryDao = new CategoryDaoImpl();
        Category category = categoryDao.findById(2);
        category.setName("床上用品");
        int count = categoryDao.update(category);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testFindByid(){
        CategoryDao categoryDao = new CategoryDaoImpl();
        System.out.println(categoryDao.findById(2));
    }
    @Test
    public void testFindAll(){
        CategoryDao categoryDao = new CategoryDaoImpl();
        System.out.println(categoryDao.findAll());
    }
}
package net.syp.shop.dao.impl;

/**
 * 功能:订单数据访问接口实现类
 * 作者:宋云鹏
 * 日期: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.syp.shop.bean.Order;
import net.syp.shop.dao.OrderDao;
import net.syp.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;
    }
}

测试
package net.syp.shop.dao.impl;

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

import java.util.Date;
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(order);
            }
        }
    }
    @Test
    public void testInsert(){
        OrderDao orderDao = new OrderDaoImpl();
        Order order = new Order();
        order.setId(3);
        order.setUsername("宋云鹏");
        order.setTelephone("19856465450");
        order.setTotalPrice(0.0);
        order.setDeliveryAddress("泸职院信息工程学院");
        order.setOrderTime(new Date());

        int count = orderDao.insert(order);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        System.out.println(order);
    }
    @Test
    public void testUpdate(){
        OrderDao orderDao = new OrderDaoImpl();
        Order order = orderDao.findById(3);
        order.setUsername("流浪者");
        order.setDeliveryAddress("太平洋");
        int count = orderDao.update(order);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        System.out.println(order);
    }
    @Test
    public void testDeleteById(){
        OrderDao orderDao = new OrderDaoImpl();
        int count = orderDao.deleteById(3);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testFindById(){
        OrderDao orderDao = new OrderDaoImpl();
        System.out.println(orderDao.findById(2));
    }
    @Test
    public void testFindList(){
        OrderDao orderDao = new OrderDaoImpl();
        System.out.println(orderDao.findLast());
    }
}
package net.syp.shop.dao.impl;

/**
 * 功能:产品数据访问接口实现类
 * 作者:宋云鹏
 * 日期: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.syp.shop.bean.Product;
import net.syp.shop.dao.ProductDao;
import net.syp.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;
    }
}

测试
package net.syp.shop.dao.impl;

import net.syp.shop.bean.Product;
import net.syp.shop.dao.CategoryDao;
import net.syp.shop.dao.ProductDao;
import org.junit.Test;

import java.security.PublicKey;
import java.util.Date;
import java.util.List;

public class TestProductDaoImpl {
    @Test
    public void testFindByCategoryId(){
        ProductDao productDao = new ProductDaoImpl();
        int categoryId = 2;
        CategoryDao categoryDao = new CategoryDaoImpl();
        //判断类别是否存在
        if (categoryDao.findById(categoryId) != null){
            //获取类别名称
            String categoryName = categoryDao.findById(categoryId).getName();
            //获取该类别商品
            List<Product> products = productDao.findByCategoryId(categoryId);
            //判断商品是否存在
            if (!products.isEmpty()){
                //遍历商品列表
                for (Product product:products){
                    System.out.println(product);
                }
            }
        }else {
            System.out.println("类别编号【"+ categoryId + "】不存在");
        }
    }
    @Test
    public void testInsert(){
        ProductDao productDao = new ProductDaoImpl();
        Product product = new Product();
        product.setId(16);
        product.setName("鱼头笔");
        product.setPrice(2);
        product.setAddTime(new Date());
        product.setCategoryId(3);
        int count = productDao.insert(product);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testDeleteById(){
        ProductDao productDao = new ProductDaoImpl();
        int count = productDao.deleteById(16);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testUpdate(){
        ProductDao productDao = new ProductDaoImpl();
        Product product = productDao.findById(2);
        product.setName("长虹电视");
        int count = productDao.update(product);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        System.out.println(product);
    }
    @Test
    public void testFindById(){
        ProductDao productDao = new ProductDaoImpl();
        System.out.println(productDao.findById(5));
    }
    @Test
    public void testFindAll(){
        ProductDao productDao = new ProductDaoImpl();
        List<Product> products = productDao.findAll();
        for (Product product : products){
            System.out.println(product);
        }
    }
}
package net.syp.shop.dao.impl;

/**
 * 功能:用户数据访问接口实现类
 * 作者:宋云鹏
 * 日期: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.syp.shop.bean.User;
import net.syp.shop.dao.UserDao;
import net.syp.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;
    }
}

测试
package net.syp.shop.dao.impl;

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

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("201028");
        user.setTelephone("1656568488");
        int count = userDao.update(user);
        if (count >0 ){
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }

        user = userDao.findById(4);
        System.out.println(user);
    }
    @Test
    public void testInsert(){
        UserDao userDao = new UserDaoImpl();
        User user = new User();
        user.setId(5);
        user.setUsername("宋云鹏");
        user.setPassword("12345");
        user.setTelephone("12345678911");
        user.setRegisterTime(new Date());
        user.setPopedom(0);


        int count = userDao.insert(user);
        if (count >0){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
    }
    @Test
    public void testDeleteById(){
        UserDao userDao = new UserDaoImpl();
        int count = userDao.deleteById(6);
        if (count > 0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }
    @Test
    public void testFindById(){
        UserDao userDao = new UserDaoImpl();
        System.out.println(userDao.findById(1));
    }
    @Test
    public void testFindByUsername(){
        String username = "宋云鹏";
        UserDao userDao = new UserDaoImpl();
        System.out.println(userDao.findByUsername(username));
    }
}

3、书写服务层并测试

在这里插入图片描述

package net.syp.shop.service;

/**
 * 功能:类别服务类
 * 作者:宋云鹏
 * 日期:2019年12月4日
 */

import java.util.List;

import net.syp.shop.bean.Category;
import net.syp.shop.dao.CategoryDao;
import net.syp.shop.dao.impl.CategoryDaoImpl;

public class CategoryService {
    /**
     * 声明类别数据访问对象
     */
    private CategoryDao categoryDao = new CategoryDaoImpl();

    public int addCategory(Category category) {
        return categoryDao.insert(category);
    }

    public int deleteCategoryById(int id) {
        return categoryDao.deleteById(id);
    }

    public int updateCategory(Category category) {
        return categoryDao.update(category);
    }

    public Category findCategoryById(int id) {
        return categoryDao.findById(id);
    }

    public List<Category> findAllCategories() {
        return categoryDao.findAll();
    }
}

测试
package net.syp.shop.service;

import net.syp.shop.bean.Category;
import net.syp.shop.dao.CategoryDao;
import net.syp.shop.dao.impl.CategoryDaoImpl;
import org.junit.Test;

import java.util.List;

public class TestCategoryService {
    @Test
    public void testAddCategory(){
        Category category = new Category();
        CategoryService categoryService = new CategoryService();
        category.setId(5);
        category.setName("自行车");
        int count = categoryService.addCategory(category);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testDeleteCategoryById(){
        CategoryService categoryService = new CategoryService();
        int count = categoryService.deleteCategoryById(5);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testUpdateCategory(){
        CategoryService categoryService = new CategoryService();
        Category category = categoryService.findCategoryById(2);
        category.setName("床上");
        int count = categoryService.updateCategory(category);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testFindCategoryById(){
        CategoryService categoryService = new CategoryService();
        System.out.println(categoryService.findCategoryById(2));
    }
    @Test
    public void testFindCategoryAll(){
        CategoryService categoryService = new CategoryService();
        List<Category> categories = categoryService.findAllCategories();
        for (Category category : categories){
            System.out.println(category);
        }
    }
}

在这里插入图片描述

package net.syp.shop.service;

/**
 * 功能:订单服务类
 * 作者:宋云鹏
 * 日期:2019年12月4日
 */

import java.util.List;

import net.syp.shop.bean.Order;
import net.syp.shop.dao.OrderDao;
import net.syp.shop.dao.impl.OrderDaoImpl;

public class OrderService {
    /**
     * 声明订单数据访问对象
     */
    OrderDao orderDao = new OrderDaoImpl();

    public int addOrder(Order order) {
        return orderDao.insert(order);
    }

    public int deleteOrderById(int id) {
        return orderDao.deleteById(id);
    }

    public int updateOrder(Order order) {
        return orderDao.update(order);
    }

    public Order findOrderById(int id) {
        return orderDao.findById(id);
    }

    public Order findLastOrder() {
        return orderDao.findLast();
    }

    public List<Order> findAllOrders() {
        return orderDao.findAll();
    }
}

测试
package net.syp.shop.service;

import net.syp.shop.bean.Order;
import net.syp.shop.dao.OrderDao;
import net.syp.shop.dao.impl.OrderDaoImpl;
import org.junit.Test;

import java.util.Date;
import java.util.List;

public class TestOrderService {

    @Test
    public void addOrder(){
        OrderService orderService = new OrderService();
        Order order = new Order();
        order.setId(3);
        order.setUsername("宋云鹏");
        order.setTelephone("19856465450");
        order.setTotalPrice(0.0);
        order.setDeliveryAddress("泸职院信息工程学院");
        order.setOrderTime(new Date());

        int count = orderService.addOrder(order);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        System.out.println(order);
    }
    @Test
    public void testDeleteOrderById(){
        OrderService orderService = new OrderService();
        int count = orderService.deleteOrderById(3);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testUpdateOrder(){
        OrderService orderService = new OrderService();
        Order order = orderService.findOrderById(2);
        order.setUsername("流浪者");
        order.setDeliveryAddress("太平洋");
        int count = orderService.updateOrder(order);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        System.out.println(order);
    }

    @Test
    public void testFindOrderById(){
        OrderService orderService = new OrderService();
        System.out.println(orderService.findOrderById(1));
    }
    @Test
    public void testFindListOrder(){
        OrderService orderService = new OrderService();
        System.out.println(orderService.findLastOrder());
    }

    @Test
    public void testFindAllOrders(){
        OrderService orderService = new OrderService();
        List<Order> orders = orderService.findAllOrders();
        for (Order order : orders){
            System.out.println(order);
        }
    }
}

在这里插入图片描述

package net.syp.shop.service;

/**
 * 功能:商品服务类
 * 作者:宋云鹏
 * 日期:2019年12月4日
 */

import java.util.List;

import net.syp.shop.bean.Product;
import net.syp.shop.dao.ProductDao;
import net.syp.shop.dao.impl.ProductDaoImpl;

public class ProductService {
    /**
     * 声明商品数据访问对象
     */
    private ProductDao productDao = new ProductDaoImpl();

    public int addProduct(Product product) {
        return productDao.insert(product);
    }

    public int deleteProductById(int id) {
        return productDao.deleteById(id);
    }

    public int updateProduct(Product product) {
        return productDao.update(product);
    }

    public Product findProductById(int id) {
        return productDao.findById(id);
    }

    public List<Product> findProductsByCategoryId(int categoryId) {
        return productDao.findByCategoryId(categoryId);
    }

    public List<Product> findAllProducts() {
        return productDao.findAll();
    }
}
测试
package net.syp.shop.service;

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

import java.util.Date;
import java.util.List;

public class TestProductService {

    @Test
    public void testAddProduct(){
        ProductService productService = new ProductService();
        Product product = new Product();
        product.setId(16);
        product.setName("鱼头笔");
        product.setPrice(2);
        product.setAddTime(new Date());
        product.setCategoryId(3);
        int count = productService.addProduct(product);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testDeleteProductById(){
        ProductService productService = new ProductService();
        int count = productService.deleteProductById(16);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
    }
    @Test
    public void testUpdateProduct(){
        ProductService productService = new ProductService();
        Product product = productService.findProductById(2);
        product.setName("长虹");
        int count = productService.updateProduct(product);
        if (count > 0){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        System.out.println(product);
    }
    @Test
    public void testFindById(){
        ProductService productService = new ProductService();
        System.out.println(productService.findProductById(6));
    }
    @Test
    public void testFindProductsByCategoryId(){
        ProductService productService = new ProductService();
        int categoryId = 1;
        CategoryService categoryService = new CategoryService();
        //判断类别是否存在
        if (categoryService.findCategoryById(categoryId) != null){
            //获取类别名称
            String categoryName = categoryService.findCategoryById(categoryId).getName();
            //获取该类别商品
            List<Product> products = productService.findProductsByCategoryId(categoryId);
            //判断商品是否存在
            if (!products.isEmpty()){
                //遍历商品列表
                for (Product product:products){
                    System.out.println(product);
                }
            }
        }else {
            System.out.println("类别编号【"+ categoryId + "】不存在");
        }
    }
    @Test
    public void testFindAllProducts(){
        ProductService productService  = new ProductService();
        List<Product> products = productService.findAllProducts();
        for (Product product : products){
            System.out.println(product);
        }
    }
}

在这里插入图片描述

package net.syp.shop.service;

/**
 * 功能:用户服务类
 * 作者:宋云鹏
 * 日期:2019年12月4日
 */

import java.util.List;

import net.syp.shop.bean.User;
import net.syp.shop.dao.UserDao;
import net.syp.shop.dao.impl.UserDaoImpl;

public class UserService {
    /**
     * 声明用户访问对象
     */
    private UserDao userDao = new UserDaoImpl();

    public int addUser(User user) {
        return userDao.insert(user);
    }

    public int deleteUserById(int id) {
        return userDao.deleteById(id);
    }

    public int updateUser(User user) {
        return userDao.update(user);
    }

    public User findUserById(int id) {
        return userDao.findById(id);
    }

    public List<User> findUsersByUsername(String username) {
        return userDao.findByUsername(username);
    }

    public List<User> findAllUsers() {
        return userDao.findAll();
    }

    public User login(String username, String password) {
        return userDao.login(username, password);
    }
}
package net.syp.shop.service;

import net.syp.shop.bean.User;
import net.syp.shop.dao.UserDao;
import net.syp.shop.dao.impl.UserDaoImpl;
import org.junit.Test;

import java.util.Date;

public class TestUserService {
    @Test
    public void testAddUser(){
        UserService userService = new UserService();
        User user = new User();
        user.setId(5);
        user.setUsername("流浪者");
        user.setPassword("12345");
        user.setTelephone("12345678911");
        user.setRegisterTime(new Date());
        user.setPopedom(0);


        int count = userService.addUser(user);
        if (count >0){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
    }
    @Test
    public void testDeleteUserById(){
        UserService userService = new UserService();
        int count = userService.deleteUserById(8);
        if (count > 0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }
    @Test
    public void testUpdateUser(){
        //创建用户数据访问对象
        UserService userService = new UserService();

        //找到用户,id为4;
        User user = userService.findUserById(4);
        //修改密码与电话
        user.setPassword("20102228");
        user.setTelephone("1656568488");
        int count = userService.updateUser(user);
        if (count >0 ){
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }

        user = userService.findUserById(4);
        System.out.println(user);
    }
    @Test
    public void testFindById(){
        UserService userService = new UserService();
        System.out.println(userService.findUserById(1));
    }
    @Test
    public void testFindByUsername(){
        String username = "宋云鹏";
        UserService userService = new UserService();
        System.out.println(userService.findUsersByUsername(username));
    }
    @Test
    public void testLogin(){
        String username,password;

        username = "admin";
        password = "12345";

        //创建用户数据访问对象
        UserService userService = new UserService();
        User user = userService.login(username,password);

        if (user != null){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值