javaee ssm框架项目添加分页控件

搭建ssm框架项目

参考上一篇博文

添加分页控件

引入依赖

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>testSSM2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>testSSM2 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 1.导入Spring相关的jar包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- 导入mybatis的jar包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.37</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!-- 配置日志信息-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!-- spring 整合 mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <!-- 4.导入springMVC需要的jar包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- 5.导入jstl jar包-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- 6.加入分页 需要的jar包-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>

    <!-- 7.导入jsp和servlet -->
    <!-- 配置servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!--配置jsp的依赖 -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <!-- 将Mapper的映射文件拷贝出来 -->
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
    <finalName>testSSM2</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

修改mybatis配置文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--配置db.properties  转移到spring中-->
    <!--<properties resource="db.properties" />-->
    <!-- 配置日志管理 -->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!-- 设置别名-->
    <typeAliases>
        <package name="com.test.pojo" />
    </typeAliases>

    <!-- 转移到spring配置文件中 -->
    <!--<environments default="development">-->
        <!--<environment id="development">-->
            <!--<transactionManager type="JDBC"/>-->
            <!--<dataSource type="POOLED">-->
                <!--<property name="driver" value="${driverClass}"/>-->
                <!--<property name="url" value="${url}"/>-->
                <!--<property name="username" value="${user}"/>-->
                <!--<property name="password" value="${password}"/>-->
            <!--</dataSource>-->
        <!--</environment>-->
    <!--</environments>-->

     <!--注册mapper文件 转移到spring中-->
    <!--<mappers>-->
       <!--<package name="com.test.mapper" />-->
    <!--</mappers>-->

    <!-- 3.添加分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor" />
    </plugins>

</configuration>   

测试

java 复制代码
package com.test.controller;

import com.github.pagehelper.PageInfo;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private IItemsService itemsService;

    public IItemsService getItemsService() {
        return itemsService;
    }

    public void setItemsService(IItemsService itemsService) {
        this.itemsService = itemsService;
    }

    @RequestMapping("/selectAllItems")
    public ModelAndView selectAllItems()
    {

        List<Items> itemsList= itemsService.selectItems();

        // System.out.println(itemsList);

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("itemsList",itemsList);

        modelAndView.setViewName("showItems");

        return modelAndView;
    }

    @RequestMapping("/selectAllItemsByPage")
    public ModelAndView selectAllItemsByPage(@RequestParam(value ="pageindex",defaultValue = "1") int pageindex)
    {
        PageInfo<Items> pageInfo= itemsService.selectItemsByPage(pageindex,2);

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.addObject("pageInfo",pageInfo);

        modelAndView.setViewName("showItemsByPage");

        return modelAndView;
    }
}
java 复制代码
package com.test.service;

import com.github.pagehelper.PageInfo;
import com.test.pojo.Items;

import java.util.List;

public interface IItemsService {

    public List<Items> selectItems();

    public PageInfo<Items> selectItemsByPage(int pageindex, int pagesize);

}
java 复制代码
package com.test.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemsService implements IItemsService {

    @Autowired
    private ItemsMapper itemsMapper;

    public ItemsMapper getItemsMapper() {
        return itemsMapper;
    }

    public void setItemsMapper(ItemsMapper itemsMapper) {
        this.itemsMapper = itemsMapper;
    }

    //查询所有
    @Override
    public List<Items> selectItems() {

        return itemsMapper.selectItems();
    }

    //分页查询
    public PageInfo<Items> selectItemsByPage(int pageindex,int pagesize)
    {
        //1.开启分页
        PageHelper.startPage(pageindex,pagesize);
        //2.查询
        List<Items> itemsList= itemsMapper.selectItems();
        //3.组装PageInfo对象 返回
        PageInfo<Items> pageInfo=new PageInfo<Items>(itemsList);

        return pageInfo;
    }
}
html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String baseUrl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();

   pageContext.setAttribute("baseUrl",baseUrl);
%>
<html>
<head >
    <base href="${baseUrl}">
    <title>Title</title>
</head>
<body>
 <c:forEach var="items" items="${pageInfo.list}">
       ${items.name}<br/>
 </c:forEach>

<c:forEach var="i" begin="1" end="${pageInfo.pages}">
   <a href="${baseUrl}/items/selectAllItemsByPage?pageindex=${i}">${i}</a>
</c:forEach>
</body>
</html>
相关推荐
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien1 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋32 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader2 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭3 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪3 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0073 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生3 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss3 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm
一棵星3 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言