SSM PageInfo和PageHelper实现多表分页,xml配置

发布时间:2024年01月21日

项目结构

在这里插入图片描述

数据库(需要源码加Q:2301748281)

一个物流对应一个地址,一个商品。即一对一

logistics 物流表

在这里插入图片描述

goods_address 商品地址表

在这里插入图片描述

goods 商品表

在这里插入图片描述

依赖

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>

实体类

Logistics

package com.tmg.domain;

import java.time.LocalDateTime;

public class Logistics {
    private Integer logisticsId;
    private Integer goodsId;
    private LocalDateTime logisticsTime;
    private LocalDateTime arriveTime;
    private Integer nummber;
    private Integer outId;
    private Integer inId;

    private Goods goods;

    private Address addressOut;
    private Address addressIn;

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    public Address getAddressOut() {
        return addressOut;
    }

    public void setAddressOut(Address addressOut) {
        this.addressOut = addressOut;
    }

    public Address getAddressIn() {
        return addressIn;
    }

    public void setAddressIn(Address addressIn) {
        this.addressIn = addressIn;
    }

    public Logistics() {
    }

    public Logistics(Integer goodsId, LocalDateTime logisticsTime, LocalDateTime arriveTime, Integer nummber, Integer outId, Integer inId) {
        this.goodsId = goodsId;
        this.logisticsTime = logisticsTime;
        this.arriveTime = arriveTime;
        this.nummber = nummber;
        this.outId = outId;
        this.inId = inId;
    }

    public Integer getLogisticsId() {
        return logisticsId;
    }

    public void setLogisticsId(Integer logisticsId) {
        this.logisticsId = logisticsId;
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public LocalDateTime getLogisticsTime() {
        return logisticsTime;
    }

    public void setLogisticsTime(LocalDateTime logisticsTime) {
        this.logisticsTime = logisticsTime;
    }

    public LocalDateTime getArriveTime() {
        return arriveTime;
    }

    public void setArriveTime(LocalDateTime arriveTime) {
        this.arriveTime = arriveTime;
    }

    public Integer getNummber() {
        return nummber;
    }

    public void setNummber(Integer nummber) {
        this.nummber = nummber;
    }

    public Integer getOutId() {
        return outId;
    }

    public void setOutId(Integer outId) {
        this.outId = outId;
    }

    public Integer getInId() {
        return inId;
    }

    public void setInId(Integer inId) {
        this.inId = inId;
    }

    @Override
    public String toString() {
        return "Logistics{" +
                "logisticsId=" + logisticsId +
                ", goodsId=" + goodsId +
                ", logisticsTime=" + logisticsTime +
                ", arriveTime=" + arriveTime +
                ", nummber=" + nummber +
                ", outId=" + outId +
                ", inId=" + inId +
                '}';
    }
}

Address

package com.tmg.domain;

public class Address {
    private Integer id;
    private String province;
    private String city;
    private String area;

    public Address() {
    }

    public Address(String province, String city, String area) {
        this.province = province;
        this.city = city;
        this.area = area;
    }

    public Integer getId() {
        return id;
    }

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

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", province='" + province + '\'' +
                ", city=" + city +
                ", area=" + area +
                '}';
    }
}

Goods

package com.tmg.domain;

public class Goods {
    private Integer id;
    private String name;
    private String type;
    private String weight;

    public Goods() {
    }

    public Goods(String name, String type, String weight) {
        this.name = name;
        this.type = type;
        this.weight = weight;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", weight='" + weight + '\'' +
                '}';
    }
}

LogisticsServlet 类

package com.tmg.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import com.tmg.domain.Logistics;
import com.tmg.service.LogisticsService;
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 java.util.List;

@Controller
public class LogisticsServlet {

    @Autowired
    LogisticsService logisticsService;
    @RequestMapping("/queryAll")
    public String queryAll(String pageNum, Model model){
    //startPage方法第一个参数是页号,第二个是大小,我写的都是1
        if(StringUtils.isNullOrEmpty(pageNum)){
            PageHelper.startPage(1,1);
        }else {
            int i = Integer.parseInt(pageNum);
            PageHelper.startPage(i,1);
        }
        List<Logistics> logisticsList = logisticsService.findAll();
        PageInfo<Logistics> pageInfo =new PageInfo<>(logisticsList);
        model.addAttribute("logisticsList",logisticsList);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println("---->"+pageInfo.getList());
        System.out.println("---->"+pageInfo);
        return "index";
    }
    @RequestMapping("/")
    public String ss(){
        System.out.println("***");
        return "forward:/queryAll";
    }

}

LogisticsService 接口

package com.tmg.service;

import com.tmg.domain.Logistics;

import java.util.List;

public interface LogisticsService {
    List<Logistics> findAll();
}

LogisticsServiceImpl 实现类

package com.tmg.service.impl;

import com.tmg.dao.LogMapper;
import com.tmg.domain.Logistics;
import com.tmg.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class LogisticsServiceImpl implements LogisticsService {
    @Autowired
    LogMapper logMapper;
    @Override
    public List<Logistics> findAll() {
        return logMapper.findAll();
    }
}

LogMapper 接口

package com.tmg.dao;

import com.tmg.domain.Logistics;

import java.util.List;

public interface LogMapper{

    List<Logistics> findAll();
}

