商品品牌分页、过滤、排序查询的完成流程

//1、首先分析前台页面的请求,从中获取四要素:请求方式、请求参数、请求路径、返回值类型   2、书写controller、service、mapper、pojo

//品牌页面

<template>  <div>    <v-layout class="px-2">      <v-flex xs2>        <v-btn color="info" small>新增品牌</v-btn>      </v-flex>      <v-spacer/>      <v-flex xs4>        <v-text-field label="搜索" hide-details append-icon="search" v-model="key"></v-text-field>      </v-flex>    </v-layout>    <v-data-table      :headers="headers"      :items="brands"      :pagination.sync="pagination"      :total-items="totalBrands"      :loading="loading"      class="elevation-1"    >      <template slot="items" slot-scope="props">        <td class="text-xs-center">{{props.item.id}}</td>        <td class="text-xs-center">{{props.item.name}}</td>        <td class="text-xs-center"><img :src="props.item.image" alt=""/></td>        <td class="text-xs-center">{{props.item.letter}}</td>        <td class="text-xs-center">          <v-btn flat icon color="info">            <v-icon>edit</v-icon>          </v-btn>          <v-btn flat icon color="error">            <v-icon>delete</v-icon>          </v-btn>        </td>      </template>      error    </v-data-table>  </div></template>

<script>  export default {    name: "my-brand",    data() {      return {        headers: [          {text: "品牌ID", value: "id", align: "center", sortable: true},          {text: "品牌名称", value: "name", align: "center", sortable: false},          {text: "品牌LOGO", value: "image", align: "center", sortable: false},          {text: "品牌首字母", value: "letter", align: "center", sortable: true},          {text: "修改,删除", align: "center", sortable: false},        ],        brands: [],        pagination: {},        totalBrands: 0,        loading: false,        key:"", //搜索条件      }    },    created() {      this.brands = [        {id: 2032, name: "OPPO", image: "1.jpg", letter: "O"},        {id: 2033, name: "飞利浦", image: "2.jpg", letter: "F"},        {id: 2034, name: "华为", image: "3.jpg", letter: "H"},        {id: 2036, name: "酷派", image: "4.jpg", letter: "K"},        {id: 2037, name: "魅族", image: "5.jpg", letter: "M"},      ];      this.totalBrands = 15;      //发送请求去后台查询      this.loadBrands();    },    watch:{      key(){        this.pagination.page=1;        //this.loadBrands();      },      pagination:{        deep:true,        handler(){          this.loadBrands();        }      }    },    methods:{      loadBrands(){        this.loading = true;        this.$http.get("/item/brand/page",{          params:{            page:this.pagination.page,   //当前页            rows:this.pagination.rowsPerPage,  //每页大小            sortBy:this.pagination.sortBy,   //排序字段            desc:this.pagination.descending,  //是否降序            key:this.key    //搜索条件          }        }).then(resp=>{          //console.log(resp);          this.brands = resp.data.items;          this.totalBrands = resp.data.total          this.loading = false;        })      }    }  }</script>

<style scoped>

</style>

//  controller层package com.leyou.item.web;

import com.leyou.common.vo.PageResult;import com.leyou.item.pojo.Brand;import com.leyou.item.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;

/** * @author newcityman * @date 2019/12/26 - 14:23 */@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    ){        PageResult<Brand> pageResult = brandService.queryBrandByPage(page,rows,sortBy,desc,key);        return ResponseEntity.ok(pageResult);    }}
//  service层
package com.leyou.item.service;

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.item.mapper.BrandMapper;import com.leyou.item.pojo.Brand;import org.apache.commons.lang3.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;

/** * @author newcityman * @date 2019/12/26 - 14:22 */@Servicepublic class BrandService {

    @Autowired    private BrandMapper brandMapper;

    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> info = new PageInfo<>(list);

        return new PageResult<>(info.getTotal(), list);    }}
//  mapper层
package com.leyou.item.mapper;

import com.leyou.item.pojo.Brand;import tk.mybatis.mapper.common.Mapper;

/** * @author newcityman * @date 2019/12/26 - 14:21 */public interface BrandMapper extends Mapper<Brand> {}
//  pojo类

package com.leyou.item.pojo;

import lombok.Data;import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;import javax.persistence.Table;

