欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot实现分页功能

程序员文章站 2022-06-15 12:25:33
本文实例为大家分享了springboot实现分页功能的具体代码,供大家参考,具体内容如下新建demo\src\main\java\com\example\demo\mapper\usermapper....

本文实例为大家分享了springboot实现分页功能的具体代码,供大家参考,具体内容如下

新建demo\src\main\java\com\example\demo\mapper\usermapper.java

package com.example.demo.mapper;

import com.example.demo.entity.user;
import org.apache.ibatis.annotations.mapper;

import java.util.list;

mapper
public interface usermapper {

    public list<user> finduserbyname(string username);

    public list<user> listuser();
    public list<user> querypage(integer startrows);
    public int getrowcount();
    public int insertuser(user user);
    public int delete(int userid);
    public int update(user user);
}

新建demo\src\main\resources\mapper\usermapper.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!doctype mapper public
        "-//mybatis.org//dtd com.example.mapper 3.0//en"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.usermapper">

    <resultmap id="result" type="com.example.demo.entity.user">
        <result property="userid" column="userid" />
  <result property="userdate" column="userdate" />
        <result property="username" column="username" />
        <result property="useraddress" column="useraddress"/>
    </resultmap>

    <select id="listuser" resultmap="result">
  select * from user
 </select>

    <select id="finduserbyname" resultmap="result" parametertype="string">
  select * from user
   where username like concat(concat('%',#{username}),'%')
   order by userid desc
 </select>

 <select id="querypage" resultmap="result"  parametertype="integer">
  select * from user
   order by userid desc
   limit #{startrows},5
 </select>

 <select id="getrowcount" resulttype="integer">
  select count(*) from user
 </select>

    <insert id="insertuser" parametertype="com.example.demo.entity.user">
  insert into user
  (
  userid,userdate,username,useraddress
  )
  values (
  #{userid},
  #{userdate, jdbctype=varchar},
  #{username, jdbctype=varchar},
  #{useraddress, jdbctype=varchar}
  )
 </insert>

    <delete id="delete" parametertype="int">
  delete from user where userid=#{userid}
 </delete>

    <update id="update" parametertype="com.example.demo.entity.user">
  update user
   set user.userdate=#{userdate},user.username=#{username},user.useraddress=#{useraddress}
   where user.userid=#{userid}
 </update>
</mapper>

新建demo\src\main\java\com\example\demo\service\userservice.java

package com.example.demo.service;

import com.example.demo.entity.user;
import com.example.demo.mapper.usermapper;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;

import java.util.list;


service
public class userservice {

    @autowired
    private usermapper usermapper;

    public list<user> findbyname(string username) {
        return usermapper.finduserbyname(username);
    }

    public list<user> querypage(integer startrows) {
        return usermapper.querypage(startrows);
    }

    public int getrowcount() {
        return usermapper.getrowcount();
    }

    public user insertuser(user user) {
        usermapper.insertuser(user);
        return user;
    }

    public list<user> listuser(){
        return usermapper.listuser();
    }

    public int update(user user){
        return usermapper.update(user);
    }

    public int delete(int userid){
        return usermapper.delete(userid);
    }

}

新建demo\src\main\java\com\example\demo\controller\userctrl.java

package com.example.demo.controller;

import com.example.demo.entity.user;
import com.example.demo.service.userservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;

import java.util.list;

restcontroller
public class userctrl {

    @autowired
    private userservice userservice;

    @requestmapping(value = "/delete", method = requestmethod.post)
    public integer delete(integer userid) {
        system.out.println(userid);
        int result = userservice.delete(userid);
        return result;
    }

    @requestmapping(value = "/update", method = requestmethod.post)
    @responsebody
    public string update(user user) {
        int result = userservice.update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    @requestmapping(value = "/insert", method = requestmethod.post)
    public user insert(user user) {
        return userservice.insertuser(user);
    }

    @requestmapping("/listuser")
    @responsebody
    public list<user> listuser() {
        return userservice.listuser();
    }

    @requestmapping("/listbyname")
    @responsebody
    public list<user> listuserbyname(string username) {
        return userservice.findbyname(username);
    }

    /**
     * 分页
     * @return
     */
    @requestmapping(value="/page")
    @responsebody
    public list<user> page(integer page){
        int pagenow = page == null ? 1 : page;
        int pagesize = 5;
        int startrows = pagesize*(pagenow-1);
        list<user> list = userservice.querypage(startrows);
        return list;
    }

    /**
     * rows
     * @return
     */
    @requestmapping(value="/rows")
    @responsebody
    public int rows(){
        return userservice.getrowcount();
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: SpringBoot 分页