【樂優商城】品牌分頁查詢
阿新 • • 發佈:2018-12-19
1、分頁結果的封裝類:
響應結果:
-
total:總條數
-
items:當前頁資料
-
totalPage:有些還需要總頁數
<!-- 分頁助手啟動器 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pageHelper.starter.version}</version> </dependency>
這裡我們封裝一個類,來表示分頁結果:
package com.leyou.common.pojo; import java.util.List; /** * @author: Lucifer * @create: 2018-11-03 14:56 * @description: **/ 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; } 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; } }
2、pojo:品牌的實體類。
注:@Table(name="資料庫中對應的表名")
@Getter,@Setter,@ToString這三個註解是lombok,需要引入這個jar包,可以不需要再去寫get/set/tostring等方法了,具體可以查百度。
dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
package com.leyou.item.pojo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author: Lucifer
* @create: 2018-11-03 15:00
* @description:
**/
@Getter
@Setter
@ToString
@Table(name = "tb_brand")
public class Brand {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;// 品牌名稱
private String image;// 品牌圖片
private Character letter;
}
3、品牌的controller方法:
package com.leyou.item.controller;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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: Lucifer
* @create: 2018-11-03 15:04
* @description:
**/
@RestController
@RequestMapping("brand")
public class BrandController {
@Autowired
private BrandService brandService;
@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> result = this.brandService.queryBrandByPageAndSort(page, rows, sortBy, desc, key);
if (result == null || result.getItems().size() == 0) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return ResponseEntity.ok(result);
}
}
@RequestParam:
HttpStatus.NOT_FOUND 對應的是404,通過原始碼可以看到:
4、使用通用mapper可以簡化開發。
package com.leyou.item.mapper;
import com.leyou.item.pojo.Brand;
import tk.mybatis.mapper.common.Mapper;
public interface BrandMapper extends Mapper<Brand> {
}
使用通用mapper的話,簡單的增刪改查就不需要去寫sql語句,底層都已經封裝好了。
5、品牌的實現類:
這裡為了方便直接寫類 BrandService ,並沒有service介面;當然在公司不能這麼做。
package com.leyou.item.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
/**
* @author: Lucifer
* @create: 2018-11-03 15:05
* @description:
**/
@Service
public class BrandService {
@Autowired
private BrandMapper brandMapper;
public PageResult<Brand> queryBrandByPageAndSort(
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().andLike("name", "%" + key + "%")
.orEqualTo("letter", key);
}
if (StringUtils.isNotBlank(sortBy)) {
// 排序
String orderByClause = sortBy + (desc ? " DESC" : " ASC");
example.setOrderByClause(orderByClause);
}
// 查詢
Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
// 返回結果
return new PageResult<>(pageInfo.getTotal(), pageInfo);
}
}
6、測試
檢視控制檯的sql語句的列印,應該就可以知道service執行了什麼了:
可以看下圖:
通過debug可以看到是通過@Table(name="")去找到對應的表的。
至於怎麼去增刪改查,這裡就不把截圖發出來了,可以自己去debug看看。