java 单机接口限流处理方案
321
2022-10-20
ssm框架+PageHelper插件实现分页查询功能
通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询。
源码:https://gitee.com/smfx1314/pagehelper
看一下项目结构:
首先创建一个maven工程,pom中引入相关jar包
pom.xml中引入page分页的jar包
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
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
PageInfo
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
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
}
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~