我们想要查看商铺拥有者的所有商铺并且进行修改等,我们需要前端传来的信息来查看商铺拥有者的id以此为依据来查看此人拥有的所有商铺,并且在所有商铺显示列表有修改按钮,因为可能查出很多店铺,所以牵扯到分页问题我们也要考虑进去分页的问题
如图
接下来实现dao层:
我们通过一个userid来查询此人的店铺。
接下来实现mapper.xml
查询店铺信息:
<!--查询shop的信息,通过shopId,因为还要查出来shop的categoryName等信息,我们用resultMap-->
<resultMap id="shopMap" type="shop">
<!--基本类型查询-->
<id column="shop_id" property="shopId" />
<result column="shop_name" property="shopName"/>
<result column="shop_desc" property="shopDesc"/>
<result column="shop_addr" property="shopAddr"/>
<result column="phone" property="phone"/>
<result column="shop_img" property="shopImg"/>
<result column="priority" property="priority"/>
<result column="create_time" property="createTime"/>
<result column="last_edit_time" property="lastEditTime"/>
<result column="enable_status" property="enableStatus"/>
<result column="advice" property="advice"/>
<!--复合对象-->
<association property="owner" column="user_id" javaType="personinfo">
<id column="user_id" property="userId"></id>
<result column="name" property="name"></result>
</association>
<association property="area" column="area_id" javaType="area">
<id column="area_id" property="areaId"></id>
<result column="area_name" property="areaName"></result>
</association>
<association property="shopCategory" column="shop_categoryId" javaType="shopCategory">
<id column="shop_category_id" property="shopCategoryId"></id>
<result column="shop_category_name" property="shopCategoryName"></result>
</association>
</resultMap>
<!--分页查询店铺列表 ,可选条件有店铺名,店铺状态,店铺类别,区域id,owner-->
<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>
<if test="shopCondition.shopCategory!=null and shopCondition.shopCategory.shopCategoryId!=null">
and s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId}
</if>
<if test="shopCondition.area!=null and shopCondition.area.areaId!=null">
and s.area_id=#{shopCondition.area.areaId}
</if>
<if test="shopCondition.shopName!=null">
and s.shop_name like '%${shopCondition.shopName}%'
</if>
<if test="shopCondition.enableStatus!=null">
and s.enable_status=#{shopCondition.enableStatus}
</if>
<if test="shopCondition.owner!=null and shopCondition.owner.userId!=null">
and s.owner_id=#{shopCondition.owner.userId}
</if>
AND s.area_id=a.area_id
AND s.shop_category_id=sc.shop_category_id
</where>
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>
<if test="shopCondition.shopCategory!=null and shopCondition.shopCategory.shopCategoryId!=null">
and s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId}
</if>
<if test="shopCondition.area!=null and shopCondition.area.areaId!=null">
and s.area_id=#{shopCondition.area.areaId}
</if>
<if test="shopCondition.shopName!=null">
and s.shop_name like '%${shopCondition.shopName}%'
</if>
<if test="shopCondition.enableStatus!=null">
and s.enable_status=#{shopCondition.enableStatus}
</if>
<if test="shopCondition.owner!=null and shopCondition.owner.userId!=null">
and s.owner_id=#{shopCondition.owner.userId}
</if>
AND s.area_id=a.area_id
AND s.shop_category_id=sc.shop_category_id
</where>
</select>
我们看到select语句有许多</if>代码块,这用来使用不同条件组合查询使用。
Service层:
serviceimpl:
/*
* 根据页数和显示数量和shop条件得到shoplist的信息和count
* */
@Override
public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
//先通过每页容纳的数量和页数将pageIndex转为rowIndex
int rowIndex= Pagecalculator.calculateRowIndex(pageIndex,pageSize);
//返回shop的list集合
List<Shop> shopList=shopDao.queryShopList(shopCondition,rowIndex,pageSize);
//计算符合条件的总记录数
int count=shopDao.queryShopCount(shopCondition);
//建立存储对象
ShopExecution shopExecution=new ShopExecution();
if(shopList!=null){
shopExecution.setShopList(shopList);
shopExecution.setCount(count);
}else {
shopExecution.setState(ShopStateEnum.INNER_ERROR.getState());
}
return shopExecution; //成功返回存储shop集合的shopExecution
}
这里没什么说的,当我们成功操作时,把相应的shopList和count存入shopExceution交出去,主要说的是这个方法
//先通过每页容纳的数量和页数将pageIndex转为rowIndex
int rowIndex= Pagecalculator.calculateRowIndex(pageIndex,pageSize);
想象下,前端都是显示的第几页,而后端显示的是第几条数据开始,所以我们需要将前端的数据的页数改为数据库中的第几行dao层才能操作,我们实现一个具体转换工具类。
我们要实现两个部分。
第一个部分是显示出商铺列表。
/*
* 通过owner查询条件返回当前用户拥有商铺列表
* */
@RequestMapping(value = "/getshoplist",method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getShopList(HttpServletRequest request) throws JsonProcessingException
{
Map<String,Object> modelMap=new HashMap<String,Object>();
PersonInfo user=new PersonInfo();
user.setUserId(1L);//先自己手动设置
user.setName("test");//返回名字,方便前台显示
request.getSession().setAttribute("user",user);//先手动设置默认值
user= (PersonInfo) request.getSession().getAttribute("user");//前面是先手动配置,目前功能未完善
try{
Shop shop=new Shop();
shop.setOwner(user);
ShopExecution shopExecution=shopService.getShopList(shop,0,100);//手动设置输出100个店铺
modelMap.put("shopList",shopExecution.getShopList());
modelMap.put("user",user);
modelMap.put("success",true);
}catch (Exception e){
modelMap.put("success",false);
modelMap.put("errMsg",e.getMessage());
}
return modelMap;//返回正确信息
}
进入
当我们点击进入时:
出现这样的页面:
这是注意上面的url带着id进去的
然后:
点击商铺信息进行修改:
注意这些操作都带着id传入,具体的页面实现通过前端,我们要说的是,如果我们直接打开
这个页面时,那么我们希望它返回到商铺列表页面去,因为直接打开并没有带着shopId参数传入。这就时我们第二个任务处理的事情
controller(二):
/*进入店铺管理界面
*
* 从商铺列表页面中,点击“进入”按钮进入
* 某个商铺的管理页面的时候,对session中的数据的校验从而进行页面的跳转,是否跳转到店铺列表页面或者可以直接操作该页面
* */
@RequestMapping(value = "/getshopmanageinfo", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopManageinfo(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 获取shopId
long shopId = HttpServletRequestUtil.getLong(request, "shopId");
// 如果shopId不合法
if (shopId < 0) {
// 尝试从当前session中获取
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
if (currentShop == null) {
// 如果当前session中也没有shop信息,告诉view层 重定向
modelMap.put("redirect", true);
modelMap.put("url", "/storepro/shopadmin/shoplist");
}else{
// 告诉view层 进入该页面
modelMap.put("redirect", false);
modelMap.put("shopId", currentShop.getShopId());
}
} else { // shopId合法的话
Shop shop = new Shop();
shop.setShopId(shopId);
// 将currentShop放到session中
request.getSession().setAttribute("currentShop", shop);
modelMap.put("redirect", false);
}
return modelMap;
}
这个contr方法实现了下面的步骤:
先通过获取request请求里的shopid
如果shopId不合法我们试着从session中取出,如果都没有那么说明我们直接打开了这个页面或者传入的id不合法,我们直接将list页面的url存入map交给前端跳转回list页面,如果成功获取正确id,我们进入该页面,我们将正确表示交给前端,由前端处理跳转到修改商铺的页面。
VIEW层:
概述
大致效果如下