Java web 2022跟学尚硅谷九书城项目
- 需求分析
- 数据库设计ER图
- 书城1.0简单部署项目
- 类图
- 相关代码
- UserController
- UserDAOImpl
- UserDAO
- Book
- CartItem
- Order
- OrderItem
- User
- UserServiceImpl
- UserService
- applicationContext.xml
- 书城1.1
- 实现功能
- 1. 用户登录
- 2. 首页图书列表展示
- 3. 首页价格筛选
- 4. 添加购物车
- 5.购物车中加减物品数量
- 6.购物车结算创建订单
- 7.添加过滤器,过滤未登录用户
- SessionFilter
- 总结
需求分析
- 需求分析
- 数据库设计
1) 实体分析
- 图书 Book
- 用户 User
- 订单 OrderBean
- 订单详情 OrderItem
- 购物车项 CartItem
2) 实体属性分析 - 图书 : 书名、作者、价格、销量、库存、封面、状态
- 用户 : 用户名、密码、邮箱
- 订单 : 订单编号、订单日期、订单金额、订单数量、订单状态、用户
- 订单详情 : 图书、数量、所属订单
- 购物车项 : 图书、数量、所属用户
数据库设计ER图
书城1.0简单部署项目
类图
相关代码
UserController
package com.atguigu.book.controller;
import com.atguigu.book.pojo.User;
import com.atguigu.book.service.UserService;
import com.atguigu.book.service.impl.UserServiceImpl;
/**
* @ClassName: UserController
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public class UserController {
private UserService userService;
public String login(String uname, String pwd) {
User user = userService.login(uname, pwd);
System.out.println("user = " + user);
return "index";
}
}
UserDAOImpl
package com.atguigu.book.dao.impl;
import com.atguigu.book.dao.UserDAO;
import com.atguigu.book.pojo.User;
import com.atguigu.myssm.basedao.BaseDAO;
/**
* @ClassName: UserDAOImpl
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public class UserDAOImpl extends BaseDAO<User> implements UserDAO {
/**
* @param
* @return com.atguigu.book.pojo.User
* @description //验证用户登录
* @param: uname
* @param: pwd
* @date 2022/12/12 19:37
* @author wty
**/
@Override
public User getUser(String uname, String pwd) {
String sqlStr = "SELECT * FROM bookdb.`t_user` a WHERE a.`uname` = ? AND a.`pwd` = ?";
return load(sqlStr, uname, pwd);
}
}
UserDAO
package com.atguigu.book.dao;
import com.atguigu.book.pojo.User;
/**
* @InterfaceName: UserDAO
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public interface UserDAO {
/**
* @description //验证用户登录
*
* @param
* @param: uname
* @param: pwd
* @return com.atguigu.book.pojo.User
* @date 2022/12/12 19:37
* @author wty
**/
public User getUser(String uname,String pwd);
}
Book
package com.atguigu.book.pojo;
/**
* @ClassName: Book
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public class Book {
private Integer id;
private String bookImg;
private String bookName;
private Double price;
private String author;
private Integer saleCount;
private Integer bookCount;
private Integer bookStatus = 0;
public Book() {
}
public Book(Integer id) {
this.id = id;
}
public Integer getBookStatus() {
return bookStatus;
}
public void setBookStatus(Integer bookStatus) {
this.bookStatus = bookStatus;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookImg() {
return bookImg;
}
public void setBookImg(String bookImg) {
this.bookImg = bookImg;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getSaleCount() {
return saleCount;
}
public void setSaleCount(Integer saleCount) {
this.saleCount = saleCount;
}
public Integer getBookCount() {
return bookCount;
}
public void setBookCount(Integer bookCount) {
this.bookCount = bookCount;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookImg='" + bookImg + '\'' +
", bookName='" + bookName + '\'' +
", price=" + price +
", author='" + author + '\'' +
", saleCount=" + saleCount +
", bookCount=" + bookCount +
'}';
}
}
CartItem
package com.atguigu.book.pojo;
/**
* @ClassName: CartItem
* @Description: 购物车项目 还需要一个Cart类表示购物车实体
* @Author: wty
* @Date: 2022/12/12
*/
public class CartItem {
private Integer id;
private Book book;
private Integer buyCount;
private User userBean;
public CartItem() {
}
public CartItem(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Integer getBuyCount() {
return buyCount;
}
public void setBuyCount(Integer buyCount) {
this.buyCount = buyCount;
}
public User getUserBean() {
return userBean;
}
public void setUserBean(User userBean) {
this.userBean = userBean;
}
}
Order
package com.atguigu.book.pojo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @ClassName: Order
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public class Order {
private Integer id;
private String orderNo;
private Date orderDate;
private User orderUser;
private Double orderMoney;
private Integer orderStatus;
/**
* 订单 : 订单详情 = 1 : N
*/
private List<OrderItem> orderItem;
public Order() {
}
public Order(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public Double getOrderMoney() {
return orderMoney;
}
public void setOrderMoney(Double orderMoney) {
this.orderMoney = orderMoney;
}
public Integer getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(Integer orderStatus) {
this.orderStatus = orderStatus;
}
}
OrderItem
package com.atguigu.book.pojo;
/**
* @ClassName: OrderItem
* @Description:订单详情
* @Author: wty
* @Date: 2022/12/12
*/
public class OrderItem {
private Integer id;
private Book book;
private Order orderBean;
private Integer buyCount;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Order getOrderBean() {
return orderBean;
}
public void setOrderBean(Order orderBean) {
this.orderBean = orderBean;
}
public Integer getBuyCount() {
return buyCount;
}
public void setBuyCount(Integer buyCount) {
this.buyCount = buyCount;
}
public OrderItem() {
}
public OrderItem(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "OrderItem{" +
"id=" + id +
", book=" + book +
", orderBean=" + orderBean +
", buyCount=" + buyCount +
'}';
}
}
User
package com.atguigu.book.pojo;
/**
* @ClassName: User
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public class User {
private Integer id;
private String uname;
private String pwd;
private String email;
private Integer role;
public User(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", uname='" + uname + '\'' +
", pwd='" + pwd + '\'' +
", email='" + email + '\'' +
", role=" + role +
'}';
}
}
UserServiceImpl
package com.atguigu.book.service.impl;
import com.atguigu.book.dao.UserDAO;
import com.atguigu.book.dao.impl.UserDAOImpl;
import com.atguigu.book.pojo.User;
import com.atguigu.book.service.UserService;
/**
* @ClassName: UserServiceImpl
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
/**
* @param
* @return com.atguigu.book.pojo.User
* @description //用户注册
* @param: uname
* @param: pwd
* @date 2022/12/12 19:52
* @author wty
**/
@Override
public User login(String uname, String pwd) {
return userDAO.getUser(uname, pwd);
}
}
UserService
package com.atguigu.book.service;
import com.atguigu.book.pojo.User;
/**
* @InterfaceName: UserService
* @Description:
* @Author: wty
* @Date: 2022/12/12
*/
public interface UserService {
public User login(String uname, String pwd);
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans [
<!ELEMENT beans (bean*)>
<!ELEMENT bean (property*)>
<!ELEMENT property (#PCDATA)>
<!ATTLIST bean id ID #REQUIRED>
<!ATTLIST bean class CDATA #IMPLIED>
<!ATTLIST property name CDATA #IMPLIED>
<!ATTLIST property ref IDREF #IMPLIED>
]>
<beans>
<!-- DAO -->
<bean id="userDAO" class="com.atguigu.book.dao.impl.UserDAOImpl"></bean>
<!-- ServiceImpl -->
<bean id="userService" class="com.atguigu.book.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
<!-- Controller -->
<bean id="page" class="com.atguigu.myssm.myspringmvc.PageController"></bean>
<bean id="user" class="com.atguigu.book.controller.UserController">
<property name="userService" ref="userService"></property>
</bean>
</beans>
<!--
1.概念
HTML 超文本标记语言
XML 可扩展的标记语言
HTML 是XML的一个自集
2.XML包含三个部分
(1)XML声明,而且声明这一行代码必须在XML文件的第一行
(2)DTD文档类型定义
(3)XML正文
-->
书城1.1
实现功能
1. 用户登录
2. 首页图书列表展示
3. 首页价格筛选
4. 添加购物车
5.购物车中加减物品数量
6.购物车结算创建订单
7.添加过滤器,过滤未登录用户
SessionFilter
package com.z_book;
import com.atguigu.book.pojo.User;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* @ClassName: SessionFilter
* @Description:
* @Author: wty
* @Date: 2022/12/14
*/
@WebFilter(urlPatterns = {"*.do", "*.html"},
initParams = {
@WebInitParam(name = "bai", value = "/pro25/page.do?operate=page&page=user/login,/pro25/user.do?")
})
public class SessionFilter implements Filter {
/**
* 白名单
*/
private List<String> baiList;
@Override
public void init(FilterConfig config) throws ServletException {
String bai = config.getInitParameter("bai");
String[] baiArr = bai.split(",");
baiList = Arrays.asList(baiArr);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String url = request.getRequestURI();
System.out.println("url = " + url);
String queryString = request.getQueryString();
System.out.println("queryString =" + queryString);
String str = "";
if (null != queryString) {
str = url + "?" + queryString;
} else {
str = url + "?";
}
/**
* 符合白名单的页面不用过滤
*/
if (baiList.contains(str)) {
filterChain.doFilter(request, response);
} else {
HttpSession session = request.getSession();
User currUser = (User) session.getAttribute("currUser");
if (null == currUser) {
response.sendRedirect("page.do?operate=page&page=user/login");
} else {
// 放行
filterChain.doFilter(request, response);
}
}
}
@Override
public void destroy() {
}
}
总结
今日内容:
- 显示主页面(index页面)
- 新建BookDAO 、 BookDAOImpl : getBookList()
- 新建BookService 、 BookServiceImpl : getBookList()
- 新建BookController : index()
- 编辑index.html
- 首页上登录成功之后显示欢迎语和购物车数量
- 点击具体图书的添加到购物车按钮
- 购物车详情
- 结账
- 订单表添加一条记录
- 订单详情表添加7条记录
- 购物车项表中需要删除对应的7条记录
- 关于订单信息中的订单数量问题
- 编辑购物车
- 关于金额的精度问题
- 过滤器判断是否是合法用户:
-
解决方法:新建SessionFilter , 用来判断session中是否保存了currUser
-
如果没有currUser,表明当前不是一个登录合法的用户,应该跳转到登录页面让其登录
-
现在添加了过滤器之后,出现了如下错误:
localhost 将您重定向的次数过多。
尝试清除 Cookie.
ERR_TOO_MANY_REDIRECTS
-