大家好,我是Rtai,今天给大家带来的是购物车2.0版本
购物车2.0版本不同于购物车1.0,因为购物车2.0运用到了分层
效果相较于1.0是没什么差别的,下面是效果图:
然后可以看看分层后的项目文件:
分为三层:数据访问层、业务逻辑层、视图层
数据访问层:util中的DBHelper和实体类 还有dao接口和dao实现类都为数据访问层
业务逻辑层:biz接口和biz实现类为业务逻辑层
视图层:主页面等
关系:业务逻辑层调用数据访问层 视图层调用业务逻辑层
代码:
实体类和帮助类与购物车1.0是相同的所以这次不展示了
主要展示dao和dao实现类 biz和biz实现类
dao接口(以商品表为示例):
package com.zking.dao;
import java.util.ArrayList;
import com.zking.entity.Goods;
public interface IGoodsDao {
public ArrayList getAll();
public Goods getById(int bid);
}
dao实现类:
package com.zking.dao.imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.dao.IGoodsDao;
import com.zking.entity.Goods;
import com.zking.util.DBHelper;
public class Imp_GoodsDao implements IGoodsDao {
//查询商品所有信息
public ArrayList getAll() {
ArrayList glist = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from goods");
rs = ps.executeQuery();
while(rs.next()) {
glist.add(new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return glist;
}
//根据商品编号查询商品信息
public Goods getById(int bid) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Goods g = null;
try {
con = DBHelper.getCon();
ps = con.prepareStatement("select * from goods where bid="+bid);
rs = ps.executeQuery();
if(rs.next()) {
g = new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.closeDb(con, ps, rs);
}
return g;
}
}
biz接口:
package com.zking.biz;
import java.util.ArrayList;
import com.zking.entity.Goods;
public interface IGoodsBiz {
public ArrayList getAll();
public Goods getById(int bid);
}
biz实现类:
package com.zking.biz.imp;
import java.util.ArrayList;
import com.zking.biz.IGoodsBiz;
import com.zking.dao.IGoodsDao;
import com.zking.dao.imp.Imp_GoodsDao;
import com.zking.entity.Goods;
public class Imp_GoodsBiz implements IGoodsBiz {
//实例化数据访问层
IGoodsDao igd = new Imp_GoodsDao();
//调用方法
public ArrayList getAll() {
//调用数据访问层的查询所有方法
return igd.getAll();
}
public Goods getById(int bid) {
//调用数据访问层的查询单个方法
return igd.getById(bid);
}
}
主页面效果与1.0相差无几,最大不同就是一个用户登录后,只能查看个人购物车,无法查看他人购物车
好了,这里是Rtai,希望这次分享对大家有帮助,下次再见!
生活是晨起暮落 时光匆匆 我们终将释怀 开心的笑着 适当的忙着 就很好



