乐优商城(六)
品牌查询和分类查询差不多,相似的地方我就不再赘述了,主要说说这里需要思考的地方
——————————————————————————————————
实现品牌查询
分析参数
(ps:本人是主攻后端,前端部分是已经是准备好的资料,直接使用,某些实现我可能也不了解)
老规矩:先看页面所需要条件
五个参数:
- 排序方式 : id 或者首字母;
- 关键字搜索:可检索首字母和名称;
- 每页大小;
- 升序降序;
- 当前页从第x条开始;
后端实现
pojo类
package com.leyou.lyitem.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "tb_brand")
@Data
public class Brand {
@Id
@KeySql(useGeneratedKeys=true)
private Long id;
private String name;// 品牌名称
private String image;// 品牌图片
private Character letter;
// getter setter 略
}
三层框架
今天流程稍稍不一样,我们先写Mapper,再写Controller最后写Service。
Mapper
先写Mapper是因为他最简单:
package com.leyou.lyitem.mapper;
import com.leyou.lyitem.pojo.Brand;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface BrandMapper extends Mapper<Brand> {
}
Controller
这里是需要思考一点的,queryBrandByPage方法的参数我们刚才已经分析过了,但是后端具体怎么做又得设计一下。
像开始页,页大小,排序方式是应该有个默认值的,这些参数很重要,但是我们又不想一直自己设置,最好的方式就是赋默认值。
检索条件是非必需的,所以在请求参数设置中required = false。
说完参数我们再想想返回值类型,list集合吗?不止,我们还要有数据总条数,总页数回写在页面上,这时我们就需要一个对象封装这些数据,整合到一起,所以要写一个通用的返回结果类PageResult。
这里有个bug,我使用@AllArgsConstructor,@NoArgsConstructor再写一个两个参数的构造函数会报错,我不知道为啥,因为我不用注解就可以解决问题,所以没有深究
package com.leyou.common.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
public class PageResult<T> {
private Long total;// 总条数
private Long totalPage;// 总页数
private List<T> items;// 当前页数据
public PageResult() {
}
public PageResult(Long total, List<T> items) {
this.total = total;
this.items = items;
}
public PageResult(Long total, Long totalPage, List<T> items) {
this.total = total;
this.totalPage = totalPage;
this.items = items;
}
}
package com.leyou.lyitem.web;
import com.leyou.common.vo.PageResult;
import com.leyou.lyitem.pojo.Brand;
import com.leyou.lyitem.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description: 品牌查询表现层
* @author: 废柴少爷
* @date: 2021年01月27日 21:50
*/
@RestController
@RequestMapping("brand")
public class BrandController {
@Autowired
private BrandService brandService;
/**
* 分页查询品牌
* @param page
* @param rows
* @param sortBy
* @param desc
* @param key
* @return
*/
@GetMapping("page")
public ResponseEntity<PageResult<Brand>> queryBrandByPage(
@RequestParam(value = "page",defaultValue = "1") Integer page,
@RequestParam(value = "rows",defaultValue = "5") Integer rows,
@RequestParam(value = "sortBy",required = false) String sortBy,
@RequestParam(value = "desc",defaultValue = "false") Boolean desc,
@RequestParam(value = "key",required = false) String key
){
return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,sortBy,desc,key));
}
}
Service
这里是处理请求的核心地方,也是最复杂的地方,下面有几个要注意的地方:
分页:我们交给分页助手来做,只需要把开始页和页大小传过去即可;
过滤:就是sql语句的where部分,这里用的是通用mapper的语法,当有检索条件时,进入语句“example.createCriteria().orLike(“name”,"%"+key+"%").orEqualTo(“letter”,key.toUpperCase());”等价于where name like “%”+key+"%" or letter = key.toUpperCase()。
排序:example.setOrderByClause(orderByClause);是设置排序方式,这里有一点要注意,orderByClause是字符串,第一位要是空格(如:" 123"),不然拼接的时候会和前面的字符连在一起。
我踩的坑:
package com.leyou.lyitem.service;
import com.leyou.common.vo.PageResult;
import com.leyou.lyitem.pojo.Brand;
import java.util.List;
public interface BrandService {
PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key);
}
package com.leyou.lyitem.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.lyitem.mapper.BrandMapper;
import com.leyou.lyitem.pojo.Brand;
import com.leyou.lyitem.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/**
* @Description: 品牌查询业务层
* @author: 废柴少爷
* @date: 2021年01月27日 21:49
*/
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
private BrandMapper brandMapper;
@Override
public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
//分页助手
PageHelper.startPage(page,rows);
//过滤
Example example = new Example(Brand.class);
if (StringUtils.isNotBlank(key)){
example.createCriteria().orLike("name","%"+key+"%").
orEqualTo("letter",key.toUpperCase());
}
//排序
if (StringUtils.isNotBlank(sortBy)){
String orderByClause= sortBy + (desc ? " DESC" : " ASC");
example.setOrderByClause(orderByClause);
}
//查询
List<Brand> list = brandMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)){
throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
}
//解析分页结果
PageInfo<Brand> pageInfo = new PageInfo<>(list);
return new PageResult<>(pageInfo.getTotal(),list);
}
}
最后我们新加了一种异常,要在枚举异常中加上:
BRAND_NOT_FOUND(404,"品牌没查到"),
插曲
前端部分按理说不应该有问题,但是在检索时会报异常,比如我在第9页查检索x,但是x相关信息两页就完了,我现在在第九页,就出错了,所以要改一下:
this.pagination.page = 1;