目录
2.数据库访问层的实现类:GoodsDaoImpl.java
这次分享的全部都是基于之前的博客中有讲述到的购物车系统,这里只 是基于购物车系统做了稍稍的更改。还有分页的制作。
1.商品的数据库访问层:IGoodsDao.java
package com.xyz.dao;
import java.util.List;
import com.xyz.pojo.Goods;
public interface IGoodsDao {
//查询所有
List<Goods> getAll(int page);
//根据id查询
Goods getOne(Integer id);
//查询所有数量
int getRowCount();
}
2.数据库访问层的实现类:GoodsDaoImpl.java
package com.xyz.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xyz.dao.IGoodsDao;
import com.xyz.pojo.Goods;
import com.xyz.pojo.User;
import com.xyz.util.DBHelper;
public class GoodsDaoImpl implements IGoodsDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
//查询所有的数量
@Override
public int getRowCount() {
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select count(1) from shop_goods");
rs=ps.executeQuery();
if(rs.next()) {
return rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return 0;
}
//查询所有
@Override
public List<Goods> getAll(int page) {
int size=4;
int begin=(page-1)*size+1;
int end=page*size;
List<Goods> list=new ArrayList<Goods>();
try {
con=DBHelper.getCon();
String sql="select * from (select a.*,rownum myr from shop_goods a) b where myr between ? and ?";
ps=con.prepareStatement(sql);
ps.setInt(1, begin);
ps.setInt(2, end);
rs=ps.executeQuery();
while(rs.next()) {
Goods goods=new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getInt(3));
goods.setInfo(rs.getString(4));
list.add(goods);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return list;
}
//根据id查询
@Override
public Goods getOne(Integer id) {
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select * from shop_goods where g_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.getInt(3));
goods.setInfo(rs.getString(4));
return goods;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return null;
}
}
3.购物车的业务逻辑层:IGoodsBiz.java
package com.xyz.biz;
import java.util.List;
import com.xyz.pojo.Goods;
public interface IGoodsBiz {
//查询全部
List<Goods> getAll(int page);
//根据id查询
Goods getOne(Integer id);
//查询所有数量
int getRowCount();
}
4.业务逻辑层的实现:GoodsBizImpl.java
package com.xyz.biz.impl;
import java.util.List;
import com.xyz.biz.IGoodsBiz;
import com.xyz.dao.IGoodsDao;
import com.xyz.dao.impl.GoodsDaoImpl;
import com.xyz.pojo.Goods;
public class GoodsBizImpl implements IGoodsBiz{
private IGoodsDao goodsDao=new GoodsDaoImpl();
@Override
public List<Goods> getAll(int page) {
return goodsDao.getAll(page);
}
@Override
public Goods getOne(Integer id) {
return goodsDao.getOne(id);
}
@Override
public int getRowCount() {
return goodsDao.getRowCount();
}
}
5.修改后的主界面:index.jsp
<%@page import="com.xyz.pojo.Goods"%>
<%@page import="com.xyz.biz.impl.GoodsBizImpl"%>
<%@page import="com.xyz.biz.IGoodsBiz"%>
<%@page import="com.xyz.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(3)::before{
content: "$";
}
</style>
</head>
<body>
<c:if test="${user==null}">
<c:redirect url="login.jsp"></c:redirect>
</c:if>
<c:if test="${empty goods}">
<c:redirect url="doIndex.jsp"></c:redirect>
</c:if>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临串串遇SuperMarket</h1>
<p>尊贵的${user.account}</p>
<a href="car.jsp" class="btn btn-primary">🛒</a>
<a href="doExit.jsp" class="btn btn-warning">退出登录</a>
</div>
</div>
<div class="container">
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品描述</th>
<th>操作</th>
</tr>
<c:forEach items="${goods}" var="g">
<tr>
<td>${g.id}</td>
<td>${g.name}</td>
<td>${g.price}</td>
<td>${g.info}</td>
<td>
<div class="btn-group btn-group-xs">
<a href="doAddCar.jsp?id=${g.id}" class="btn btn-primary">添加购物车</a>
</div>
</td>
</tr>
</c:forEach>
</table>
</div>
<div class="container text-center">
<ul class="pagination" style="margin: 20px auto;">
<li>
<a href="doIndex.jsp?ipage=${Math.max(ipage-1,1)}"><span>«</span></a>
</li>
<c:forEach begin="1" end="${maxPage}" var="i">
<li class="${ipage==i?"active":""}">
<a href="doIndex.jsp?ipage=${i}">${i}</a>
</li>
</c:forEach>
<li>
<a href="doIndex.jsp?ipage=${ipage+1}"><span>»</span></a>
</li>
</ul>
</div>
</body>
</html>
6.修改后的购物车界面:car.jsp
修改后的购物车界面:car.jsp
<%@page import="com.xyz.pojo.User"%>
<%@page import="com.xyz.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(4)::before,
* {
outline: none !important;
}
td,
th {
text-align: center;
}
input {
text-align: center;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临串串遇购物车🛒</h1>
<p>尊贵的${user.account}</p>
<a href="doIndex.jsp" class="btn btn-primary">返回主界面</a>
</div>
</div>
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品个数</th>
<th>商品总价</th>
<th>操作</th>
</tr>
<c:forEach items="${car}" var="c">
<tr>
<td style="line-height: 30.5px;">${c.goods.id}</td>
<td style="line-height: 30.5px;">${c.goods.name}</td>
<td style="line-height: 30.5px;">${c.goods.price}</td>
<td>
<div class="input-group" style="width: 120px;margin: auto;">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=${c.goods.id}&type=0" class="btn btn-default" type="button">-</a>
</span>
<input onblur="location.href='doUpdCar.jsp?id=${c.goods.id}&count='+this.value" value="${c.count}" type="number" class="form-control">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=${c.goods.id}&type=1" class="btn btn-default" type="button">+</a>
</span>
</div>
</td>
<td style="line-height: 30.5px;">${c.sum}</td>
<td style="line-height: 30.5px;">
<a href="doDelCar.jsp?id=${c.goods.id}" class="btn btn-primary">删除</a>
</td>
</tr>
</c:forEach>
</table>
<h1 class="alert alert-info">
<a href="doClear.jsp" class="btn btn-danger">点我结算</a>
</h1>
</body>
</html>
今天的学习就到这里啦。