• 019 品牌的查询


    先看看我们要实现的效果:

    点击“品牌管理”菜单:

    路由路径:/item/brand

    根据路由文件知,对应的页面是:src/pages/item/Brand.vue

    页面会发送如下请求:

     注意:程序的编写逻辑

    01 Dao层--------数据库对应的实体类及mapper映射

    02 Service层-------编写具体的查询方法

    03 Controller层------接收url发送的参数和调用service方法

     

    1.逻辑思路分析

    2.后台提供查询接口

    (1)分页结果类PageResult

    分页结果类PageResult以后可能在其它项目中也有需求,因此我们将其抽取到leyou-common中,提高复用性。

    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;
        }
    }

    不要忘记在leyou-item-service工程的pom.xml中引入leyou-common的依赖

    <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.效果图

  • 相关阅读:
    未定义的标示符“RECT”,引入了windows.h头文件也没有用?
    解决Opencv高低版本不兼容问题
    在OpenCV2.2后的版本中没有CvvImage类的解决方法(及出现错误:IntelliSense: 未定义标识符 "CvvImage" )
    opencv中Mat与IplImage,CVMat类型之间转换
    opencv中VideoCapture和cvCapture有什么区别?
    2019-2020-1 20175302_20175314_20175316 实验三 并发程序
    2019-2020-1 20175314 《信息安全系统设计基础》第8周学习总结
    2019-2020-1 20175302_20175314_20175316 实验二 固件程序设计
    2019-2020-1 20175302_20175314_20175316 实验一 开发环境的熟悉
    *2019-2020-1 20175302_20175314_20175316 实验一 开发环境的熟悉*
  • 原文地址:https://www.cnblogs.com/luckyplj/p/11494053.html
Copyright © 2020-2023  润新知