实战SSM_O2O商铺_22【商铺列表】Service层开发

2021-08-17 11:31:06 浏览数 (1)

文章目录

  • 概述
  • ShopService接口新增接口方法
  • ShopServie接口实现类
  • 分页工具类
  • 单元测试
  • Github地址

概述

ShopService接口中仅需要定义一个接口方法,在该接口方法的实现类中调用DAO层的两个方法 selectShopList 和 selectShopCount ,并将数据封装到ShopExecution中,以便控制层获取数据,在View层做展示。

ShopExecution 这里增加了一个空构造函数,方便实例化并调用set方法,将shopList和shopCount 封装到ShopExecution 中


ShopService接口新增接口方法

代码语言:javascript复制
	/**
	 * 
	 * 
	 * @Title: getShopList
	 * 
	 * @Description: 获取商铺列表. 在这一个方法中同样的会调用查询总数的DAO层方法,封装到ShopExecution中
	 * 
	 * @param shopCondition
	 * @param pageIndex
	 *            前端页面 只有第几页 第几页 定义为pageIndex
	 * @param pageSize
	 *            展示的行数
	 * @throws ShopOperationException
	 * 
	 * @return: ShopExecution
	 */
	ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) throws ShopOperationException;;

ShopServie接口实现类

代码语言:javascript复制
@Override
	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) throws ShopOperationException {
		// 前台页面插入的pageIndex(第几页), 而dao层是使用 rowIndex (第几行) ,所以需要转换一下
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		List<Shop> shopList = new ArrayList<Shop>();
		ShopExecution se = new ShopExecution();
		// 查询带有分页的shopList
		shopList = shopDao.selectShopList(shopCondition, rowIndex, pageSize);
		// 查询符合条件的shop总数
		int count = shopDao.selectShopCount(shopCondition);
		// 将shopList和 count设置到se中,返回给控制层
		if (shopList != null) {
			se.setShopList(shopList);
			se.setCount(count);
		} else {
			se.setState(ShopStateEnum.INNER_ERROR.getState());
		}
		return se;
	}

分页工具类

负责将前台的PageIndex转换为DAO层的rowIndex.

代码语言:javascript复制
package com.artisan.o2o.util;

/**
 * 
 * 
 * @ClassName: PageCalculator
 * 
 * @Description: 将前台使用的pageIndex 转换为 dao层使用的 rowIndex
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年6月7日 上午12:24:38
 */
public class PageCalculator {

	public static int calculateRowIndex(int pageIndex, int pageSize) {
		return (pageIndex > 0) ? (pageIndex - 1) * pageSize : 0;
	}
}

单元测试

tb_shop中的数据

代码语言:javascript复制
@Test
	public void testGetShopList() {

		Shop shopCondition = new Shop();
		PersonInfo personInfo = new PersonInfo();
		personInfo.setUserId(1L);

		shopCondition.setOwner(personInfo);
		shopCondition.setShopName("咖啡");

		// 符合 shop_name like '%咖啡%' 且 owner_id =1 有3条数据,

		// 第二个参数 和 第三个参数 从pageIndex=1 第一页取数据,取2条 pageSize=2
		ShopExecution se = shopService.getShopList(shopCondition, 1, 2);
		// 按照tb_shop中的数据筛选 符合条件的数据3条, 从第一页开始取2条,se.getShopList().size() 应该有2条数据,
		Assert.assertNotNull(se);
		Assert.assertEquals(2, se.getShopList().size());
		Assert.assertEquals(3, se.getCount());

		// 按照tb_shop中的数据筛选 符合条件的数据3条, 从第2页开始取2条,se.getShopList().size()
		// 应该只有1条数据,总数仍为3
		se = shopService.getShopList(shopCondition, 2, 2);
		Assert.assertNotNull(se);
		Assert.assertEquals(1, se.getShopList().size());
		Assert.assertEquals(3, se.getCount());
	}
代码语言:javascript复制
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@24ba9639] will not be managed by Spring
==>  Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ORDER BY s.priority DESC LIMIT ? , ? 
==> Parameters: 1(Long), 0(Integer), 2(Integer)
<==    Columns: shop_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice, area_id, area_name, shop_category_id, shop_category_name
<==        Row: 24, 咖啡点, 小工匠的咖啡店, NanJing, 9876553, uploaditemshopImage242018052118095757182.jpg, 99, 2018-05-21 18:09:57.0, 2018-05-21 18:09:57.0, 0, 审核中, 1, 北京, 1, 咖啡奶茶
<==        Row: 25, 咖啡店Improve, 小工匠的咖啡店Improve, NanJing-Improve, 9876553, uploaditemshopImage252018052214472089649.jpg, 99, 2018-05-22 14:46:16.0, 2018-05-22 14:46:18.0, 0, 审核中Improve, 1, 北京, 1, 咖啡奶茶
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74bf1791]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@12f9af83] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7e6ef134] will not be managed by Spring
==>  Preparing: SELECT count(1) FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id 
==> Parameters: 1(Long)
<==    Columns: count(1)
<==        Row: 3
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@12f9af83]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f132176] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@2631f68c] will not be managed by Spring
==>  Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id ORDER BY s.priority DESC LIMIT ? , ? 
==> Parameters: 1(Long), 2(Integer), 2(Integer)
<==    Columns: shop_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice, area_id, area_name, shop_category_id, shop_category_name
<==        Row: 28, Modify咖啡店, Modify小工匠的咖啡店28, Modify-NanJing218, 12345628, uploaditemshopImage282018060500333541696.jpg, 78, 2018-05-28 23:13:37.0, 2018-06-06 20:56:31.0, 0, null, 2, 上海, 2, 咖啡
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f132176]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bcef303] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@33308786] will not be managed by Spring
==>  Preparing: SELECT count(1) FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.shop_name like '%咖啡%' and s.owner_id = ? AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id 
==> Parameters: 1(Long)
<==    Columns: count(1)
<==        Row: 3
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bcef303]

Github地址

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

0 人点赞