本系列文章所讲的购物车,其功能主要使用支持web开发的Eclipse和Oracle数据库进行实现,在开发中使用三层架构的模式对所有的包、接口、类、JSP文件进行编写,想了解三层架构可以看看这些文章:
Web开发之三层架构_小阿飞_的博客-CSDN博客
Web开发之回顾总结&三层架构_小阿飞_的博客-CSDN博客
本期内容是购物车开发的第一部分的内容
目录
一,数据库代码
二,Eclipse中的操作
1,项目文件目录展示
2, 项目的编写
以下sql代码是购物车开发的第一部分相关功能所需要的代码:
create table shop_user(--用户表
id number primary key,
account varchar2(50) not null,
password varchar2(50) not null
);
select * from shop_user;
insert into shop_user values(1,'aa','123');
create table shop_goods(--商品表
id number primary key,
name varchar2(50) not null,
price number default 0.0,
info varchar2(255) default '无' not null--商品描述
);
select * from shop_goods;
insert into shop_goods values(1,'书包',29.9,'无');
insert into shop_goods values(2,'书',9.9,'无');
insert into shop_goods values(3,'包',19.9,'无');
insert into shop_goods values(4,'书包1',49.9,'无');
--记得一定要提交
commit;
购物车开发第一部分所建的包、接口、类,导入后的jar包展示:
购物车开发第一部分所建的JSP文件、web.xml文件、美化包、导入的jar包展示:
util包中DBHelper类的编写:
package web_08.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
public class DBHelper {
//加载驱动:先导入驱动包,build path再OracleDriver
static {
//OracleDriver
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//定义连接字符串
private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
public static Connection getCon() {
try {
return DriverManager.getConnection(URL,"scott","123");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//关闭资源
public static void close(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if (con!=null&&con.isClosed()) {
con.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//验证数据库连接是否存在问题
public static void main(String[] args) {
System.out.println(DBHelper.getCon());
}
}
登录界面login.jsp的编写:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Document
pojo包中用户实体类User的编写:
package web_08.zking.pojo;
/**
*用户实体类
*/
public class User {
private Integer id;
private String account;
private String password;
public User() {
// TODO Auto-generated constructor stub
}
public User(Integer id, String account, String password) {
super();
this.id = id;
this.account = account;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", account=" + account + ", password=" + password + "]";
}
}
数据库访问层:
package web_08.zking.dao;
import web_08.zking.pojo.User;
/**
*数据库访问接口
*/
public interface IUserDao {
//登录方法,默认public static
User login(User user);
}
package web_08.zking.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import web_08.zking.dao.IUserDao;
import web_08.zking.pojo.User;
import web_08.zking.util.DBHelper;
/**
*数据库访问层:去数据库查到了用户账号,将这个原材料交给业务逻辑层处理
*/
public class UserDaoImpl implements IUserDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
@Override
public User login(User user) {
try {
con=DBHelper.getCon();
//"select * from shop_user where account="+user.getAccount()sql语句尽量不要拼接,存在数据库注入风险,如在表单上填一些sql语句,如;delete 表
ps=con.prepareStatement("select * from shop_user where account=?");
ps.setString(1,user.getAccount());
rs=ps.executeQuery();
if(rs.next()){
User u=new User();
u.setId(rs.getInt(1));
u.setAccount(rs.getString(2));
u.setPassword(rs.getString(3));
return u;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con,ps,rs);
}
return null;
}
}
业务逻辑层:
package web_08.zking.biz;
import web_08.zking.pojo.User;
/**
* 业务逻辑层接口
*/
public interface IUserBiz {
//登录方法,默认public static
User login(User user);
}
package web_08.zking.biz.impl;
import web_08.zking.biz.IUserBiz;
import web_08.zking.dao.IUserDao;
import web_08.zking.dao.impl.UserDaoImpl;
import web_08.zking.pojo.User;
/**
* 业务逻辑层:拿到并处理数据库访问层的数据
*/
public class UserBizImpl implements IUserBiz{
//new出dao层:用接口声明,子类实现
private IUserDao userDao=new UserDaoImpl();
@Override
//业务逻辑层处理登录功能
public User login(User user) {//user:客户端传入
//调用dao,拿到数据库中的用户
User u=userDao.login(user);
if(u!=null) {//非空判断
//加工数据:将用户输入的密码和数据库的用户密码进行比较
if(u.getPassword().equals(user.getPassword())) {
return u;//如果u为null那么数据库就没有数据,返回其他可以修改返回类型为String
}
}
return null;
}
}
由于服务器启动默认跳转到index.jsp首页界面,可以修改web.xml文件中的默认跳转路径,修改后要重启服务器,详细操作可以看这篇文章:https://blog.csdn.net/yifei_345678/article/details/124146108?spm=1001.2014.3001.5501
修改后的web.xml文件中的代码:
web_08
/login.jsp
表示层:
<%@page import="web_08.zking.vo.CarItem"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="web_08.zking.pojo.User"%>
<%@page import="web_08.zking.biz.impl.UserBizImpl"%>
<%@page import="web_08.zking.biz.IUserBiz"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//jsp表示层:
//拿请求中的数据
request.setCharacterEncoding("utf-8");
String account = request.getParameter("account");
String password = request.getParameter("password");
//jsp表示层调用业务逻辑层
IUserBiz userBiz = new UserBizImpl();
User user = userBiz.login(new User(0, account, password));
if (user == null) {
//重定向到登录界面
response.sendRedirect("/login.jsp");
} else {
//登录成功后,首页需要登录数据
session.setAttribute("user", user);
//登录一成功就分配购物车 <泛型>
List car = new ArrayList<>();
//放到session中(把购物车给我)
session.setAttribute("car", car);
//登录成功后,跳转到首页
response.sendRedirect("index.jsp");
}
%>
登录成功后跳转到项目主界面index.jsp主界面的编写:
<%@page import="web_08.zking.pojo.Goods"%>
<%@page import="web_08.zking.biz.impl.GoodsBizImpl"%>
<%@page import="web_08.zking.biz.IGoodsBiz"%>
<%@page import="web_08.zking.pojo.User"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Document
<%
//拿登录数据,数据放在了doLogin,session中
//存储在session中的是obj类型
Object obj = session.getAttribute("user");
if(obj==null){
//为空跳回登录界面
response.sendRedirect("login.jsp");
//jsp执行时要将页面上所有的java语句编译完成才执行,为空后的操作不终止代码(User)obj).getAccount()中的User就会报空指针
return;
}
%>
欢迎光临SuperMarket
尊贵的<%=((User)obj).getAccount()%>
<%=session.getAttribute("car") %>
商品序号
商品名称
商品单价
商品描述
操作
<%
//调用业务逻辑层
IGoodsBiz goodsBiz=new GoodsBizImpl();
for (Goods goods : goodsBiz.getAll()) {
%>
<%=goods.getId()%>
<%=goods.getName()%>
<%=goods.getPrice()%>
<%=goods.getInfo()%>
<%}%>
商品对象pojo包中Goods实体类的编写:
package web_08.zking.pojo;
/**
*1,商品实体类
*/
public class Goods {
private Integer id;
private String name;
//!Double需要精准运算可以使用BigDecimal类/转成int类型计算0.01*100=1
private Double price;
private String info;
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(Integer id, String name, Double price, String info) {
super();
this.id = id;
this.name = name;
this.price = price;
this.info = info;
}
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 Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + "]";
}
}
数据库访问层:
package web_08.zking.dao;
/**
*数据库连接层接口
*/
import java.util.List;
import web_08.zking.pojo.Goods;
public interface IGoodsDao {
//查询所有商品
List getAll();
//查询单个商品
Goods getOne(Integer id);
}
package web_08.zking.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import web_08.zking.dao.IGoodsDao;
import web_08.zking.pojo.Goods;
import web_08.zking.pojo.User;
import web_08.zking.util.DBHelper;
/**
*数据库连接层:将List给对应的业务逻辑层去加工
*/
public class GoodsDaoImpl implements IGoodsDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
@Override
public List getAll() {
List list=new ArrayList();
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select * from shop_goods");
rs=ps.executeQuery();
while(rs.next()){
Goods goods=new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getDouble(3));
goods.setInfo(rs.getString(4));
list.add(goods);
}
return list;
}catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con,ps,rs);
}
return null;
}
@Override
public Goods getOne(Integer id) {
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select * from shop_goods where id=?");
ps.setInt(1, id);
rs=ps.executeQuery();
if(rs.next()){
Goods goods=new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getDouble(3));
goods.setInfo(rs.getString(4));
return goods;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con,ps,rs);
}
return null;
}
}
业务逻辑层:
package web_08.zking.biz;
import java.util.List;
import web_08.zking.pojo.Goods;
/**
* 业务逻辑层接口
*/
public interface IGoodsBiz {
//查询所有商品
List getAll();
//查询单个商品
Goods getOne(Integer id);
}
package web_08.zking.biz.impl;
import java.util.List;
import web_08.zking.biz.IGoodsBiz;
import web_08.zking.dao.IGoodsDao;
import web_08.zking.dao.impl.GoodsDaoImpl;
import web_08.zking.pojo.Goods;
import web_08.zking.pojo.User;
/**
* 业务逻辑层
*/
public class GoodsBizImpl implements IGoodsBiz{
//调用dao
private IGoodsDao goodsDao=new GoodsDaoImpl();
@Override
public List getAll() {
return goodsDao.getAll();
}
@Override
public Goods getOne(Integer id) {
return goodsDao.getOne(id);
}
}
VO包(view视图,object模型/对象,前端用的包)
vo包中新建的CarItem类的意思是购物车选项(可以看成是显示在购物车的一行商品,但是不止有商品类Goods的属性),用于满足购物车的需求:需要商品的数量列和总价列
vo包中CarItem类的编写:
package web_08.zking.vo;
import web_08.zking.pojo.Goods;
/**
* 购物车选项,有Goods商品的属性,也有购物车特有的属性
*/
public class CarItem {
private Integer count;//数量
private Double sum;//条目总价
private Goods goods;//商品类,也可以继承Goods类
public CarItem() {
// TODO Auto-generated constructor stub
}
public CarItem(Integer count, Double sum, Goods goods) {
super();
this.count = count;
this.sum = sum;
this.goods = goods;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Double getSum() {
return sum;
}
public void setSum(Double sum) {
this.sum = sum;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
@Override
public String toString() {
return "CarItem [count=" + count + ", sum=" + sum + ", goods=" + goods + "]";
}
}
以上就是本期文章的第一部分的内容了,剩余内容请期待下文
感 谢 阅 读^_^