2006-京淘Day19

it2025-02-12  7

1.用户模块实现

1.1 用户信息回显

1.1.1 页面URL分析

1.1.2 检查页面JS

1.1.3 编辑JT-SSO的Controller

/** * 业务实现: * 1.用户通过cookie信息查询用户数据. 通过ticket获取redis中的业务数据. * 2.url请求: http://sso.jt.com/user/query/+ _ticket * 3.参数: 参数在url中. 利用restFul获取 * 4.返回值要求: SysResult对象(userJSON) */ @RequestMapping("/query/{ticket}") public JSONPObject findUserByTicket(@PathVariable String ticket, HttpServletResponse response, String callback){ String userJSON = jedisCluster.get(ticket); //1.lru算法清空数据 2.有可能cookie信息有误 if(StringUtils.isEmpty(userJSON)){ //2.应该删除cookie信息. Cookie cookie = new Cookie("JT_TICKET", ""); cookie.setMaxAge(0); cookie.setDomain("jt.com"); cookie.setPath("/"); response.addCookie(cookie); return new JSONPObject(callback,SysResult.fail()); } return new JSONPObject(callback,SysResult.success(userJSON)); }

1.2 编辑Cookie工具API

package com.jt.util; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CookieUtil { //1.新增cookie public static void addCookie(HttpServletResponse response,String cookieName, String cookieValue, int seconds, String domain){ Cookie cookie = new Cookie(cookieName,cookieValue); cookie.setMaxAge(seconds); cookie.setDomain(domain); cookie.setPath("/"); response.addCookie(cookie); } //2.根据name查询value的值 public static String getCookieValue(HttpServletRequest request,String cookieName){ Cookie[] cookies = request.getCookies(); if(cookies !=null && cookies.length >0){ for (Cookie cookie : cookies){ if(cookieName.equals(cookie.getName())){ return cookie.getValue(); } } } return null; } //3.删除cookie public static void deleteCookie(HttpServletResponse response,String cookieName,String domain){ addCookie(response,cookieName,"",0, domain); } }

1.3 用户退出操作

1.3.1 业务说明

如果用户点击退出操作, 首先应该删除Redis中的数据 其次删除Cookie中的数据 之后重定向到系统首页.

1.3.2 URL分析

1.3.3 编辑UserController

/** * 实现用户的退出操作.重定向到系统首页 * url: http://www.jt.com/user/logout.html * 业务: * 1.删除Redis中的数据 key * 2.删除Cookie记录 */ @RequestMapping("logout") public String logout(HttpServletRequest request,HttpServletResponse response){ //1.根据JT_TICKET获取指定的ticket String ticket = CookieUtil.getCookieValue(request,"JT_TICKET"); //2.判断ticket是否为null if(!StringUtils.isEmpty(ticket)){ jedisCluster.del(ticket); CookieUtil.deleteCookie(response,"JT_TICKET","jt.com"); } return "redirect:/"; }

2 实现商品详情展现

2.1 业务说明

说明: 当用户点击商品时,需要跳转到商品的展现页面中 页面名称item.jsp

2.2 重构JT-MANAGE

2.2.1创建接口

说明:在jt-common中创建接口

2.2.2 编辑Dubbo实现类

package com.jt.service; import com.alibaba.dubbo.config.annotation.Service; import com.jt.mapper.ItemDescMapper; import com.jt.mapper.ItemMapper; import com.jt.pojo.Item; import com.jt.pojo.ItemDesc; import org.springframework.beans.factory.annotation.Autowired; @Service(timeout = 3000) public class DubboItemServiceImpl implements DubboItemService{ @Autowired private ItemMapper itemMapper; @Autowired private ItemDescMapper itemDescMapper; @Override public Item findItemById(Long itemId) { return itemMapper.selectById(itemId); } @Override public ItemDesc findItemDescById(Long itemId) { return itemDescMapper.selectById(itemId); } }

2.2.3 编辑YML配置文件

server: port: 8091 servlet: context-path: / spring: datasource: #引入druid数据源 #type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true username: root password: root mvc: view: prefix: /WEB-INF/views/ suffix: .jsp #mybatis-plush配置 mybatis-plus: type-aliases-package: com.jt.pojo mapper-locations: classpath:/mybatis/mappers/*.xml configuration: map-underscore-to-camel-case: true logging: level: com.jt.mapper: debug #关于Dubbo配置 dubbo: scan: basePackages: com.jt #指定dubbo的包路径 扫描dubbo注解 application: #应用名称 name: provider-manage #一个接口对应一个服务名称 一个接口可以有多个实现 registry: #注册中心 用户获取数据从机中获取 主机只负责监控整个集群 实现数据同步 address: zookeeper://192.168.126.129:2181?backup=192.168.126.129:2182,192.168.126.129:2183 protocol: #指定协议 name: dubbo #使用dubbo协议(tcp-ip) web-controller直接调用sso-Service port: 20881 #每一个服务都有自己特定的端口 不能重复.

2.2.4 编辑ItemController

package com.jt.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.jt.pojo.Item; import com.jt.pojo.ItemDesc; import com.jt.service.DubboItemService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/items") public class ItemController { @Reference(timeout = 3000) private DubboItemService itemService; /** * 实现商品详情页面跳转 * url: http://www.jt.com/items/562379.html * 参数: 562379 itemId * 返回值: item.jsp页面 * 页面取值说明: * ${item.title } item对象 * ${itemDesc.itemDesc } itemDesc对象 * * 思路: * 1.重构jt-manage项目 * 2.创建中立接口DubboItemService * 3.实现业务调用获取item/itemDesc对象 */ @RequestMapping("/{itemId}") public String findItemById(@PathVariable Long itemId, Model model){ Item item = itemService.findItemById(itemId); ItemDesc itemDesc = itemService.findItemDescById(itemId); //将数据保存到request域中 model.addAttribute("item",item); model.addAttribute("itemDesc",itemDesc); return "item"; } }

2.2.5 页面效果展现

3 购物车模块实现

3.1 创建服务提供者

3.1.1 创建项目JT-CART

3.1.2 添加继承/依赖/插件

<!--2.添加依赖信息--> <dependencies> <!--依赖实质依赖的是jar包文件--> <dependency> <groupId>com.jt</groupId> <artifactId>jt-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <!--3.添加插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

3.1.3 编辑POJO对象

@TableName("tb_cart") @Data @Accessors(chain = true) public class Cart extends BasePojo{ //使用包装类型 @TableId(type = IdType.AUTO) private Long id; private Long uesrId; //用户ID号 private Long itemId; //商品ID号 private String itemTitle; //商品标题 private String itemImage; //图片 private Long itemPrice; //商品价格 private Integer num; //商品数量 }

3.1.4 编辑CartService接口

3.1.5 jt-cart代码结构

3.2 购物车列表页面展现

3.2.1 页面url分析

说明:当用户点击购物车按钮时,需要跳转到购物车展现页面. cart.jsp 页面取值: ${cartList} 要求: 只查询userId=7的购物车列表信息.之后进行页面展现

3.2.2 编辑CartController

package com.jt.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.jt.pojo.Cart; import com.jt.service.DubboCartService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/cart") public class CartController { @Reference(timeout = 3000,check = false) private DubboCartService cartService; /** * 业务需求: 根据userId查询购物车数据 * url地址: http://www.jt.com/cart/show.html * 请求参数: 动态获取userId * 返回值结果: cart.jsp页面 * 页面取值方式: ${cartList} */ @RequestMapping("/show") public String findCartListByUserId(Model model){ Long userId = 7L; List<Cart> cartList = cartService.findCartListByUserId(userId); model.addAttribute("cartList",cartList); return "cart"; } }

3.2.3 编辑CartService

package com.jt.service; import com.alibaba.dubbo.config.annotation.Service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.jt.mapper.CartMapper; import com.jt.pojo.Cart; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; @Service public class DubboCartServiceImpl implements DubboCartService{ @Autowired private CartMapper cartMapper; @Override public List<Cart> findCartListByUserId(Long userId) { QueryWrapper<Cart> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", userId); return cartMapper.selectList(queryWrapper); } }

3.2.4 页面效果展现

3.3 购物车数量的修改

3.3.1 业务说明

3.3.2 页面JS分析

3.3.3 编辑CartController

/** * 业务: 实现购物车数量的更新 * url: http://www.jt.com/cart/update/num/562379/12 * 参数: itemId/num * 返回值: void */ @RequestMapping("/update/num/{itemId}/{num}") @ResponseBody //1.返回值转化为json 2.ajax结束标识 public void updateCartNum(Cart cart){ //key名称必须与属性的名称一致. Long userId = 7L; cart.setUserId(userId); cartService.updateCartNum(cart); }

3.3.4 编辑CartService

@Override public void updateCartNum(Cart cart) { //cartMapper.updateCartNum(cart); //1.准备修改的数据 根据对象中不为null的元素当做set条件 Cart cartTemp = new Cart(); cartTemp.setNum(cart.getNum()); //2.根据对象中不为null的元素当做where条件 UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>(cart.setNum(null)); /*updateWrapper.eq("user_id", cart.getUserId()) .eq("item_id", cart.getItemId());*/ cartMapper.update(cartTemp,updateWrapper); }

3.3.5 编辑CartMapper

public interface CartMapper extends BaseMapper<Cart> { @Update("update tb_cart set num = #{num},updated=now() where user_id=#{userId} and item_id=#{itemId}") void updateCartNum(Cart cart); }

3.4 购物车新增

3.4.1 购物车新增业务

当用户点击购物车按钮时实现购物车入库操作. 如果用户重复加购则应该修改购物车商品的数量. 加购成功之后,应该重定向到购物车列表页面.

3.4.2 页面分析

3.4.3 页面表单提交

<form id="cartForm" method="post"> <input class="text" id="buy-num" name="num" value="1" onkeyup="setAmount.modify('#buy-num');"/> <input type="hidden" class="text" name="itemTitle" value="${item.title }"/> <input type="hidden" class="text" name="itemImage" value="${item.images[0]}"/> <input type="hidden" class="text" name="itemPrice" value="${item.price}"/> </form>

3.4.4 页面JS解析

<a class="btn-append " id="InitCartUrl" onclick="addCart();" clstag="shangpin|keycount|product|initcarturl">加入购物车<b></b></a> //利用post传值 function addCart(){ var url = "http://www.jt.com/cart/add/${item.id}.html"; document.forms[0].action = url; //js设置提交链接 document.forms[0].submit(); //js表单提交 }

3.4.5 编辑CartController

/** * 完成购物车新增 * url: http://www.jt.com/cart/add/562379.html * 参数: form表单提交 对象接收 * 返回值: 重定向到购物车列表页面中 */ @RequestMapping("/add/{itemId}") public String saveCart(Cart cart){ Long userId = 7L; cart.setUserId(userId); cartService.saveCart(cart); return "redirect:/cart/show.html"; }

3.4.6 编辑CartService

/** * 如果重复加购则更新数量 * 1.查询是否已经有改数据 user_id/item_id */ @Override public void saveCart(Cart cart) { QueryWrapper<Cart> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", cart.getUserId()) .eq("item_id", cart.getItemId()); Cart cartDB = cartMapper.selectOne(queryWrapper); if(cartDB == null){ //用户第一次加购 cartMapper.insert(cart); }else { //用户需要修改数量 int num = cart.getNum() + cartDB.getNum(); cart.setNum(num); cartMapper.updateCartNum(cart); } }

3.4.6 页面效果

最新回复(0)