Pagehelper分页插件的使用

Pagehelper分页插件的使用

之前都是用的前端分页插件,今天尝试着用用后台的插件,首先想到的就是pagehelper分页插件。当我开始使用的时候就踩坑了。发现这个插件诸多bug啊。不知道是我使用错了还是怎样,接下来就是我pagehelper插件结合前端知识改良版。
下边开始我的表演吧。本项目采用的是ssm框框架。先上项目结构图吧:

  1. 项目结构图
    本项目也是严格遵守mvc三层架构的,哈哈

Pagehelper分页插件的使用_第1张图片

  1. 准备工作(老规矩先导入pom文件)
里面包含了ssm的基本jar包和pagehelper的分页插件。
版本看自己仓库的版本稍作修改。



  4.0.0

  com.cnct
  phelper
  1.0-SNAPSHOT
  war

  phelper Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
      
      
          com.alibaba
          druid
          1.0.20
      
      
        javax.servlet
        servlet-api
        2.5
        provided
      
      
          com.github.pagehelper
          pagehelper
          4.1.6
      
      
        org.springframework
        spring-aop
        4.3.10.RELEASE
      
      
        org.springframework
        spring-aspects
        4.3.10.RELEASE
      
      
        org.springframework
        spring-context-support
        4.3.10.RELEASE
      
      
        org.springframework
        spring-jdbc
        4.3.10.RELEASE
      
      
        org.springframework
        spring-tx
        4.3.10.RELEASE
      
      
        org.springframework
        spring-web
        4.3.10.RELEASE
      
      
        org.springframework
        spring-webmvc
        4.3.10.RELEASE
      

      
        org.mybatis
        mybatis
        3.2.8
      
      
        org.mybatis
        mybatis-spring
        1.3.0
      

      
        mysql
        mysql-connector-java
        5.1.38
      
      
        jstl
        jstl
        1.2
      

      
        commons-dbcp
        commons-dbcp
        1.4
      
      
        commons-collections
        commons-collections
        3.2
      
      
        org.aspectj
        aspectjweaver
        1.6.9
      
      
        aopalliance
        aopalliance
        1.0
      
  

  
    phelper
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

  1. SSM框架的三个配置文件和数据库的配置信息该配置吧。
    3.1.这个啥的配置文件 (applicationContext-dao.xml)



	
	
	
	
	
	
		
		
		
		
		
		
		
		
		
		
	
	
	
	
		
		
	
	
	
	
		
	

  1. 这个啥的配置文件(applicationContext-service.xml)


	
	

  1. SpringMVC配置文件(springmvc.xml)



    
    
    
    
    
    
    

    
        
        
    

  1. 数据库配置文件(db.properties)
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/web?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
jdbc.username=root
jdbc.password=root
 
  1. 分页插件的配置信息以及别名包 (SqlMapConfig.xml)




    
        
    
    
    
        
            
            
        
    


  1. 别忘记 (web.xml)


  maven-ssm

  
    index.jsp
  

  
  
    contextConfigLocation
    classpath:spring/applicationContext-*.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
  
  
    CharacterEncodingFilter
    /*
  

  
  
    maven-ssm
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring/springmvc.xml
    
    1
  

  
  
    maven-ssm
    *.action
  

  1. 日志配置文件看根据自己需求来吧(log4j.properties)
log4j.rootLogger=INFO,console,dailyFile
# TODO \u53d1\u5e03\u5230\u963f\u91cc\u4e91\u8bb0\u5f97\u6dfb\u52a0\uff0c\u53e6\u5916\u63a7\u5236\u53f0\u4e0d\u8f93\u51fa(\u53ea\u8f93\u51fawarn\u6216\u8005error\u4fe1\u606f)
#INFO,console,dailyFile
# log4j.logger.org.mybatis = INFO
log4j.logger.com.sihai.mapper=INFO
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n
# \u5b9a\u671f\u6eda\u52a8\u65e5\u5fd7\u6587\u4ef6\uff0c\u6bcf\u5929\u90fd\u4f1a\u751f\u6210\u65e5\u5fd7
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.encoding=UTF-8
log4j.appender.dailyFile.Threshold=INFO
# TODO \u672c\u5730\u65e5\u5fd7\u5730\u5740\uff0c\u6b63\u5f0f\u73af\u5883\u8bf7\u52a1\u5fc5\u5207\u6362\u4e3a\u963f\u91cc\u4e91\u5730\u5740
log4j.appender.dailyFile.File=C:/logs/maven-ssm-alipay/log.log4j
log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n
  • ----------------------------------开始写代码了 ----------------------------------开始写代码了-------------------------------
  1. controller
package com.cnct.pagehelper.controller;

import com.cnct.pagehelper.model.User;
import com.cnct.pagehelper.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/userList.action")
    public String toIndex(@RequestParam(required = true, defaultValue = "1") Integer page, Model model) {
		//开启分页
        PageHelper.startPage(page, 3);
        //查询所有记录
        List userList = userService.findAll();
        PageInfo p = new PageInfo<>(userList);
        model.addAttribute("page", p);
        model.addAttribute("userList", userList);
        return "index";
    }
}
  1. service这一层就一个接口
package com.cnct.pagehelper.service;

import com.cnct.pagehelper.model.User;

import java.util.List;

public interface UserService {
    List findAll();
}
  1. serviceImpl这就是service的实现类
package com.cnct.pagehelper.service.impl;

import com.cnct.pagehelper.mapper.UserMapper;
import com.cnct.pagehelper.model.User;
import com.cnct.pagehelper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List findAll() {
        return userMapper.findAll();
    }
}
  1. mapper文件
package com.cnct.pagehelper.mapper;

import com.cnct.pagehelper.model.User;

import java.util.List;
public interface UserMapper {
    List findAll();
}
  1. 别忘了很重要的实体类User
package com.cnct.pagehelper.model;

public class User {
    private Integer id;

    private String username;

    private String password;

    public User(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public User() {
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
}
  1. 对应的UserMapper.xml是不是该写呢?
这里用的mybatis的逆向工程生成的代码,所以有点冗余,生成方法参考[上一篇文章](https://blog.csdn.net/li18782540852/article/details/83822714)。



  
    
      
      
      
    
  
  
    id, username, password
  
  
  
  
    delete from user
    where id = #{id,jdbcType=INTEGER}
  
  
    insert into user (id, username, password
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        password,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{password,jdbcType=VARCHAR},
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        password = #{password,jdbcType=VARCHAR},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  

  1. 代码写完了吗?不不。。还没有写页面呢,新建一个index.jsp放在WEB-INF/jsp文件夹下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



        
ID 姓名 密码
${list.id} ${list.username} ${list.password}
<%--之前这里有坑的,分页的边界有问题,就是首页竟然还有上一页,尾页竟然还有下一页。这个坑现在被我填上了,肯定不是最佳的,但是比较容易理解--%>

一共${page.pages}页

第一页 1 ${page.prePage}">上一页 ${page.pages} ${page.nextPage}">下一页 最后页
  1. 写到这儿本想停笔的,但是发现还没贴数据库的SQL代码。来把最后一个。
/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50022
Source Host           : localhost:3306
Source Database       : web

Target Server Type    : MYSQL
Target Server Version : 50022
File Encoding         : 65001

Date: 2018-11-15 16:30:52
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(50) default NULL,
  `password` varchar(50) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'a', 'a');
INSERT INTO `user` VALUES ('2', 'b', 'b');
INSERT INTO `user` VALUES ('3', 'c', 'c');
INSERT INTO `user` VALUES ('4', 'd', 'd');
INSERT INTO `user` VALUES ('5', 'e', 'e');
INSERT INTO `user` VALUES ('6', 'f', 'f');
INSERT INTO `user` VALUES ('7', 'g', 'g');
INSERT INTO `user` VALUES ('8', 'h', 'h');
INSERT INTO `user` VALUES ('9', 'i', 'i');
INSERT INTO `user` VALUES ('10', 'j', 'j');
INSERT INTO `user` VALUES ('11', 'k', 'k');
INSERT INTO `user` VALUES ('12', 'l', 'l');
INSERT INTO `user` VALUES ('13', 'm', 'm');
INSERT INTO `user` VALUES ('14', 'n', 'n');
INSERT INTO `user` VALUES ('15', 'o', 'o');
INSERT INTO `user` VALUES ('16', 'p', 'p');
INSERT INTO `user` VALUES ('17', 'q', 'q');
INSERT INTO `user` VALUES ('18', 'r', 'r');
INSERT INTO `user` VALUES ('19', 's', 's');
INSERT INTO `user` VALUES ('20', 't', 't');
INSERT INTO `user` VALUES ('21', 'u', 'u');
INSERT INTO `user` VALUES ('22', 'v', 'v');
INSERT INTO `user` VALUES ('23', 'w', 'w');
INSERT INTO `user` VALUES ('24', 'x', 'x');
INSERT INTO `user` VALUES ('25', 'y', 'y');
INSERT INTO `user` VALUES ('26', 'z', 'z');

写了这么多了上一个分页的效果图吧,是不是好low,哈哈我也觉得好low,low到爆。不过加一点样式就会好多了,这个由你们去加吧。
Pagehelper分页插件的使用_第2张图片

求打赏,哈哈

写到这儿也学学别人,求点打赏吧。哈哈。刚开始写博客还有待提高。写得不好仅做参考。
Pagehelper分页插件的使用_第3张图片

Pagehelper分页插件的使用_第4张图片

你可能感兴趣的:(java)