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

简介: 实战SSM_O2O商铺_39【前端展示】首页轮播图和一级商铺Dao+Service+Controller层的实现

概述


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


20180720082953632.png

可以分析得出,主页中轮播图需要从后台加载数据,同样的一级类别(即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">
    SELECT
      line_id,
      line_name,
      line_link,
      line_img,
      priority,
      enable_status,
      create_time,
      last_edit_time
    FROM
      tb_head_line
    <where>
      <if test="headLineConditon.enableStatus != null">
        and enable_status = #{headLineConditon.enableStatus}
      </if>
    </where>
    ORDER  BY 
      priority 
    DESC 
  </select>
</mapper>   



单元测试

模拟数据:

20180725180216325.png


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 {
  @Autowired
  private HeadLineDao headLineDao;
  @Test
  public 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);
    }
  }
}

20180725180329806.png

单元测试日志:

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 {
  @Autowired
  HeadLineDao headLineDao;
  @Override
  public 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 {
  @Autowired
  private HeadLineService headLineService;
  @Test
  public 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);
    }
  }
}


20180725180541248.png


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


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">
    SELECT
      shop_category_id ,
      shop_category_name,
      shop_category_desc,
      shop_category_img,
      priority,
      create_time,
      last_edit_time,
      parent_id
    FROM
      tb_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 {
  @Autowired
  ShopCategoryDao shopCategoryDao;
  @Test
  public 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));
  }
}


20180725180954962.png

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


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 {
  @Autowired
  private HeadLineService headLineService;
  @Autowired
  private ShopCategoryService shopCategoryService;
  @RequestMapping(value = "/listmainpage", method = RequestMethod.GET)
  @ResponseBody
  public 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

相关文章
|
3月前
|
前端开发 JavaScript 索引
前端性能优化:虚拟滚动技术原理与实战
前端性能优化:虚拟滚动技术原理与实战
467 80
|
7月前
|
安全 前端开发 开发工具
【01】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-项目开发实战-优雅草卓伊凡拟开发一个一站式家政服务平台-前期筹备-暂定取名斑马家政软件系统-本项目前端开源-服务端采用优雅草蜻蜓Z系统-搭配ruoyi框架admin后台-全过程实战项目分享-从零开发到上线
【01】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-项目开发实战-优雅草卓伊凡拟开发一个一站式家政服务平台-前期筹备-暂定取名斑马家政软件系统-本项目前端开源-服务端采用优雅草蜻蜓Z系统-搭配ruoyi框架admin后台-全过程实战项目分享-从零开发到上线
323 5
【01】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-项目开发实战-优雅草卓伊凡拟开发一个一站式家政服务平台-前期筹备-暂定取名斑马家政软件系统-本项目前端开源-服务端采用优雅草蜻蜓Z系统-搭配ruoyi框架admin后台-全过程实战项目分享-从零开发到上线
|
9月前
|
存储 前端开发 JavaScript
前端状态管理:Vuex 核心概念与实战
Vuex 是 Vue.js 应用程序的状态管理模式和库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。本教程将深入讲解 Vuex 的核心概念,如 State、Getter、Mutation 和 Action,并通过实战案例帮助开发者掌握在项目中有效使用 Vuex 的技巧。
|
10月前
|
Web App开发 缓存 监控
前端性能优化实战:从代码到部署的全面策略
前端性能优化实战:从代码到部署的全面策略
172 1
|
10月前
|
缓存 监控 前端开发
前端性能优化实战:从加载速度到用户体验
前端性能优化实战:从加载速度到用户体验
|
7月前
|
存储 Java 关系型数据库
ssm026校园美食交流系统(文档+源码)_kaic
本文介绍了基于Java语言和MySQL数据库的校园美食交流系统的设计与实现。该系统采用B/S架构和SSM框架,旨在提高校园美食信息管理的效率与便捷性。主要内容包括:系统的开发背景、目的及内容;对Java技术、MySQL数据库、B/S结构和SSM框架的介绍;系统分析部分涵盖可行性分析、性能分析和功能需求分析;最后详细描述了系统各功能模块的具体实现,如登录、管理员功能(美食分类管理、用户管理等)和前台首页功能。通过此系统,管理员可以高效管理美食信息,用户也能方便地获取和分享美食资讯,从而提升校园美食交流的管理水平和用户体验。
|
6月前
|
Java 关系型数据库 MySQL
weixin050高校体育场管理系统+ssm(文档+源码)_kaic
本文针对高校体育场管理系统的开发与实现进行详细介绍。随着经济快速发展,人们对手机软件需求增加,高校体育场管理系统应运而生。系统采用JAVA技术、Mysql数据库和SSM框架等成熟技术,通过分析功能需求、可行性及性能,设计出包含管理员、用户和学生角色的功能模块。系统实现用户注册登录、信息管理等功能,简化传统手工统计模式,提高管理效率,满足用户对信息获取的及时性与准确性需求。
weixin050高校体育场管理系统+ssm(文档+源码)_kaic
|
Java 数据库连接 Maven
手把手教你如何搭建SSM框架、图书商城系统案例
这篇文章是关于如何搭建SSM框架以及实现一个图书商城系统的详细教程,包括了项目的配置文件整合、依赖管理、项目结构和运行效果展示,并提供了GitHub源码链接。
手把手教你如何搭建SSM框架、图书商城系统案例
|
6月前
|
前端开发 Java 关系型数据库
基于ssm的社区物业管理系统,附源码+数据库+论文+任务书
社区物业管理系统采用B/S架构,基于Java语言开发,使用MySQL数据库。系统涵盖个人中心、用户管理、楼盘管理、收费管理、停车登记、报修与投诉管理等功能模块,方便管理员及用户操作。前端采用Vue、HTML、JavaScript等技术,后端使用SSM框架。系统支持远程安装调试,确保顺利运行。提供演示视频和详细文档截图,帮助用户快速上手。
216 17
|
6月前
|
前端开发 Java 关系型数据库
基于ssm的超市会员(积分)管理系统,附源码+数据库+论文,包安装调试
本项目为简单内容浏览和信息处理系统,具备管理员和员工权限。管理员可管理会员、员工、商品及积分记录,员工则负责积分、商品信息和兑换管理。技术框架采用Java编程语言,B/S架构,前端使用Vue+JSP+JavaScript+Css+LayUI,后端为SSM框架,数据库为MySQL。运行环境为Windows,JDK8+Tomcat8.5,非前后端分离的Maven项目。提供演示视频和详细文档,购买后支持免费远程安装调试。
273 19

热门文章

最新文章