文章目录

  • 概述
  • HeadLine Dao层
    • 接口
    • 映射文件
    • 单元测试
  • HeadLine Service层
    • 接口
    • 实现类
    • 单元测试
  • ShopCategory Dao层完善
    • 映射文件完善
    • 单元测试
  • Controller层
    • MainPageController
    • 测试
  • Github地址

概述

接下来我们来完成前端展示模块部分的功能,极其丑陋的页面原型如下

可以分析得出,主页中轮播图需要从后台加载数据,同样的一级类别(即parent_id = null )的商铺信息也需要从后台加载数据


HeadLine Dao层

接口

package com.artisan.o2o.dao;import java.util.List;import org.apache.ibatis.annotations.Param;import com.artisan.o2o.entity.HeadLine;public interface HeadLineDao {/*** * * @Title: selectHeadLineList* * @Description: 根据enable_status查询符合条件的头条信息* * @param headLineConditon* @return* * @return: List<HeadLine>*/List<HeadLine> selectHeadLineList(@Param("headLineConditon") HeadLine headLineConditon);
}

映射文件

<?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.artisan.o2o.dao.HeadLineDao"><select id="selectHeadLineList" resultType="HeadLine">SELECTline_id,line_name,line_link,line_img,priority,enable_status,create_time,last_edit_timeFROMtb_head_line<where><if test="headLineConditon.enableStatus != null">and enable_status = #{headLineConditon.enableStatus}</if></where>ORDER  BY priority DESC </select>
</mapper>   

单元测试

模拟数据:

package com.artisan.o2o.dao;import java.util.List;import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.HeadLine;public class HeadLineDaoTest extends BaseTest {@Autowiredprivate HeadLineDao headLineDao;@Testpublic void testSelectHeadLineList() {HeadLine headLineConditon = new HeadLine();// 状态 0 不可用 1 可用headLineConditon.setEnableStatus(0);// 查询不可用的头条信息List<HeadLine> headLineList = headLineDao.selectHeadLineList(headLineConditon);Assert.assertEquals(2, headLineList.size());for (HeadLine headLine : headLineList) {System.out.println(headLine);}// 查询可用的头条信息headLineConditon.setEnableStatus(1);headLineList = headLineDao.selectHeadLineList(headLineConditon);Assert.assertEquals(3, headLineList.size());for (HeadLine headLine : headLineList) {System.out.println(headLine);}// 查询全部状态的头条信息headLineList = headLineDao.selectHeadLineList(new HeadLine());Assert.assertEquals(5, headLineList.size());for (HeadLine headLine : headLineList) {System.out.println(headLine);}}
}

单元测试日志:

JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7e6ef134] will not be managed by Spring
==>  Preparing: SELECT line_id, line_name, line_link, line_img, priority, enable_status, create_time, last_edit_time FROM tb_head_line WHERE enable_status = ? ORDER BY priority DESC
==> Parameters: 0(Integer)
<==    Columns: line_id, line_name, line_link, line_img, priority, enable_status, create_time, last_edit_time
<==        Row: 4, test4, aa, bb, 96, 0, null, null
<==        Row: 5, test5, cc, dd, 95, 0, null, null
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4716be8b]
HeadLine [lineId=4, lineName=test4, lineLink=aa, lineImg=bb, priority=96, enableStatus=0, createTime=null, lastEditTime=null]
HeadLine [lineId=5, lineName=test5, lineLink=cc, lineImg=dd, priority=95, enableStatus=0, createTime=null, lastEditTime=null]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@21a21c64] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@42bc14c1] will not be managed by Spring
==>  Preparing: SELECT line_id, line_name, line_link, line_img, priority, enable_status, create_time, last_edit_time FROM tb_head_line WHERE enable_status = ? ORDER BY priority DESC
==> Parameters: 1(Integer)
<==    Columns: line_id, line_name, line_link, line_img, priority, enable_status, create_time, last_edit_time
<==        Row: 1, test1, xxx, yyy, 99, 1, null, null
<==        Row: 2, test2, x, y, 98, 1, null, null
<==        Row: 3, test3, xx, yy, 97, 1, null, null
<==      Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@21a21c64]
HeadLine [lineId=1, lineName=test1, lineLink=xxx, lineImg=yyy, priority=99, enableStatus=1, createTime=null, lastEditTime=null]
HeadLine [lineId=2, lineName=test2, lineLink=x, lineImg=y, priority=98, enableStatus=1, createTime=null, lastEditTime=null]
HeadLine [lineId=3, lineName=test3, lineLink=xx, lineImg=yy, priority=97, enableStatus=1, createTime=null, lastEditTime=null]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74f5ce22] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@16fdec90] will not be managed by Spring
==>  Preparing: SELECT line_id, line_name, line_link, line_img, priority, enable_status, create_time, last_edit_time FROM tb_head_line ORDER BY priority DESC
==> Parameters:
<==    Columns: line_id, line_name, line_link, line_img, priority, enable_status, create_time, last_edit_time
<==        Row: 1, test1, xxx, yyy, 99, 1, null, null
<==        Row: 2, test2, x, y, 98, 1, null, null
<==        Row: 3, test3, xx, yy, 97, 1, null, null
<==        Row: 4, test4, aa, bb, 96, 0, null, null
<==        Row: 5, test5, cc, dd, 95, 0, null, null
<==      Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74f5ce22]
HeadLine [lineId=1, lineName=test1, lineLink=xxx, lineImg=yyy, priority=99, enableStatus=1, createTime=null, lastEditTime=null]
HeadLine [lineId=2, lineName=test2, lineLink=x, lineImg=y, priority=98, enableStatus=1, createTime=null, lastEditTime=null]
HeadLine [lineId=3, lineName=test3, lineLink=xx, lineImg=yy, priority=97, enableStatus=1, createTime=null, lastEditTime=null]
HeadLine [lineId=4, lineName=test4, lineLink=aa, lineImg=bb, priority=96, enableStatus=0, createTime=null, lastEditTime=null]
HeadLine [lineId=5, lineName=test5, lineLink=cc, lineImg=dd, priority=95, enableStatus=0, createTime=null, lastEditTime=null]

