购物网Web项目1

一、新建数据库
1、创建MySQL数据库simonshop,包含四张表:用户表(t_user)、类别表(t_category)、商品表(t_product)和订单表(t_order)。
2、新建一个simonshop项目
二、在WEB-INF里新建lib包,在里面导入数据库架包
在这里插入图片描述
在src下创建net.hw.shop.dbutil包,在里面创建ConnectionManager类
在这里插入图片描述
三、新建包net.xyl.shop.bean
1、创建四个实体类:User、Category、Product与Order,与四张表t_user、t_category、t_product与t_order一一对应。
在这里插入图片描述
(1)User

package net.xyl.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 +

                '}';

    }

}


(2)、Category

package net.xyl.shop.bean;

public class Category {

    /**

     * 类别标识符

     */

    private int id;

    /**

     * 类别名称

     */

    private String name;



    public int getId() {

        return id;

    }



    public void setId(int id) {

        this.id = id;

    }

(3)、Product


package net.xyl.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 +

                '}';

    }

}

(4)、Order

package net.xyl.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 +

                '}';

    }

}

2、在src下创建net.hw.shop.dbutil包,在里面创建ConnectionManager类

package net.xyl.shop.dbutil;

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 = "1";



    /**

     * 私有化构造方法,拒绝实例化

     */

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

    }

}

运行结果
在这里插入图片描述
四、数据访问接口(XXXDao)
1、在src里创建net.hw.shop.dao包,在里面创建UserDao、CategoryDao、ProductDao与OrderDao。
(1)、UserDao

package net.xyl.shop.dao;

import java.util.List;



import net.xyl.shop.bean.User;



public interface UserDao {

    // 插入用户

    int insert(User user);

    // 按标识符删除用户

    int deleteById(int id);

    // 更新用户

    int update(User user);

    // 按标识符查询用户

    User findById(int id);

    // 按用户名查询用户

    List<User> findByUsername(String username);

    // 查询全部用户

    List<User> findAll();

    // 用户登录

    User login(String username, String password);

}

(2)、CategoryDao

package net.xyl.shop.dao;

import java.util.List;

import net.xyl.shop.bean.Category;

public interface CategoryDao {

    // 插入类别

    int insert(Category category);

    // 按标识符删除类别

    int deleteById(int id);

    // 更新类别

    int update(Category category);

    // 按标识符查询类别

    Category findById(int id);

    // 查询全部类别

    List<Category> findAll();

}

(3)、ProductDao

package net.xyl.shop.dao;

import java.util.List;



import net.xyl.shop.bean.Product;



public interface ProductDao {

    // 插入商品

    int insert(Product product);

    // 按标识符删除商品

    int deleteById(int id);

    // 更新商品

    int update(Product product);

    // 按标识符查询商品

    Product findById(int id);

    // 按类别查询商品

    List<Product> findByCategoryId(int categoryId);

    // 查询全部商品

    List<Product> findAll();

}

(4)、OrderDao

import java.util.List;

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

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值