LogisticsDao.xml 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tmg.dao.LogMapper">

    <resultMap id="logisticsMap" type="Logistics">
        <id property="logisticsId" column="logistics_id"></id>
        <result property="goodsId" column="goods_id"></result>
        <result property="logisticsTime" column="logistics_time"></result>
        <result property="arriveTime" column="arrive_time"></result>
        <result property="nummber" column="nummber"></result>
        <result property="outId" column="out_id"></result>
        <result property="inId" column="in_id"></result>
        <association property="goods" javaType="com.tmg.domain.Goods" column="goods_id">
            <id property="id" column="goods_id"></id>
            <result property="name" column="goods_name"></result>
            <result property="type" column="goods_type"></result>
            <result property="weight" column="goods_weight"></result>
        </association>
        <association property="addressOut" javaType="com.tmg.domain.Address" column="out_id">
            <id property="id" column="add_id"></id>
            <result property="province" column="add_province"></result>
            <result property="city" column="add_city"></result>
            <result property="area" column="add_area"></result>
        </association>
        <association property="addressIn" javaType="com.tmg.domain.Address" column="in_id">
            <id property="id" column="add_id1"></id>
            <result property="province" column="add_province1"></result>
            <result property="city" column="add_city1"></result>
            <result property="area" column="add_area1"></result>
        </association>

    </resultMap>
    <select id="findAll" resultMap="logisticsMap">
    select l.*,g.*,a1.add_id add_id,a1.add_province add_province,a1.add_city add_city,a1.add_area add_area,a2.add_id add_id1,a2.add_province add_province1,a2.add_city add_city1,a2.add_area add_area1 from logistics l join goods g on g.goods_id=l.goods_id join goods_address a1 on a1.add_id=l.out_id join goods_address a2 on a2.add_id=l.in_id
    </select>

</mapper>

mybatis-config.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>
    <!--全局配置-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
<!--    自定义类型别名包-->
<!--    <typeAliases>-->
<!--        <typeAlias type="com.tmg.domain.Type" alias="type"></typeAlias>-->
<!--    </typeAliases>-->

<!--    配置分页助手插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:conext="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">



<!--    <conext:component-scan base-package="com.tmg"></conext:component-scan>-->
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!--配置会话工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--设置数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置类型别名包 映射文件中的类名可以省略包名-->
        <property name="typeAliasesPackage" value="com.tmg.domain"></property>
        <!--mybatis全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--映射文件的位置-->
        <property name="mapperLocations" value="classpath:dao/*.xml"></property>

    </bean>

    <!--配置映射接口的扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--映射接口所在的包-->
        <property name="basePackage" value="com.tmg.dao"></property>
        <!--会话工厂名字配置-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--包的扫描-->
    <context:component-scan base-package="com.tmg"></context:component-scan>

    <!--配置视图解析器 index ===> /WEB-INF/pages/index.jsp -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀 存放页面目录的相对路径-->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!--配置后缀名-->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--配置静态资源的处理器-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--配置注解驱动-->
    <mvc:annotation-driven ></mvc:annotation-driven>


</beans>

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--导入mvc和mybatis的配置-->
    <import resource="classpath:spring-mvc.xml"></import>
    <import resource="classpath:spring-mybatis.xml"></import>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
  <display-name>Archetype Created Web Application</display-name>

  <!--配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--配置文件的位置-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value>
    </init-param>
    <!--配置启动顺序 负数或不配置表示当servlet第一次被访问时创建 0或正数表示服务器启动后自动创建 值越小越早启动-->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!--控制器映射 /代表处理所有请求-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--编码过滤器-->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

index.jsp


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<style type="text/css">
    .one{
        width: 50px;
        height: 50px;
        display: inline-block;
        background-color: aqua;
        text-align: center;
        line-height: 50px;
        font-size: 20px;

    }
    .active{
        background-color: red;
    }
    a{
        text-decoration: none;
        list-style: none;
    }
</style>
<body>
<table border="1px" cellpadding="10px" cellspacing="0px">
    <tr>
        <th>编号</th>
        <th>货物名称</th>
        <th>发货时间</th>
        <th>到货时间</th>
        <th>数量</th>
        <th>发货地址</th>
        <th>到货地址</th>
        <th></th>
    </tr>

    <c:forEach items="${logisticsList}" var="logistics">
        <tr>
            <td>${logistics.logisticsId}</td>
            <td>${logistics.goods.name}</td>
            <td>${logistics.logisticsTime}</td>
            <td>${logistics.arriveTime}</td>
            <td>${logistics.nummber}</td>
            <td>${logistics.addressOut.province}${logistics.addressOut.city}${logistics.addressOut.area}</td>
            <td>${logistics.addressIn.province}${logistics.addressIn.city}${logistics.addressIn.area}</td>
            <td><a>修改</a>|<a>删除</a></td>
        </tr>
    </c:forEach>
<%--    pageInfo--%>
</table>
<c:forEach var="i" items="${pageInfo.navigatepageNums}" begin="0" end="${pageInfo.pages}" step="1">
    <div class="one ${pageInfo.pageNum==i?'active':''}">
        <a href="/queryAll?pageNum=${i}">${i}</a>
    </div>
</c:forEach>
</body>
</html>

运行结果—>自己配置tomcat运行

数据太少,就一条数据为一页,要美观可自行修改
在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_57689217/article/details/135730345
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。