版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/85685690
在本系统中,购物车的方法以及关注商品的相关方法都是在同一个类中,所以购物车和关注商品在一块实现
实现顺序是由:持久层——dao层(数据层)——service层(事务层)——controller层(控制层)——jsp层(表现层)。由于持久化类已经全部实现,所以直接从dao层开始
一、实现dao层
dao层由接口以及映射文件组成,两者同名,姑且命名为CartDao
其接口文件中包含购物车以及关注商品的方法:关注商品、是否关注、是否已添加购物车、添加购物车、更新购物车、查询购物车、删除购物车、清空购物车。
其接口详细代码如下所示:
package com.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository("cartDao")
@Mapper
public interface CartDao {
public int focus(Map<String , Object> map);
public List<Map<String, Object>> isFocus(Map<String ,Object> map);
public List<Map<String, Object>> isPutCart(Map<String, Object> map);
public int putCart (Map<String , Object> map);
public int updateCart (Map<String , Object> map);
public List<Map<String, Object>> selectCart(Integer id);
public int deleteAgoods (Map<String , Object> map);
public int clear(Integer id);
}
映射方法对接口中的每个方法都编写了对应的sql语句,其映射文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.CartDao">
<!-- 关注商品 -->
<insert id="focus" parameterType="map">
insert into focustable(id,goodstable_id,busertable_id,focustime) value (null,#{gid},#{uid},now())
</insert>
<!-- 是否已关注-->
<select id="isFocus" parameterType="map" resultType="map">
select * from focustable where goodstable_id=#{gid} and busertable_id=#{uid}
</select>
<!-- 是否已添加购物车 -->
<select id="isPutCart" parameterType="map" resultType="map">
select * from carttable where goodstable_id=#{gid} and busertable_id=#{uid}
</select>
<!-- 添加购物车 -->
<insert id="putCart" parameterType="map">
insert into carttable(id,busertable_id,goodstable_id,shoppingnum) values (null,#{gid},#{uid},#{shoppingnum})
</insert>
<!-- 更新购物车 -->
<update id="updateCart" parameterType="map">
update carttable set shoppingnum=shoppingnum+#{shoppingnum} where busertable_id=#{uid} and goodstable_id=#{gid}
</update>
<select id="selectCart" parameterType="Integer" resultType="map">
select gt.id, gt.gname, gt.gpicture, gt.grprice, ct.shoppingnum, ct.shoppingnum*gt.grprice smallsum
from GOODSTABLE gt, CARTTABLE ct where gt.id=ct.goodstable_id and ct.busertable_id=#{id}
</select>
<!-- 删除购物车 -->
<delete id="deleteAgoods" parameterType="map">
delete from carttable where busertable_id=#{uid} and goodstable_id=#{gid}
</delete>
<!-- 清空购物车 -->
<delete id="clear" parameterType="Integer">
delete from carttable where busertable_id=#{uid}
</delete>
</mapper>
二、实现service层(事务层)
此层是由service层接口和其实现类组成的,
在其接口中有如下方法:关注商品、添加购物车、查询购物车、删除购物车、清空购物车、订单确认
其接口详细代码:
package com.service.before;
import javax.servlet.http.HttpSession;
import org.springframework.ui.Model;
public interface CartService {
public String focus(Model model,Integer id, HttpSession session);
public String putCart(Model model,Integer shoppingnum, Integer id, HttpSession session);
public String selectCart(Model model, HttpSession session);
public String deleteAgoods(Integer id,HttpSession session);
public String clear(HttpSession session);
public String orderConfirm(Model model, HttpSession session);
}
其实现类如下所示:
package com.service.before;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import com.dao.CartDao;
import com.util.MyUtil;
@Service("cartService")
@Transactional
public class CartServiceImpl implements CartService {
@Autowired
private CartDao cartDao;
@Override
public String focus(Model model,Integer id, HttpSession session) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uid", MyUtil.getUserId(session));
map.put("gid", id);
List<Map<String, Object>> list = cartDao.isFocus(map);
if(list.size() > 0) {
model.addAttribute("msg", "已关注该商品!");
}else {
int n = cartDao.focus(map);
if(n > 0)
model.addAttribute("msg", "成功关注该商品!");
else
model.addAttribute("msg", "关注失败!");
}
return "forward:/goodsDetail?id=" + id;
}
@Override
public String putCart(Model model,Integer shoppingnum, Integer id, HttpSession session) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uid", MyUtil.getUserId(session));
map.put("gid", id);
map.put("shoppingnum", shoppingnum);
//是否已添加购物车
List<Map<String, Object>> list = cartDao.isPutCart(map);
if(list.size() > 0)
cartDao.updateCart(map);
else
cartDao.putCart(map);
return "forward:/cart/selectCart";
}
@Override
public String selectCart(Model model, HttpSession session) {
List<Map<String, Object>> list = cartDao.selectCart(MyUtil.getUserId(session));
double sum = 0;
for (Map<String, Object> map : list) {
sum = sum + (Double)map.get("smallsum");
}
model.addAttribute("total", sum);
model.addAttribute("cartlist", list);
return "before/cart";
}
@Override
public String deleteAgoods(Integer id, HttpSession session) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uid", MyUtil.getUserId(session));
map.put("gid", id);
cartDao.deleteAgoods(map);
return "forward:/cart/selectCart";
}
@Override
public String clear(HttpSession session) {
cartDao.clear(MyUtil.getUserId(session));
return "forward:/cart/selectCart";
}
@Override
public String orderConfirm(Model model, HttpSession session) {
List<Map<String, Object>> list = cartDao.selectCart(MyUtil.getUserId(session));
double sum = 0;
for (Map<String, Object> map : list) {
sum = sum + (Double)map.get("smallsum");
}
model.addAttribute("total", sum);
model.addAttribute("cartlist", list);
return "before/orderconfirm";
}
}
三、controller层(控制层)
此层包含一个控制类,此类中包含六个方法,详细如下:
package com.controller.before;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.service.before.CartService;
@Controller
@RequestMapping("/cart")
public class CartController extends BaseBeforeController{
@Autowired
private CartService cartService;
@RequestMapping("/focus")
public String focus(Model model,Integer id,HttpSession session) {
return cartService.focus(model, id, session);
}
@RequestMapping("/putCart")
public String putCart(Model model,Integer shoppingnum,Integer id,HttpSession session) {
return cartService.putCart(model, shoppingnum, id, session);
}
@RequestMapping("/selectCart")
public String selectCart(Model model,HttpSession session) {
return cartService.selectCart(model, session);
}
@RequestMapping("/deleteCart")
public String deleteAgoods(Integer id,HttpSession session) {
return cartService.deleteAgoods(id, session);
}
@RequestMapping("/clear")
public String clear(HttpSession session) {
return cartService.clear(session);
}
//确认订单去结算
@RequestMapping("/orderConfirm")
public String orderConfirm(Model model,HttpSession session) {
return cartService.orderConfirm(model, session);
}
}
四、jsp层(表现层)
此层包含一个jsp文件
cart.jsp文件如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:include page="head.jsp"></jsp:include>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>购物车</title>
<link href="css/before/daohang.css" rel="stylesheet" type="text/css" />
<link href="css/before/common.css" rel="stylesheet" type="text/css" />
<link href="css/before/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
//确认
function goOrderConfirm(){
window.location.href="/ch20/cart/orderConfirm";
}
//清空
function godelete(){
if(window.confirm("真的清空购物车吗?")){
window.location.href="/ch20/cart/clear";
return true;
}
return false;
}
//删除
function deleteAgoods(gno){
if(window.confirm("真的删除该商品吗?")){
window.location.href="/ch20/cart/deleteAgoods?id=" + gno;
return true;
}
return false;
}
</script>
</head>
<body>
<div class="blank"></div>
<div class="block clearfix">
<!--当前位置-->
<div class="location ared">
当前位置: <a href="before?id=0">首页</a> > 购物流程 > 购物车
</div>
<div class="blank"></div>
<div>
<div class="nFlowBox">
<table width="99%" align="center" border="0" cellpadding="5"
cellspacing="1" bgcolor="#dddddd">
<tr>
<th>商品信息</th>
<th>单价(元)</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
<tr>
<td colspan="5" height="15px"
style="border: 0 none; background: #FFF"></td>
</tr>
<!-- 这里使用了jstl标签-->
<c:forEach var="ce" items="${cartlist}">
<tr>
<td bgcolor="#ffffff" align="center"><a href="goodsDetail?id=${ce.id}"> <img
style="width: 100px; height: 100px;"
src="logos/${ce.gpicture}" border="0"
title="${ce.gname}" />
</a><br/><a style="text-decoration: none;" href="goodsDetail?id=${ce.id}" class="f6">${ce.gname}</a></td>
<td bgcolor="#ffffff" width="110px" align="center"><span>${ce.grprice}</span></td>
<td align="center" bgcolor="#ffffff" width="115px"
valign="middle"><input type="text" name="goods_number"
value="${ce.shoppingnum}" size="4"
class="inputBg"
style="text-align: center; width: 36px; color: #999999" /></td>
<td align="center" bgcolor="#ffffff" width="115px">¥ <span>${ce.smallsum}</span>
</td>
<td align="center" bgcolor="#ffffff" width="185px"><a
style="text-decoration: none;" href="javaScript:deleteAgoods('${ce.id}')"
class="f6" title="删除"><img src="images/before/sc.png" />
</a></td>
</tr>
</c:forEach>
<tr>
<td align="right" bgcolor="#ffffff" colspan="4" height="41px;"
style="border-left: 0 none;"><font
style="color: #a60401; font-size: 13px; font-weight: bold; letter-spacing: 0px;">
购物金额总计(不含运费) ¥ <span id="stotal"></span>
${total}元
</font></td>
<td align="center" bgcolor="#ffffff"><input type="button"
value="清空购物车" onclick="godelete()" class="bnt_blue_1" id="bnt11" />
</td>
</tr>
<tr>
<td bgcolor="#ffffff" colspan="4" align="right"
style="padding: 5px; padding-left: 2px; border-right: 0 none">
<a href="before"> <img src="images/before/jxgw.jpg" alt="continue" />
</a>
</td>
<td bgcolor="#ffffff" align="center"
style="border-left: 0 none; padding: 5px; padding-right: 2px;">
<a style="cursor: pointer;" href="javascript:goOrderConfirm()">
<img src="images/before/qjs.jpg" alt="checkout" />
</a>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
五、测试
重启tomcat,在浏览器网址栏输入:http://localhost:8080/testshop1/before
登录之后点击任一商品
点击关注:
最后去查看一下购物车: