shop--7.店铺编辑和列表--店铺列表 分页查询,模糊查询--dao层实现
在service层中, 有一个问题,就是:
dao层中的分页查询传参,传入的是rowIndex(从第几行开始查询), pageSize(查询多少条记录)
而在service层中分页查询传的是pageIndex(第几页),pageSize
所以在第一个参数要有一个转换才可以
所以编写一个工具类,将pageIndex转为rowIndex
public class PageCalculator {
/**
* 将pageIndex(第几页)转换为查询结果中的第几行rowIndex
* @param pageIndex
* @param pageSize
* @return
*/
public static int calculateRowIndex(int pageIndex, int pageSize){
return (pageIndex > 0) ? (pageIndex - 1) * pageSize : 0;
}
}
service接口
/**
* 实现分页查询店铺,通过条件组合,来筛选出条件范围内的店铺列表
* @param shopCondition
* @param pageIndex 第几页
* @param pageSize 一页中数据的数量
* @return ShopExecution 其中包含shopList和count
*/
public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize);
serviceImpl
public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
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;
}
controller层实现
/**
* 实现分页查询店铺,通过条件组合,来筛选出条件范围内的店铺列表
* @param request
* @return
*/
@RequestMapping(value="/getshoplist", method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopList(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
PersonInfo user = new PersonInfo();
user.setUserId( 1L );
ShopExecution shopExecution = null;
try{
Shop shopCondition = new Shop();
shopCondition.setOwner( user );
shopCondition.setShopName("f");
Area area = new Area();
area.setAreaId( 2 );
shopCondition.setArea(area);
shopExecution = shopService.getShopList( shopCondition, 1, 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.toString() );
}
return modelMap;
}
这个不知道是做什么的
/**
*管理session相关的
* @param request
* @return
*/
@RequestMapping(value="/getshopmanagementinfo", method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopManagementInfo(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
long shopId = (long) request.getSession().getAttribute("shopId");
//如果shopId不存在
if(shopId <= 0){
//判断当前有没有登录
Object currentShopObj = request.getSession().getAttribute("currentShop");
//如果当前既没有shopId传入,也没有店铺登录的话,就重定向到店铺列表页面
if(currentShopObj == null){
modelMap.put( "redirect", true );
modelMap.put( "url", "/shop/shoplist" );
}else{
//如果当前有店铺登录的话
Shop currentShop = (Shop)currentShopObj;
modelMap.put( "redirect", false );
modelMap.put( "shopId", currentShop.getShopId());
}
}else{
//如果当前有shopId
Shop currentShop = new Shop();
currentShop.setShopId(shopId);
request.getSession().setAttribute("currentShop", currentShop);
modelMap.put( "redirect", false );
}
return modelMap;
}