今天的学习主要是完成一个购物车项目,该项目中使用servlet完成,对于不知道servlet不知道如何使用的去看servlet的使用该篇文章,该文章中有教大家如何使用servlet。
目录
购物车数据操作 :CarServlet, 我们在点击首页的加入购物车页面时,会到该类中进行数据操作,将商品添加进购物车
一.项目所需表
我们一共需要使用到四张表格,将代码数据提供给到大家,大家直接拿到数据库中运行,四张表格分别是用户表,商品表,订单表,订单详情表。
create table car_user
(
id number primary key,
account varchar2(30) not null,
password varchar(32) not null
);
comment on column car_user.ID is '用户编号';
comment on column car_user.account is '用户账户';
comment on column car_user.password is '用户密码(MD5)';
create table car_goods
(
id number primary key,
name varchar2(20) not null,
describe varchar2(100) default '此商品暂时没有介绍🤣' not null,
price number not null
);
comment on column car_goods.ID is '商品编号';
comment on column car_goods.name is '商品名称';
comment on column car_goods.describe is '商品描述';
comment on column car_goods.price is '用户价格';
create table car_order
(
id number primary key,
user_id number not null,
total number not null
);
comment on column car_order.ID is '订单编号';
comment on column car_order.user_id is '谁的订单!';
comment on column car_order.total is '订单总价';
create table car_order_item
(
id number primary key,
order_id number not null,
goods_id number not null,
quantity number not null,
total number not null
);
comment on column car_order_item.ID is '订单项编号';
comment on column car_order_item.order_id is '哪个订单!';
comment on column car_order_item.goods_id is '哪个商品!';
comment on column car_order_item.quantity is '下单数量';
comment on column car_order_item.total is '订单项总价';
comment on table car_user is '购物车用户表';
comment on table car_goods is '购物车商品表';
comment on table car_order is '购物车订单表';
comment on table car_order_item is '购物车订单项表';
create unique index car_user_account_idx on car_user (account);
insert into car_user
values (1, '2890@fox.com', 'ff9830c42660c1dd1942844f8069b74a');-- root123
insert into car_user
values (2, '2357@fox.com', 'e10adc3949ba59abbe56e057f20f883e');-- 123456
insert into car_goods
select 1, '丝袜奶茶', '冰冰娘娘,很好喝', 99
from dual
union
select 2, '勃勃奶茶', '啊~,好冰冰', 29
from dual
union
select 3, '蜜雪大补丁', '可以加个桃桃🍑', 59
from dual
union
select 4, '臭🐟咖啡', '人气最高', 88
from dual
union
select 5, '雪王咖啡', 'incredible taste', 999
from dual
union
select 6, '拉稀弹筒', '👌,就亿点点', 1
from dual;
insert into car_order_item
values (1, 1, 1, 2, 99 * 2);
insert into car_order_item
values (2, 1, 2, 3, 29 * 3);
insert into car_order_item
values (3, 1, 6, 100, 100);
insert into car_order
values (1, 1, 99 * 2 + 29 * 3 + 100);
insert into car_order_item
values (4, 2, 3, 2, 59 * 2);
insert into car_order_item
values (5, 2, 4, 3, 88 * 3);
insert into car_order_item
values (6, 2, 5, 100, 999 * 100);
insert into car_order
values (2, 2, 59 * 2 + 88 * 3 + 999 * 100);
select *
from car_user;
select *
from car_order;
select *
from car_order_item;
select *
from car_goods;
如图所示:
- 订单表中显示的数据,肯定是登录进来的用户他的所有订单数据,一个用户对应着多个订单。
- 订单详情表:对应着订单的编号,商品的编号,我们是根据自己想查看的订单的详细 数据,一个订单对应着多件商品。
二.购物车项目实操
该购物车我们具备登录,首页,将商品加入购物车,购物车界面,在实操的时候首先我们需要把需要的包导进去。
文件放置位置
1.登录操作
思路:
- 登录界面具备的方法就是去到数据库查询用户是否存在,如果用户存在我们就跳转到首页去
pojo包下的用户实体类代码如下: User
package com.yjx.pojo;
/**
* 用户实体类
* @author zjjt
*
*/
public class User {
private Integer id;
private String name;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User() {
// TODO Auto-generated constructor stub
}
public User(Integer id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
用户数据库访问层接口代码如下: IUserDao
package com.yjx.dao;
/**
* 用户数据访问层接口
* @author zjjt
*
*/
import com.yjx.pojo.User;
public interface IUserDao {
//登录验证
User login(User u);
}
用户数据库访问层代码如下: 该类中具备登录验证的方法 UserDaoImpl
package com.yjx.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.yjx.dao.IUserDao;
import com.yjx.pojo.User;
import com.yjx.util.DBHeper;
public class UserDaoImpl implements IUserDao {
private Connection con;
private PreparedStatement ps;
private ResultSet