ssm分页查询实例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36155000/article/details/101119534

效果图

在这里插入图片描述

项目整体结构

在这里插入图片描述
这个只是一个分页查询实例,不涉及其他复杂业务,所以ApplicationContext-*.xml的几个文件都没有内容

应用实例

Mapper层

TestMapper.java

package com.right.mapper;

import com.right.entity.OrderItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface TestMapper {
     List<OrderItem> selectByPage(HashMap<String,Object> map);
     Integer findTotalCount();
     List<OrderItem> findByPage(Map<String, Object> map);
}
在这里插入代码片

TestMapper.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.right.mapper.TestMapper" >
    <resultMap id="testRM" type="com.right.entity.OrderItem">
        <id property="orderItemId" column="orderItemId"></id>
        <result property="quantity" column="quantity"/>
        <result property="subtotal" column="subtotal"/>
        <result property="bid" column="bid"/>
        <result property="bname" column="bname"/>
        <result property="currPrice" column="currPrice"/>
        <result property="image" column="image_b"/>
        <result property="oid" column="oid"/>
     </resultMap>
     <select id="findTotalCount" resultType="Integer">
          select count(*) from t_orderitem
     </select>

    <select id="findByPage" resultMap="testRM" parameterType="map">
        select * from t_orderitem
        <if test="size!=null and start!=null">
            limit  #{start},#{size};
        </if>
    </select>
</mapper>

Service层

TestService.java

package com.right.service;

import com.right.entity.OrderItem;
import com.right.entity.PageBean;

public interface TestService {
    PageBean<OrderItem> findByPage(Integer currpage);

    Integer findtotal();
}

TestServiceImpl.java

package com.right.service;

import com.right.entity.OrderItem;
import com.right.entity.PageBean;
import com.right.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestServiceImpl implements TestService {


    @Autowired
    TestMapper testMapper;
    @Override
    public PageBean<OrderItem> findByPage(Integer currpage) {
        Map<String,Object> map=new  HashMap<String,Object>();
        PageBean<OrderItem> pageBean=new PageBean<>();
        Integer totalCount = testMapper.findTotalCount();
        Integer pageSize=5;
        pageBean.setCurrPage(currpage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
        double totalPage = Math.ceil(totalCount / pageSize);
        pageBean.setTotalPage((int) totalPage);
        map.put("start",(currpage-1)*pageSize);
        map.put("size",pageSize);
        List<OrderItem> eachPageOrderItemList=testMapper.findByPage(map);
        pageBean.setEachPageInfo(eachPageOrderItemList);
        return pageBean;
    }

    @Override
    public Integer findtotal() {
        return testMapper.findTotalCount();
    }
}

##Controler层
TestController.java

package com.right.controller;

import com.right.entity.OrderItem;
import com.right.entity.PageBean;
import com.right.service.TestService;
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 org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @Autowired
    TestService testService;
    @RequestMapping("/index")
    public String index(){
        System.out.println(testService.findtotal());
        return "index";
    }
    @RequestMapping("/page")
    public String page(@RequestParam("currPage")Integer currpage, Model model){
        PageBean<OrderItem> bean = testService.findByPage(currpage);
        model.addAttribute("bean",bean);
        return  "forward:index";
    }


}

##Entity实例类

package com.right.entity;

import java.math.BigDecimal;

public class OrderItem {

    /**
     * CREATE TABLE `t_orderitem` (
     *   `orderItemId` char(32) NOT NULL,
     *   `quantity` int(11) DEFAULT NULL,
     *   `subtotal` decimal(8,2) DEFAULT NULL,
     *   `bid` char(32) DEFAULT NULL,
     *   `bid` varchar(200) DEFAULT NULL,
     *   `currPrice` decimal(8,2) DEFAULT NULL,
     *   `image_b` varchar(100) DEFAULT NULL,
     *   `oid` char(32) DEFAULT NULL,
     *   PRIMARY KEY (`orderItemId`),
     *   KEY `FK_t_orderitem_t_order` (`oid`),
     *   CONSTRAINT `FK_t_orderitem_t_order` FOREIGN KEY (`oid`) REFERENCES `t_order` (`oid`)
     * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
     */
    private String orderItemId;
    private Integer quantity;
    private Integer subtotal;
    private String bid;
    private String bname;
    private BigDecimal currPrice;
    private String image;
    private String oid;

     ..getter and settter.....
}

PageBean.java

package com.right.entity;

import java.util.List;

public class PageBean<T> {

    private Integer currPage;//当前页数
    private Integer pageSize;//每页显示的记录数
    private Integer totalCount;// zongyeshu
    private List<T> eachPageInfo;//meiye xinxi
    private Integer totalPage;//zongyeshu

   ..gettter and settter
}

##静态资源文件
Springmvc-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <context:component-scan base-package="com.right.controller"/>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"/>
     </bean>

</beans>

ApplicationContext-dao.xml

<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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!--   读取jdbcproperteiswenjain-->
<context:property-placeholder location="classpath:jdbc.properties"/>
 开启包扫描
<context:component-scan base-package="com.right.service"/>
<!--    配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.Driver}"></property>
    <property name="url" value="${jdbc.Url}"></property>
    <property name="username" value="${jdbc.UserName}"></property>
    <property name="password" value="${jdbc.Passwd}"></property>
    <property name="maxActive" value="20"/>
    <property name="minIdle" value="6"/>
</bean>
<!--    配置mybatis工厂-->
<bean  id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation"  value="classpath:SqlMapConfig.xml"></property>
</bean>
<!--    配置 mapper扫描-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.right.mapper"/>
</bean>
</beans>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Mr.Right
  Date: 2019/9/21
  Time: 16:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>showInfo</title>
</head>

<body>


   <table border="1" >
       <tr >
           <td>dorderItemId</td>
           <td>quantity</td>
           <td>subtotal</td>
           <td>bid</td>
           <td>bname</td>
           <td>currprice</td>
           <td>image</td>
           <td>oid</td>
       </tr>
        <c:forEach items="${bean.eachPageInfo}" var="page">
           <tr>
               <td>${page.orderItemId}</td>
            <td>${page.quantity}</td>
            <td>${page.subtotal}</td>
            <td>${page.bid}</td>
            <td>${page.bname}</td>
            <td>${page.currPrice}</td>
            <td>${page.image}</td>
            <td>${page.oid}</td>
           </tr>
        </c:forEach>
   </table>
   <table border="1">
       <tr>
          <c:if test="${bean.currPage>5}">
           <td><a href="${pageContext.request.contextPath}/page?currPage=${bean.currPage-1}">上一页</a></td>
           </c:if>
           <c:forEach begin="${bean.currPage}" end="${bean.currPage+5}" varStatus="status">
               <c:if test="${status.end<bean.totalCount}">
                   <td><a href="${pageContext.request.contextPath}/page?currPage=${bean.currPage}">${status.index}</a></td>
               </c:if>
           </c:forEach>
           <td><a href="${pageContext.request.contextPath}/page?currPage=${bean.currPage+1}">下一页</a></td>
       </tr>

   </table>


</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_36155000/article/details/101119534
今日推荐