1. ShopDao接口
在shopDao接口中添加:
/**
* 分页查询店铺,可输入的条件有:店铺名(模糊),店铺状态,店铺类别,区域id,owner
* @param shopCondition
* @param rowIndex 从第几行开始取数据
* @param pageSize 返回的条数
* @return
*/
List<Shop> queryShopList(
@Param("shopCondition")Shop shopCondition,
@Param("rowIndex")int rowIndex,
@Param("pageSize")int pageSize);
/**
* 返回queryShopList总数
* @param shopCondition
* @return
*/
int queryShopCount(@Param("shopCondition") Shop shopCondition);
2. ShopDao.xml实现类
在ShopDao.xml中添加:
<sql id="selectShopByCondition">
<!-- 可输入的查询条件:
商铺名(要求模糊查询)
区域Id
商铺状态
商铺类别
owner -->
<!-- 商铺名(要求模糊查询) -->
<if test="shopCondition.shopName != null and '' != shopCondition.shopName">
and s.shop_name like '%${shopCondition.shopName}%'
</if>
<!-- 区域Id -->
<if test="shopCondition.area != null and shopCondition.area.areaId != null ">
and s.area_id = #{shopCondition.area.areaId}
</if>
<!-- 商铺状态 -->
<if test="shopCondition.enableStatus !=null">
and s.enable_status = #{shopCondition.enableStatus}
</if>
<!-- 商铺类别 -->
<if test="shopCondition.shopCategory != null and shopCondition.shopCategory.shopCategoryId != null ">
and s.shop_category_id = #{shopCondition.shopCategory.shopCategoryId}
</if>
<!-- owner -->
<if test="shopCondition.owner != null and shopCondition.owner.userId != null">
and s.owner_id = #{shopCondition.owner.userId}
</if>
</sql>
<select id="queryShopList" resultMap="shopMap">
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>
<include refid="selectShopByCondition"/>
</where>
AND s.area_id = a.area_id
AND s.shop_category_id = sc.shop_category_id
ORDER BY s.priority DESC
LIMIT #{rowIndex} , #{pageSize}
</select>
<!--返回店铺展示列表总数-->
<select id="queryShopCount" resultType="Integer">
SELECT
count(1)
FROM
tb_shop s,
tb_area a,
tb_shop_category sc
<where>
<include refid="selectShopByCondition"/>
</where>
AND s.area_id = a.area_id
AND s.shop_category_id = sc.shop_category_id
</select>
3. ShopDaoTest测试类
@Test
public void testQueryShopListAndCount(){
Shop shopCondition = new Shop();
PersonInfo owner = new PersonInfo();
owner.setUserId(2L);
shopCondition.setOwner(owner);
List<Shop> shopList =
shopDao.queryShopList(shopCondition,0,2);
System.out.println("店铺列表大小:"+shopList.size());
int i = shopDao.queryShopCount(shopCondition);
System.out.println(i);
}