ssm框架+PageHelper插件实现分页查询功能

网友投稿 321 2022-10-20


ssm框架+PageHelper插件实现分页查询功能

通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询。

源码:https://gitee.com/smfx1314/pagehelper

看一下项目结构:

首先创建一个maven工程,pom中引入相关jar包

org.springframework

spring-core

4.3.2.RELEASE

org.springframework

spring-aop

4.3.2.RELEASE

org.springframework

spring-aspects

4.3.2.RELEASE

org.springframework

spring-beans

4.3.2.RELEASE

org.springframework

spring-context

4.3.2.RELEASE

org.springframework

spring-expression

4.3.2.RELEASE

org.springframework

spring-jdbc

4.3.2.RELEASE

org.springframework

spring-test

4.3.2.RELEASE

org.springframework

spring-tx

4.3.2.RELEASE

org.springframework

spring-web

4.3.2.RELEASE

org.springframework

spring-webmvc

4.3.2.RELEASE

org.mybatis

mybatis-spring

1.3.0

org.mybatis

mybatis

3.4.4

commons-io

commons-io

2.4

org.apache.commons

commons-lang3

3.4

commons-logging

commons-logging

1.1.1

org.apache.logging.log4j

log4j-core

2.9.1

org.slf4j

slf4j-log4j12

1.7.21

test

aopalliance

aopalliance

1.0

mysql

mysql-connector-java

5.1.38

com.mchange

c3p0

0.9.5.2

javax.servlet

jstl

1.2

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet.jsp

javax.servlet.jsp-api

2.2.1

provided

taglibs

standard

1.1.2

com.github.pagehelper

pagehelper

5.1.2

org.apache.maven.plugins

maven-compiler-plugin

3.6.1

utf-8

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

UTF-8

pom.xml中引入page分页的jar包

com.github.pagehelper

pagehelper

5.1.2

jar包这里就引入完了。下面引入配置文件

applicationContext.xml

xmlnhttp://s:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

xmlnhttp://s:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

springmvc.xml

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd">

mybatis-config.xml

"http://mybatis.org/dtd/mybatis-3-config.dtd">

以上你也可以直接配置的applicationContext中。

web.xml

pagehelper

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

springmvc

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

controller:

package com.jiangfx.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import com.jiangfx.entity.User;

import com.jiangfx.service.UserService;

@Controller

public class UserController {

@Autowired

private UserService pageService;

/**

* 分页查询

*/

@RequestMapping(value="/list",method=RequestMethod.GET)

public String pageList(ModelMap map,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){

Integer pageSize=4;//每页显示记录数

//分页查询

PageHelper.startPage(pageNo, pageSize);

List userList = pageService.list();//获取所有用户信息

PageInfo pageInfo=new PageInfo(userList);

map.addAttribute("pageInfo", pageInfo);

return "list";

}

}

serviceImpl:接口自己定义,这里就不贴了

package com.jiangfx.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.jiangfx.entity.User;

import com.jiangfx.mapper.UserMapper;

import com.jiangfx.service.UserService;

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserMapper userMapper;

/**

* 查询所有用户

* @return

*/

@Override

public List list() {

return userMapper.getAllUser();

}

}

entity:

package com.jiangfx.entity;

public class User {

private Integer id;

private String username;

private String sex;

private String city;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

@Override

public String toString() {

return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", city=" + city + "]";

}

}

mapper

package com.jiangfx.mapper;

import java.util.List;

import com.jiangfx.entity.User;

public interface UserMapper {

//查询所有用户

List getAllUser();

}

mapper.xml

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from user

下面是jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

分页查询

返回list

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

当前 ${pageInfo.pageNum }页,总${pageInfo.pages }

页,总 ${pageInfo.total } 条记录

第一页

上一页

下一页

最后页

访问localhost:8080/pagehelper

点击分页查询结果:

以上就是ssm框架+PageHelper实现分页查询功能的详细内容,更多关于ssm PageHelper分页查询的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:什么是电视光端机?电视光端机是怎么分类的?
下一篇:如何解决光端机的开关量问题?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~