服务器之家

服务器之家 > 正文

SpringBoot实现分页功能

时间:2021-12-11 18:51     来源/作者:waillyer

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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();
    }
}

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

原文链接:https://blog.csdn.net/waillyer/article/details/110421383

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部