使用饿汉式实现基于缓存的数据分页查询

2021-12-07 12:49:49 浏览数 (1)

优点:基于缓存查的更快,无需次次访问数据库,查询开销低。

缺点:当多个用户使用此方法进行条件查询时,查询的结果会冲突,但如果只是分页查看所有数据,并不会冲突,同时牺牲了空间,占用了内存,当数据量过大时不建议使用。

用到的实体类:

代码语言:javascript复制
import lombok.Data;

@Data
public class UserInformation {

    private Consumer consumer;
    private User user;

}
复制代码
代码语言:javascript复制
import lombok.Data;
 
@Data
public class Consumer {
 
    private int id;
    private String userName;
    private String userCode;
    private String level;
    private int creditworthiness;
    private double totalIncome;
    private String identityCard;
    private String contact;
    private String address;
    private String authenticationInformation;
    private String cooperationWay;
    private String bankCard;
    private String date;
 
}
复制代码
代码语言:javascript复制
import lombok.Data;
 
@Data
public class User {
 
    private int id;
    private String userCode;
    private String passWord;
    private String registerCode;
    private String admin;
    private int userRole;
    private String state;
 
}
复制代码

分页实现:

代码语言:javascript复制
import com.management.novel.pojo.UserInformation;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
 
@Data
public class UserPageInfo {
 
    private static UserPageInfo userPageInfo = new UserPageInfo();
    private int pageNum;
    private int pages;
    private int total;
    private int firstPage=1;
    private int prePage;
    private int nextPage;
    private int lastPage;
    private int page;
    private List<UserInformation> list;
 
 
    private UserPageInfo(){
 
    }
 
    public static UserPageInfo getUserPageInfo(){
        return userPageInfo;
    }
 
    public void setPageInfo(int pageNum, List<UserInformation> list) {
        this.pageNum = pageNum;
        this.list = list;
        this.total = list.size();
        if(this.total % this.pageNum > 0){
            this.pages = this.total/this.pageNum   1;
        }else {
            this.pages = this.total/this.pageNum;
        }
        this.lastPage = this.pages;
    }
 
    public List<UserInformation> getPageInfo(int page){
        this.page=page;
        this.prePage=this.page-1;
        this.nextPage=this.page 1;
        if (this.prePage<=0)     this.prePage=this.page;
        if (this.nextPage>pages)    this.nextPage=this.page;
 
        List<UserInformation> results = new ArrayList<>();
 
        for (int i =  (this.page-1) * this.pageNum; i < this.page * this.pageNum && i < this.total; i  ) {
            results.add(this.list.get(i));
        }
 
        return results;
    }
 
}
复制代码

前端:

代码语言:javascript复制
<div class="row">
    <!--分页文字信息  -->
    <div class="col-md-6" id="page_info_area">
         当前<span th:text="${pageInfo.page}"></span>页,
         总<span th:text="${pageInfo.pages}"></span>页,
         总<span th:text="${pageInfo.total}"></span>条记录
    </div>
    <!-- 分页条信息 -->
    <div class="col-md-6" id="page_nav_area">
         <ul class="pagination">
         <li><a th:href="'/admin/search/' 
                          ${keyTO} '/' 
                          ${cooperationWaySelectTO} '/' 
                          ${levelSelectTO} '/' 
                          ${creditworthinessSelectTO} '/' 
                          ${consumerStateSelectTO} 
                          '?pn=' ${pageInfo.firstPage}">首页</a></li>
         <li><a th:href="'/admin/search/' 
                          ${keyTO} '/' 
                          ${cooperationWaySelectTO} '/' 
                          ${levelSelectTO} '/' 
                          ${creditworthinessSelectTO} '/' 
                          ${consumerStateSelectTO} 
                          '?pn=' ${pageInfo.prePage}">&laquo;</a></li>
         <li><a th:href="'/admin/search/' 
                          ${keyTO} '/' 
                          ${cooperationWaySelectTO} '/' 
                          ${levelSelectTO} '/' 
                          ${creditworthinessSelectTO} '/' 
                          ${consumerStateSelectTO} 
                          '?pn=' ${pageInfo.page}" th:text="${pageInfo.page}"> </a></li>
         <li><a th:href="'/admin/search/' 
                          ${keyTO} '/' 
                          ${cooperationWaySelectTO} '/' 
                          ${levelSelectTO} '/' 
                          ${creditworthinessSelectTO} '/' 
                          ${consumerStateSelectTO} 
                          '?pn=' ${pageInfo.nextPage}">&raquo;</a></li>
         <li><a th:href="'/admin/search/' 
                          ${keyTO} '/' 
                          ${cooperationWaySelectTO} '/' 
                          ${levelSelectTO} '/' 
                          ${creditworthinessSelectTO} '/' 
                          ${consumerStateSelectTO} 
                          '?pn=' ${pageInfo.lastPage}">末页</a></li>
        </ul>
    </div>
</div>
复制代码

后端:

代码语言:javascript复制
    @RequestMapping("/users")
    public String getUsers( Model model,@RequestParam(value = "pn", defaultValue = "1")Integer pn){
 
        UserPageInfo page = UserPageInfo.getUserPageInfo();
        if(pn==1){
            List<UserInformation> list = userInformationService.getAllUserInformation();
            page.setPageInfo(10,list);
        }
        List<UserInformation> users = page.getPageInfo(pn);
 
        List<Select> cooperationWaySelect = selectService.getCooperationWaySelect();
        List<Select> levelSelect = selectService.getLevelSelect();
        List<Select> creditworthinessSelect = selectService.getCreditworthinessSelect();
        List<Select> consumerStateSelect = selectService.getConsumerStateSelect();
 
        model.addAttribute("cooperationWaySelects",cooperationWaySelect);
        model.addAttribute("levelSelects",levelSelect);
        model.addAttribute("creditworthinessSelects",creditworthinessSelect);
        model.addAttribute("consumerStateSelects",consumerStateSelect);
        model.addAttribute("pageInfo",page);
        model.addAttribute("users",users);
 
        return "admin-user-management";
    }

0 人点赞