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>
相关推荐
minos.cpp18 分钟前
《Java并发编程的艺术》笔记 —— 第一章
java·笔记·idea
程序猿进阶21 分钟前
系统上云-流量分析和链路分析
java·后端·阿里云·面试·性能优化·系统架构·云计算
爱吃烤鸡翅的酸菜鱼1 小时前
MySQL初学之旅(1)配置与基础操作
java·数据库·mysql·database
shawya_void1 小时前
java网络编程
java
hummhumm4 小时前
Oracle 第22章:数据仓库与OLAP
java·javascript·后端·python·sql·mysql·database
说私域6 小时前
全渠道供应链打造中企业定制开发2+1链动模式S2B2C商城小程序的策略与影响
java·大数据·小程序
JingHongB6 小时前
Spring Data Redis的基本使用
java·redis·spring
开心工作室_kaic6 小时前
ssm042在线云音乐系统的设计与实现+jsp(论文+源码)_kaic
java·开发语言·php
菠萝咕噜肉i6 小时前
SpringBoot+Shiro权限管理
java·spring boot·后端·shiro
一 乐7 小时前
家常菜点餐|基于java和小程序的家庭大厨家常菜点餐系统设计与实现(源码+数据库+文档)
java·数据库·小程序·家庭点餐小程序·家庭家常菜点餐