SpringBoot实现分页功能

【SpringBoot实现分页功能】本文实例为大家分享了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; Mapperpublic interface UserMapper {public List findUserByName(String userName); public List ListUser(); public List 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
SELECT * FROM user SELECT * FROM userwhere userName like concat(concat('%',#{userName}),'%')order by userId desc select * from userorder by userId desclimit #{startRows},5 select count(*) from user INSERT INTO user(userId,userDate,userName,userAddress)VALUES (#{userId},#{userDate, jdbcType=VARCHAR},#{userName, jdbcType=VARCHAR},#{userAddress, jdbcType=VARCHAR}) delete from user where userId=#{userId} update userset user.userDate=#{userDate},user.userName=#{userName},user.userAddress=#{userAddress}where user.userId=#{userId}

新建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; Servicepublic class UserService {@Autowiredprivate UserMapper userMapper; public List findByName(String userName) {return userMapper.findUserByName(userName); }public List 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 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; RestControllerpublic class UserCtrl {@Autowiredprivate UserService userService; @RequestMapping(value = "https://www.it610.com/delete", method = RequestMethod.POST)public Integer delete(Integer userId) {System.out.println(userId); int result = userService.delete(userId); return result; }@RequestMapping(value = "https://www.it610.com/update", method = RequestMethod.POST)@ResponseBodypublic String update(User user) {int result = userService.Update(user); if (result >= 1) {return "修改成功"; } else {return "修改失败"; }}@RequestMapping(value = "https://www.it610.com/insert", method = RequestMethod.POST)public User insert(User user) {return userService.insertUser(user); }@RequestMapping("/ListUser")@ResponseBodypublic List ListUser() {return userService.ListUser(); }@RequestMapping("/ListByName")@ResponseBodypublic List ListUserByName(String userName) {return userService.findByName(userName); }/*** 分页* @return*/@RequestMapping(value="https://www.it610.com/page")@ResponseBodypublic List page(Integer page){int pageNow = page == null ? 1 : page; int pageSize = 5; int startRows = pageSize*(pageNow-1); List list = userService.queryPage(startRows); return list; }/*** rows* @return*/@RequestMapping(value="https://www.it610.com/rows")@ResponseBodypublic int rows(){return userService.getRowCount(); }}

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

    推荐阅读