Skip to content

warehouse-dev-002-paging-fix #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion springBootBlog/src/main/resources/templates/posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h6 class="card-text" th:utext="'Author ID: ' + ${post.authorId}">Author</h6>
</div>
</div>
</div>
<!--
<!--
顯示分頁資訊
https://www.796t.com/article.php?id=200769
-->
Expand Down
12 changes: 12 additions & 0 deletions springWarehouse/data/merchant_sample.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
name,city,type,status
merchant-1,taipei,normal,0
merchant-2,sapporo,normal,0
merchant-3,kushiro,normal,0
merchant-1,taipei,normal,0
merchant-2,sapporo,normal,0
merchant-3,kushiro,normal,0
merchant-1,taipei,normal,0
merchant-2,sapporo,normal,0
merchant-3,kushiro,normal,0
merchant-1,taipei,normal,0
merchant-2,sapporo,normal,0
merchant-3,kushiro,normal,0
merchant-1,taipei,normal,0
merchant-2,sapporo,normal,0
merchant-3,kushiro,normal,0
5 changes: 5 additions & 0 deletions springWarehouse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@
<version>1.7.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
8 changes: 6 additions & 2 deletions springWarehouse/sql/ddl/order.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ CREATE TABLE IF NOT EXISTS orders (

INSERT INTO orders(id, merchant_id, product_id, amount, status, create_time, update_time)
VALUES
("213ac245-7a2a-453b-a4a7-149426b13f84", 1, 1, 1, "completed", now(), now()),
("d42b3224-b715-46af-9294-3b2ecc6ccc7a", 2, 2, 1, "cancelled", now(), now()),
("213ac24efgefdf84", 1, 1, 1, "completed", now(), now()),
("d42b3224-b715-4gfgf-3b2eccc7a", 2, 2, 1, "cancelled", now(), now()),
("d42b322;;4-b715-4gfgf-3b2eccc7a", 2, 2, 1, "cancelled", now(), now()),
("-b715-4gfg;;f-3b2eccc7a", 2, 2, 1, "cancelled", now(), now()),
("-b715-4gfgf-", 2, 2, 1, "cancelled", now(), now()),
("d42b3224--4gfgf-3b2eccc7a", 2, 2, 1, "cancelled", now(), now()),
(uuid(), 1, 3, 1, "pending", now(), now());
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ public static void main(String[] args) {
SpringApplication.run(WarehouseApplication.class, args);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@TableName("merchant")
//@TableName("merchant")
@Table(name="merchant")
public class Merchant implements Serializable {

private static final long serialVersionUID = 234345356533250815L;

@TableId(type = IdType.AUTO)
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
//@TableId(type = IdType.AUTO)
private int id;

@TableField("name")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.yen.springWarehouse.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {

//映射路徑
registry.addMapping("/**")
//允許跨網域請求的來源
.allowedOrigins("/*")
//允許跨域攜帶cookie資訊,預設跨網域請求是不攜帶cookie資訊的。
.allowCredentials(true)
//允許使用那些請求方式
.allowedMethods("GET", "POST", "PUT", "DELETE")
//允許哪些Header
.allowedHeaders("/*")
//可獲取哪些Header(因為跨網域預設不能取得全部Header資訊)
.exposedHeaders("Header1", "Header2");
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.yen.springWarehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.yen.springWarehouse.bean.Merchant;
import com.yen.springWarehouse.bean.ProductType;
import com.yen.springWarehouse.repository.MerchantRepository;
import com.yen.springWarehouse.service.MerchantService;
import com.yen.springWarehouse.util.csvUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -25,9 +31,14 @@
@RequestMapping("/merchant")
public class MerchantController {

private final int PAGINATIONSIZE = 3;

@Autowired
MerchantService merchantService;

@Autowired
MerchantRepository merchantRepository;

@GetMapping("/toInput")
public String input(Map<String, Object> map) {

Expand Down Expand Up @@ -78,30 +89,50 @@ public String createBatch(@RequestParam("file") MultipartFile file, Map<String,


@GetMapping("/list")
public String list(Map<String, Object> map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) {

int pageNo;

// check pageNo
pageNo = Integer.parseInt(pageNoStr);
if(pageNo < 1){
pageNo = 1;
public String list(@RequestParam(value = "pageNum", required = false, defaultValue = "0") int pageNum,
@RequestParam(value="pageSize", defaultValue = "0" + PAGINATIONSIZE) int pageSize,
Map<String, Object> map) {

//pageNum = 2;
log.info(">>> pageNum = {}", pageNum);

// Page<Merchant> page = new Page<>(pageNum, PAGINATIONSIZE);
// QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
// IPage<Merchant> iPage = merchantService.page(page,
// new LambdaQueryWrapper<Merchant>()
// .orderByAsc(Merchant::getId)
// );
// log.info("iPage.total = {}, iPage.getPages = {} iPage = {}", iPage.getTotal(), iPage.getPages(), iPage);
// map.put("page", iPage);


Pageable pageRequest = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC, "id"));
Page<Merchant> merchantsPage = merchantRepository.findAll(pageRequest); //merchantService //postRepository.findAll(pageRequest);
List<Merchant> merchants = merchantsPage.toList();
log.info(">>> merchants length = {}", merchants.toArray().length);
PageInfo<Merchant> pageInfo = null;
//為了程式的嚴謹性,判斷非空:
if(pageNum <= 0){
pageNum = 0;
}
log.info("當前頁是:"+pageNum+"顯示條數是:"+pageSize);
//1.引入分頁外掛,pageNum是第幾頁,pageSize是每頁顯示多少條,預設查詢總數count
PageHelper.startPage(pageNum, pageSize);
//2.緊跟的查詢就是一個分頁查詢-必須緊跟.後面的其他查詢不會被分頁,除非再次呼叫PageHelper.startPage
try {
//Page<Post> postList = postRepository.findAll(pageRequest);//service查詢所有的資料的介面
List<Merchant> merchantList = merchantService.getAllMerchant();//service查詢所有的資料的介面
log.info(">>> 分頁資料:" + merchantList.get(0).toString());
//3.使用PageInfo包裝查詢後的結果,5是連續顯示的條數,結果list型別是Page<E>
pageInfo = new PageInfo<Merchant>(merchantList, pageSize);
//4.使用model/map/modelandview等帶回前端
System.out.println(">>> (merchant) pageInfo = " + pageInfo.getPages());

map.put("pageInfo",pageInfo);
map.put("merchants", merchants);
}finally {
PageHelper.clearPage(); //清理 ThreadLocal 儲存的分頁引數,保證執行緒安全
}

/*
* 1st param:which page
* 2nd param : record count per page
*/
log.info("pageNo = {}", pageNo);
Page<Merchant> page = new Page<>(pageNo,3);
QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
IPage<Merchant> iPage = merchantService.page(page,
new LambdaQueryWrapper<Merchant>()
.orderByAsc(Merchant::getId)
);
log.info("iPage.total = {}, iPage.getPages = {} iPage = {}", iPage.getTotal(), iPage.getPages(), iPage);
map.put("page", iPage);

return "merchant/list_merchant";
}

Expand All @@ -112,7 +143,7 @@ public String remove(@PathVariable("typeId") Integer typeId) {
return "redirect:/merchant/list";
}

@GetMapping(value="/preUpdate/{typeId}")
@GetMapping(value = "/preUpdate/{typeId}")
public String preUpdate(@PathVariable("typeId") Integer typeId, Map<String, Object> map) {

Merchant merchant = merchantService.getById(typeId);
Expand All @@ -121,7 +152,7 @@ public String preUpdate(@PathVariable("typeId") Integer typeId, Map<String, Obje
return "merchant/update_merchant";
}

@PostMapping(value="/update")
@PostMapping(value = "/update")
public String update(Merchant merchant) {

log.info("update merchant type as {}", merchant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@Slf4j
@Controller
@RequestMapping("/product")
//@RestController
public class ProductController {

private int PAGE_SIZE = 3;
Expand Down Expand Up @@ -84,8 +83,6 @@ public String list(@RequestParam(value="pageNo", required=false, defaultValue="1
map.put("helper", helper);
log.info("(ProductController list) map = {}", map);
return "product/list_product";
//return "index";
//return "OK";
}

@PostMapping(value="/remove/{productNo}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
public interface MerchantMapper extends BaseMapper<Merchant> {

List<Merchant> getMerchantList(Page<Merchant> merchantPage, @Param("ew") QueryWrapper<Merchant> wrapper);

List<Merchant> getAllMerchant();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<!-- name space -->
<mapper namespace="com.yen.springWarehouse.mapper.MerchantMapper">

<!-- SQL -->
<!-- SQL query with paging, NOTE : if there is group type in Mybatis, only use element type in group -->
<!-- query with paging, NOTE : if there is group type in Mybatis, only use element type in group -->
<select id="getMerchantList" resultType="com.yen.springWarehouse.bean.Merchant">
select
*
Expand All @@ -17,4 +16,12 @@
</where>
</select>

<!-- List<Merchant> getAllMerchant(); -->
<select id="getAllMerchant" resultType="com.yen.springWarehouse.bean.Merchant">
select
*
from
merchant
</select>

</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.yen.springWarehouse.repository;

import com.yen.springWarehouse.bean.Merchant;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Component;

@Component
public interface MerchantRepository extends PagingAndSortingRepository<Merchant, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import com.yen.springWarehouse.util.MerchantQueryHelper;
import com.yen.springWarehouse.util.ProductQueryHelper;

import java.util.List;

public interface MerchantService extends IService<Merchant> {

Page<Merchant> getMerchantPage(MerchantQueryHelper helper, Integer pageNo, Integer pageSize);

List<Merchant> getAllMerchant();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public Page<Merchant> getMerchantPage(MerchantQueryHelper helper, Integer pageNo
return new Page<>();
}

@Override
public List<Merchant> getAllMerchant() {
return merchantMapper.getAllMerchant();
}

}
2 changes: 1 addition & 1 deletion springWarehouse/src/main/resources/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>
</div>
<div id="footer">
Product System - dev 1.0.0
WareHouse System - dev 1.0.0
</div>
<script type="text/javascript" th:src="@{/js/jquery-3.1.1.min.js}"></script>
<script th:inline="javascript">
Expand Down
Loading