019 品牌的查询
先看看我们要实现的效果:
点击“品牌管理”菜单:
根据路由文件知,对应的页面是:src/pages/item/Brand.vue
注意:程序的编写逻辑
01 Dao层--------数据库对应的实体类及mapper映射
02 Service层-------编写具体的查询方法
03 Controller层------接收url发送的参数和调用service方法
1.逻辑思路分析
2.
package lucky.leyou.common.domain; import java.util.List; public class PageResult<T> { private Long total;// 总条数 private Integer totalPage;// 总页数 private List<T> items;// 当前页数据 public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public List<T> getItems() { return items; } public void setItems(List<T> items) { this.items = items; } }
<dependency> <groupId>lucky.leyou.common</groupId> <artifactId>leyou-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
(2)实体类
package lucky.leyou.item.domain; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name = "tb_brand") public class Brand { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name;// 品牌名称 private String image;// 品牌图片 private Character letter; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Character getLetter() { return letter; } public void setLetter(Character letter) { this.letter = letter; } }
(3)mapper
通用mapper来简化开发:
package lucky.leyou.item.mapper; import lucky.leyou.item.domain.Category; import tk.mybatis.mapper.common.Mapper; public interface BrandMapper extends Mapper<Category> { }
(4)service及其实现类
接口
package lucky.leyou.item.service; import lucky.leyou.common.domain.PageResult; import lucky.leyou.item.domain.Brand; public interface IBrandService { /** * 根据查询条件分页并排序查询品牌信息 * @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); }
实现类
package lucky.leyou.item.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lucky.leyou.common.domain.PageResult; import lucky.leyou.item.domain.Brand; import lucky.leyou.item.mapper.BrandMapper; import lucky.leyou.item.service.IBrandService; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import tk.mybatis.mapper.entity.Example; import java.util.List; @Service public class BrandServiceImpl implements IBrandService { @Autowired private BrandMapper brandMapper; /** * 根据查询条件分页并排序查询品牌信息 * @param key * @param page * @param rows * @param sortBy * @param desc * @return */ @Override 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或根据数据库表字段letter进行首字母查询 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为github上开源的分页工具包的工具类 PageInfo<Brand> pageInfo=new PageInfo<>(brands); //包装成分页结果集返回 return new PageResult<>(pageInfo.getTotal(),pageInfo.getList()); } }
(5)controller
package lucky.leyou.item.controller; import lucky.leyou.common.domain.PageResult; import lucky.leyou.item.domain.Brand; import lucky.leyou.item.service.impl.BrandServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping(path = "/brand") public class BrandController { @Autowired private BrandServiceImpl brandService; /** * 根据查询条件分页并排序查询品牌信息 * @param key * @param page * @param rows * @param sortBy * @param desc * @return */ @RequestMapping(path = "/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 ){ //01 调用service中的方法进行品牌查询,返回查询结果集 PageResult<Brand> brandPageResult = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc); //02 判断返回结果集是否为null 或者返回的结果集的子集为空 if(CollectionUtils.isEmpty(brandPageResult.getItems())){ //404 return ResponseEntity.notFound().build(); } //查询成功 return ResponseEntity.ok(brandPageResult); } }
2.效果图
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步