标签:image str character require category led 搜索 sort data
1.数据库表
CREATE TABLE `tb_brand` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘品牌id‘,
`name` varchar(50) NOT NULL COMMENT ‘品牌名称‘,
`image` varchar(200) DEFAULT ‘‘ COMMENT ‘品牌图片地址‘,
`letter` char(1) DEFAULT ‘‘ COMMENT ‘品牌的首字母‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=325400 DEFAULT CHARSET=utf8 COMMENT=‘品牌表,一个品牌下有多个商品(spu),一对多关系‘;
品牌和商品分类之间是多对多关系,需要一张中间表来维护两者间关系
CREATE TABLE `tb_category_brand` (
`category_id` bigint(20) NOT NULL COMMENT ‘商品类目id‘,
`brand_id` bigint(20) NOT NULL COMMENT ‘品牌id‘,
PRIMARY KEY (`category_id`,`brand_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘商品分类和品牌的中间表,两者是多对多关系‘;
没有设置外键约束
在代码中通过逻辑来维护表关系,而不设置外键。
2.建立实体类
@Table(name = "tb_brand")
public class Brand {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;// 品牌名称
private String image;// 品牌图片
private Character letter;
// getter setter 忽略吧
}
3.mapper(通用mapper来简化开发)
public interface BrandMapper extends Mapper<Brand> { }
3.1.引入通用mapper依赖
<!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version>
</dependency>
3.2.在启动类上记得添加@MapperScan(mapper包路径)
@SpringBootApplication @EnableDiscoveryClient @MapperScan("com.leyou.item.mapper") // mapper接口的包扫描 public class ItemServiceApplication { public static void main(String[] args) { SpringApplication.run(ItemServiceApplication.class, args); } }
4.Controller类编写
编写controller先思考四个问题,参照前端页面的控制台
请求方式:查询,肯定是Get
请求路径:分页查询,/brand/page
请求参数:根据我们刚才编写的页面,有分页功能,有排序功能,有搜索过滤功能,因此至少要有5个参数:
page:当前页,int
rows:每页大小,int
sortBy:排序字段,String
desc:是否为降序,boolean
key:搜索关键词,String
响应结果:分页结果一般至少需要两个数据
total:总条数
items:当前页数据
4.1首先需要封装一个类,来表示分页结果:
public class PageResult<T> { private Long total;// 总条数 private Integer 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; } public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public List<T> getItems() { return items; } public void setItems(List<T> items) { this.items = items; } public Long getTotalPage() { return totalPage; } public void setTotalPage(Long totalPage) { this.totalPage = totalPage; } }
4.2编写controller类
@RestController @RequestMapping("brand") public class BrandController { @Autowired private BrandService brandService; /** * 根据查询条件分页并排序查询品牌信息 * @param key * @param page * @param rows * @param sortBy * @param desc * @return */ @GetMapping("page") public ResponseEntity<PageResult<Brand>> queryBrandsByPage( @RequestParam(value = "key", required = false)String key, @RequestParam(value = "page", defaultValue = "1")Integer page, @RequestParam(value = "rows", defaultValue = "5")Integer rows, @RequestParam(value = "sortBy", required = false)String sortBy, @RequestParam(value = "desc", required = false)Boolean desc ){ PageResult<Brand> result = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc); if (CollectionUtils.isEmpty(result.getItems())){ return ResponseEntity.notFound().build(); } return ResponseEntity.ok(result); } }
5.编写server类
@Service public class BrandService { @Autowired private BrandMapper brandMapper; /** * 根据查询条件分页并排序查询品牌信息 * * @param key * @param page * @param rows * @param sortBy * @param desc * @return */ public PageResult<Brand> queryBrandsByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) { // 初始化example对象 Example example = new Example(Brand.class); Example.Criteria criteria = example.createCriteria(); // 根据name模糊查询,或者根据首字母查询 if (StringUtils.isNotBlank(key)) { criteria.andLike("name", "%" + key + "%").orEqualTo("letter", key); } // 添加分页条件 PageHelper.startPage(page, rows); // 添加排序条件 if (StringUtils.isNotBlank(sortBy)) { example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc")); } List<Brand> brands = this.brandMapper.selectByExample(example); // 包装成pageInfo PageInfo<Brand> pageInfo = new PageInfo<>(brands); // 包装成分页结果集返回 return new PageResult<>(pageInfo.getTotal(), pageInfo.getList()); } }
标签:image str character require category led 搜索 sort data
原文地址:https://www.cnblogs.com/zxh06820/p/12913136.html