/** * @author newcityman * @date 2019/12/26 - 14:15 */@Data@Table(name = "tb_brand")public class Brand {    @Id    @KeySql(useGeneratedKeys = true)    private Long id;    private String name;      //品牌名称    private String image;     //品牌图片    private Character letter;}
 
 


原文地址:https://www.cnblogs.com/newcityboy/p/12103658.html

时间: 2024-08-30 13:31:25

商品品牌分页、过滤、排序查询的完成流程的相关文章

应用程序框架实战二十四:基础查询扩展 - 分页与排序

上一篇介绍了IQueryable的Where方法存在的问题,并扩展了一个名为Filter的过滤方法,它是Where方法的增强版.本篇将介绍查询的另一个重要主题——分页与排序. 对于任何一个信息系统,查询都需要分页,因为不可能直接返回表中的所有数据. 如果直接使用原始的Ado.Net,我们可以编写一个通用分页存储过程来进行分页查询,然后通过一个DataTable返回给业务层.不过进入Entity Framework时代,分页变得异常简单,通过Skip和Take两个方法配合就可以完成任务. 为了让分

使用插件bootstrap-table实现表格记录的查询、分页、排序等处理

在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有名的开源表格插件,在很多项目中广泛的应用.Bootstrap-table插件提供了非常丰富的属性设置,可以实现查询.分页.排序.复选框.设置显示列.Card view视图.主从表显示.合并列.国际化处理等处理功能,而且该插件同时也提供了一些不错的扩展功能,如移动行.移动列位置等一些特殊的功能,插件可

2016/3/13 七种查询 (普通查询 条件查询 排序查询 模糊查询 统计查询 分组查询 分页查询 )

一句话概括的话,SQL作为结构化查询语言,是标准的关系型数据库通用的标准语言: T-SQL 是在SQL基础上扩展的SQL Server中使用的语言 1,普通查询 #查询Info表中的所有列 select * from Info #查询Info表中的Name和Code列 select Name,Code from Info 2,条件查询 关键字where #查询Info表的左右列 限定范围 列名为p001 select * from Info where 列名="p001" #查询条件之

动态多条件查询分页以及排序(一)--MVC与Entity Framework版url分页版

一.前言 多条件查询分页以及排序  每个系统里都会有这个的代码 做好这块 可以大大提高开发效率  所以博主分享下自己的6个版本的 多条件查询分页以及排序 二.目前状况 不论是ado.net 还是EF 在做多条件搜索时 都有这类似的代码 这样有几个不好的地方 1.当增加查询条件,需要改代码,对应去写相应的代码. 2.对多表查询以及or的支持 不是很好.而我们很常见的需求不可能是一个表的查询 3. 这样写表示层直接出现 了SQL语句 或者 linq 的拉姆达表达式  这是很不好的 表示层不应该知道数

oracle入门(8)——实战:支持可变参数、多种条件、多个参数排序、分页的存储过程查询组件

[本文介绍] 学了好几天,由于项目需要,忙活了两天,写出了个小组件,不过现在还只能支持单表操作.也没考虑算法上的优化,查询速度要比hibernate只快了一点点,可能是不涉及多表查询的缘故吧,多表的情况下才更快. 经非专业的测试,在有分页的情况下,在300万条数据里面查询的时间保持在0.1秒内.相同查询条件+分页的情况下,hibernate 用时0.3秒内. 不分页的条件下,查出来的数据越多,时间越长,时间长的话,跟hibernate相相比就没什么优势了. [思路] 我的思路是从java传来”字

【Spring Data 系列学习】Spring Data JPA 自定义查询,分页,排序,条件查询

Spring Boot Jpa 默认提供 CURD 的方法等方法,在日常中往往时无法满足我们业务的要求,本章节通过自定义简单查询案例进行讲解. 快速上手 项目中的pom.xml.application.properties与 Chapter1 相同 实体类映射数据库表 user 实体类 @Entity public class User implements Serializable { private static final long serialVersionUID = -39076354

MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)

MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作): a.创建2张表 create table userinfo(nid int not null auto_increment primary key, name varchar(10), age int, part_nid int )engine=innodb default charset=utf8; create table part( nid int not null auto_in

SpringBoot JPA实现增删改查、分页、排序、事务操作等功能

今天给大家介绍一下SpringBoot中JPA的一些常用操作,例如:增删改查.分页.排序.事务操作等功能.下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex): public List<User> findByHeightAndSex(int height,char sex); // Or --- 等价于 SQL 中的 or 关键字,比如 findByHeig

夺命雷公狗ThinkPHP项目之----商城8商品品牌管理

本章的主要目标是为了对商品品牌进行CRUD(增删改查)操作. 我们在写的时候就要了解业务逻辑,也就是了解表结构 每个字段代表什么含义,为什么是这个类型的 logo:保存的是图片,一般是分两个途径来保存的, 1.保存图片的本身,保存在目录之下 2.将图片的路径保存到数据表中 我们先创建一个名字为BrandController的控制器,和在view视图里面创建一个Brand的文件夹来进行存放模版文件,如下图所示: 然后将add里面的css和图片路径该下即可代码如下: <!DOCTYPE html P