HeadLine Service层

接口

package com.artisan.o2o.service;import java.util.List;import com.artisan.o2o.entity.HeadLine;public interface HeadLineService {/*** * * @Title: queryHeadLineList* * @Description: 查询headLine* * @param headLineConditon* * @return: List<HeadLine>*/List<HeadLine> queryHeadLineList(HeadLine headLineConditon);
}

实现类

package com.artisan.o2o.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.artisan.o2o.dao.HeadLineDao;
import com.artisan.o2o.entity.HeadLine;
import com.artisan.o2o.service.HeadLineService;@Service
public class HeadLineServiceImpl implements HeadLineService {@AutowiredHeadLineDao headLineDao;@Overridepublic List<HeadLine> queryHeadLineList(HeadLine headLineConditon) {return headLineDao.selectHeadLineList(headLineConditon);}}

单元测试

package com.artisan.o2o.service;import java.util.List;import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.HeadLine;public class HeadLineServiceTest extends BaseTest {@Autowiredprivate HeadLineService headLineService;@Testpublic void testQueryHeadLineList() {HeadLine headLineConditon = new HeadLine();// 状态 0 不可用 1 可用headLineConditon.setEnableStatus(0);// 查询不可用的头条信息List<HeadLine> headLineList = headLineService.queryHeadLineList(headLineConditon);Assert.assertEquals(2, headLineList.size());for (HeadLine headLine : headLineList) {System.out.println(headLine);}// 查询可用的头条信息headLineConditon.setEnableStatus(1);headLineList = headLineService.queryHeadLineList(headLineConditon);Assert.assertEquals(3, headLineList.size());for (HeadLine headLine : headLineList) {System.out.println(headLine);}}
}

检查是否符合预期,单元测试正常


ShopCategory Dao层完善

因为按照设计,首页展示的商品类别是一级商品类别,即parent_id为null的商铺类别信息。 因此需要扩招之前写好的Dao层的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.artisan.o2o.dao.ShopCategoryDao"><select id="queryShopCategoryList" resultType="com.artisan.o2o.entity.ShopCategory">SELECTshop_category_id ,shop_category_name,shop_category_desc,shop_category_img,priority,create_time,last_edit_time,parent_idFROMtb_shop_category<where><!-- 首页查询一级类别的商铺信息 --><if test="shopCategoryCondition == null">and parent_id is null</if><!-- 控制层getshopinitinfo的方法 shopCategoryService.getShopCategoryList(new ShopCategory());只能选择二级商铺类别,不能挂载一级商铺类别大类目录下--><if test="shopCategoryCondition != null">and parent_id is not null</if><!-- 如果传递了父类的id,则查询对应父类下的目录 --><if test="shopCategoryCondition != null and shopCategoryCondition.parent != null">and parent_id = #{shopCategoryCondition.parent.shopCategoryId}</if></where>   ORDER BY priority DESC  </select>
</mapper>

单元测试

package com.artisan.o2o.dao;import java.util.List;import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest;
import com.artisan.o2o.entity.ShopCategory;public class ShopCategoryDaoTest extends BaseTest {@AutowiredShopCategoryDao shopCategoryDao;@Testpublic void testQueryShopCategoryList() {// shopCategoryCondition 不为null的情况,查询parent_id is not null 的数据ShopCategory shopCategory = new ShopCategory();List<ShopCategory> categoryList = shopCategoryDao.queryShopCategoryList(shopCategory);Assert.assertEquals(2, categoryList.size());for (ShopCategory shopCategory2 : categoryList) {System.out.println(shopCategory2);}// shopCategoryCondition.parent 不为null的情况// 查询parent=1的店铺目录ShopCategory child = new ShopCategory();ShopCategory parent = new ShopCategory();parent.setShopCategoryId(1L);child.setParent(parent);categoryList = shopCategoryDao.queryShopCategoryList(child);Assert.assertEquals(2, categoryList.size());for (ShopCategory shopCategory2 : categoryList) {System.out.println(shopCategory2);}// 查询 parent is null 的情况categoryList = shopCategoryDao.queryShopCategoryList(null);Assert.assertEquals(1, categoryList.size());System.out.println(categoryList.get(0));}}

检查是否符合预期,单元测试正常


Controller层

MainPageController

package com.artisan.o2o.web.frontend;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;import com.artisan.o2o.entity.HeadLine;
import com.artisan.o2o.entity.ShopCategory;
import com.artisan.o2o.enums.HeadLineStateEnum;
import com.artisan.o2o.enums.ShopCategoryStateEnum;
import com.artisan.o2o.service.HeadLineService;
import com.artisan.o2o.service.ShopCategoryService;@Controller
@RequestMapping("/frontend")
public class MainPageController {@Autowiredprivate HeadLineService headLineService;@Autowiredprivate ShopCategoryService shopCategoryService;@RequestMapping(value = "/listmainpage", method = RequestMethod.GET)@ResponseBodypublic Map<String, Object> listMainPage() {Map<String, Object> modelMap = new HashMap<String, Object>();List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();List<HeadLine> headLineList = new ArrayList<HeadLine>();try {// 查询状态为1的可见的headLine信息HeadLine headLineConditon = new HeadLine();headLineConditon.setEnableStatus(1);headLineList = headLineService.queryHeadLineList(headLineConditon);modelMap.put("headLineList", headLineList);} catch (Exception e) {e.printStackTrace();modelMap.put("errMsg", HeadLineStateEnum.INNER_ERROR.getStateInfo());}try{// 查询parentId为null的一级类别shopCategoryList = shopCategoryService.getShopCategoryList(null);modelMap.put("shopCategoryList", shopCategoryList);} catch (Exception e) {e.printStackTrace();modelMap.put("success", false);modelMap.put("errMsg", ShopCategoryStateEnum.INNER_ERRO.getStateInfo());}modelMap.put("success", true);return modelMap;}
}

测试

启动tomcat,访问 http://localhost:8080/o2o/frontend/listmainpage

得到JSON字符串如下

{"shopCategoryList": [{"shopCategoryId": 1,"shopCategoryName": "咖啡奶茶","shopCategoryDesc": "咖啡奶茶大类","shopCategoryImg": "/xxxx/xxxx","priority": 0,"createTime": 1526580836000,"lastEditTime": 1526580838000,"parent": null}],"success": true,"headLineList": [{"lineId": 1,"lineName": "test1","lineLink": "xxx","lineImg": "yyy","priority": 99,"enableStatus": 1,"createTime": null,"lastEditTime": null},{"lineId": 2,"lineName": "test2","lineLink": "x","lineImg": "y","priority": 98,"enableStatus": 1,"createTime": null,"lastEditTime": null},{"lineId": 3,"lineName": "test3","lineLink": "xx","lineImg": "yy","priority": 97,"enableStatus": 1,"createTime": null,"lastEditTime": null}]
}

符合预期。后端完成,接下来我们来试下View层的逻辑。


Github地址

代码地址: https://github.com/yangshangwei/o2o

实战SSM_O2O商铺_39【前端展示】首页轮播图和一级商铺Dao+Service+Controller层的实现相关推荐

  1. 实战SSM_O2O商铺_40【前端展示】首页轮播图和一级商铺View层的实现

    文章目录 概述 index.html index.js index.css Controller 调测 修复问题 头条图片展示修复 一级类别商铺图片展示修复 Github地址 概述 在完成了后端 实战 ...

  2. 实战SSM_O2O商铺_41【前端展示】店铺列表页面Dao+Service+Controller层的实现

    文章目录 概述 Dao层 接口 映射文件 单元测试 Service层 接口方法 单元测试 Controller层 增加 ShopListController 单元测试 Github地址 概述 在完成了 ...

  3. 微服务项目实战技术点汇总:“尚硅谷的谷粒在线教育”七、redis数据库缓存页面数据、使用NUXT框架搭建前台系统环境、前台系统页面、首页轮播图(banner数据显示)、首页热门课程,名师推荐

    文章目录 一.NUXT前台环境搭建 1.如何学习NUXT 2.下载安装使用NUXT入门模板starter-template 3.机制 二.编写静态页面 1.设置布局(首尾固定,中间用nuxt引用组件) ...

  4. (转)淘淘商城系列——首页轮播图展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72848306 上文我们一起学习了内容管理,由于时间太紧了,好多功能都没实现,在此对读者说声抱歉 ...

  5. 六十六、实现Vue项目首页轮播图(vue-awesome-swiper)

    2020/10/23. 周五.今天又是奋斗的一天. @Author:Runsen @Date:2020/10/22 写在前面:我是「Runsen」,热爱技术.热爱开源.热爱编程.技术是开源的.知识是共 ...

  6. 【VIP视频网站项目二】搭建爱奇艺优酷腾讯视频官网首页轮播图效果及实现原理分析

    这个是实现的效果,基本上轮播效果和主流网站的一致,但是我也在上面优化了一些效果, 可以在线预览效果:https://vip.52tech.tech/ 目前项目代码已经全部开源:项目地址:https:/ ...

  7. 基于Redis优化首页轮播图查询

    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET&q ...

  8. UI-网站首页轮播图、易拉宝、发布在微信公众号的宣传海报的图片设计信息

    UI交互设计 一."新建" 常见的颜色模式: 颜色模式,是将某种颜色表现为数字形式的模型,或者说是一种记录图像颜色的方式.分为:RGB模式.CMYK模式.HSB模式.Lab颜色模式 ...

  9. 后台实现电商首页轮播图功能

    这后端实现轮播图要做的功能:将能够展示的轮播图 从数据库中查询出来,返回给前端,就就这么一点功能,但是主要是 数据库表的设计. 1.:轮播图的表结构: 1.1 :关于背景色:也可以不用,主要看你的轮播 ...

最新文章

  1. 比较全的字符串验证类,有人顶的话以后继续发
  2. 【OpenCV 4开发详解】轮廓面积与长度
  3. uboot通过使用U磁盘引导内核RT5350成功
  4. 提高代码质量 CheckStyle FindBugs PMD
  5. 嵌入式常见笔试题总结(3)
  6. BlocksKit源码分析(一)
  7. [转]IIS状态代码的含义
  8. No.4 Linux用户、组的创建、修改和权限的修改
  9. LayaAir WebSocket 通信
  10. socket 实现淘宝秒杀器(抢拍器) 附源码与截图
  11. select模型+epoll模型+reactor模型
  12. gbk、gb2312、big5、unicode、utf-8
  13. 机器学习10大经典算法
  14. 程序员发布:一张图读懂支撑阿里双十一1682亿的技术架构体系
  15. 【Git】规范化 Git 提交信息 Commitizen
  16. linux 删除文件的最后一行
  17. python画平行四边形_利用transform skewX制作平行四边形导航菜单
  18. 制作我自己的桌面小机器人Zbot(遇到的问题总结)
  19. 测试iphone硬件好坏的软件,如何检测Apple手机硬件是否损坏
  20. hive 开窗函数OVER(PARTITION)详解(一)

热门文章

  1. php的wsgi框架结构,理解 WSGI 框架
  2. 推荐系统笔记(开源工具)
  3. 怎么寻找科研论文?(二)
  4. pythonexcelweb交互插件_来一次Python与Excel的完美交互
  5. 深度学习100例 | 第26天-卷积神经网络(CNN):乳腺癌识别
  6. 如何在tensorflow2环境运行tensorflow1代码
  7. MATLAB对字符串进行分割
  8. LeetCode-数组-35. 搜索插入位置
  9. Python编程基础:第三十五节 文件删除Delete a File
  10. python数据类型转换方法列表