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

javaee ssm框架项目添加分页控件_第1张图片

搭建ssm框架项目

参考上一篇博文

添加分页控件

引入依赖



<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.0modelVersion>

  <groupId>org.examplegroupId>
  <artifactId>testSSM2artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>testSSM2 Maven Webappname>
  
  <url>http://www.example.comurl>

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

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.37version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.6version>
    dependency>
    <dependency>
      <groupId>com.mchangegroupId>
      <artifactId>c3p0artifactId>
      <version>0.9.5.2version>
    dependency>

    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.17version>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.0version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>

    
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>5.1.2version>
    dependency>

    
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.2version>
      <scope>providedscope>
    dependency>
  dependencies>

  <build>
    <resources>
      <resource>
        
        <directory>src/main/javadirectory>
        <includes>
          <include>**/*.xmlinclude>
        includes>
        <filtering>truefiltering>
      resource>
    resources>
    <finalName>testSSM2finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

修改mybatis配置文件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    
    
    
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>

    
    <typeAliases>
        <package name="com.test.pojo" />
    typeAliases>

    
    
        
            
            
                
                
                
                
            
        
    

     
    
       
    

    
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor" />
    plugins>

configuration>   

测试

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;
    }
}

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);

}

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;
    }
}


<%@ 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>Titletitle>
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>

你可能感兴趣的:(Mac开发,java-ee,java)