概述
接下来我们来完成前端展示模块部分的功能,极其丑陋的页面原型如下
可以分析得出,主页中轮播图需要从后台加载数据,同样的一级类别(即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>
单元测试
模拟数据:
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); } } }
单元测试日志:
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); } } }
检查是否符合预期,单元测试正常
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)); } }
检查是否符合预期,单元测试正常
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层的逻辑。