今天的内容是写一个购物车项目,用到咱们最近学的servlet,过滤器去写
1.在数据库中创建表:
car_user 用户表
car_order 订单表
car_order_item 订单项表
car_goods 商品表
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;
这是在oracle数据库中创建的表与数据
2.在web项目中创建项目
把三层架构写 表示层--->biz层---->dao层
用到咱们的servlet去写biz层
注意把路径设置好 @webservlet("")
我们可以做一个设置编译码的过滤器,让每个界面都进行过滤,就不用每个界面都进行编译码设置了
package com.zking.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**编码过滤器
* @author zjjt
*
*/
@WebFilter("/*")
public class EncodingFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
//设置编码
req.setCharacterEncoding("utf-8");
//让你通过
chain.doFilter(req, resp);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
咱们还在加密了数据库中用户表的密码
所以在调用登录方法时要进行加密
String pwd=DigestUtils.md5Hex(u.getPassword());
传一个String类型 接收一个String类型
其他的都和平常三层的模式一样