Java獲取文章的上一篇/下一篇


思路:每次查詢詳情返回三條數據信息,當前對象,上一條與下一條的Id和標題

代碼實現如下:

1、PreAndNextModel

 1 public class PreAndNextModel {
 2     /**
 3      * 上一篇或者下一篇的id
 4      */
 5     private Integer id;
 6     /**
 7      * 上一篇或者下一篇的標題
 8      */
 9     private String title;
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public void setId(Integer id) {
16         this.id = id;
17     }
18 
19     public String getTitle() {
20         return title;
21     }
22 
23     public void setTitle(String title) {
24         this.title = title;
25     }
26 }

 

2、獲取上一篇

 1 @Override
 2 public PreAndNextModel getPreModel(Integer productId, Integer proCategoryId) {
 3 
 4     IProductDao iProductDao = new ProductDaoImpl();
 5     //new model
 6     PreAndNextModel pre = new PreAndNextModel();
 7 
 8     Integer preId = null;
 9     List<ProductDto> list;
10     //查詢該分類下的集合
11     list = iProductDao.findAllByCategoryId(proCategoryId);
12     //獲取集合長度
13     int count = list.size();
14     //將該分類下的Id放入數組當中
15     int[] intId = new int[count];
16     for (int i = 0; i < count; i++) {
17         intId[i] = list.get(i).getId();
18     }
19     //獲取當前Id的上一個Id下標
20     for (int j = 0; j < count; j++) {
21         if (intId[j] == productId) {
22             if (j != 0) {
23                 preId = intId[j - 1];
24             }
25         }
26     }
27     isPreAndNext(iProductDao, pre, preId);
28     return pre;
29 }

 

3、獲取下一篇

 1 @Override
 2 public PreAndNextModel getNextModel(Integer productId, Integer proCategoryId) {
 3     IProductDao iProductDao = new ProductDaoImpl();
 4     PreAndNextModel next = new PreAndNextModel();
 5 
 6     Integer nextId = null;
 7     List<ProductDto> list;
 8     //查詢該分類下的集合
 9     list = iProductDao.findAllByCategoryId(proCategoryId);
10     //獲取集合長度
11     int count = list.size();
12     //將該分類下的Id放入數組當中
13     int[] intId = new int[count];
14     for (int i = 0; i < count; i++) {
15         intId[i] = list.get(i).getId();
16     }
17     //獲取當前Id的下一個Id下標
18     for (int j = 0; j < count; j++) {
19         if (intId[j] == productId) {
20             //判斷是不是最后一個下標
21             if (j != (count - 1)) {
22                 nextId = intId[j + 1];
23             }
24         }
25     }
26     isPreAndNext(iProductDao, next, nextId);
27     return next;
28 }

 

4、判斷上一篇或者下一篇是否存在

 1 private void isPreAndNext(IProductDao iProductDao, 
 2              PreAndNextModel pre, Integer preId) {
 3     ProductDto productDto;
 4     if (preId == null) {
 5         pre.setId(null);
 6         pre.setTitle("無");
 7     } else {
 8         //將上一頁Id和標題賦值返回
 9         productDto = iProductDao.findById(preId);
10         pre.setId(productDto.getId());
11         pre.setTitle(productDto.getName());
12     }
13 }

 

5、調用返回

1 //上一個
2 PreAndNextModel pre = iProductService.getPreModel(productId, 
3                 productModel.getProCategoryId());
4 //下一個
5 PreAndNextModel next = iProductService.getNextModel(productId, 
6                 productModel.getProCategoryId());

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM