SpringBoot实现分页功能
更新时间:2021年08月31日 16:00:47 作者:waillyer
这篇文章主要为大家详细介绍了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; 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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Spring MVC利用Swagger2如何构建动态RESTful API详解
这篇文章主要给大家介绍了关于在Spring MVC中利用Swagger2如何构建动态RESTful API的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。2017-10-10Java获取年月日(格式:xxxx年xx月xx日)的方法详解
在开发应用程序时,经常需要获取当前的年、月、日,并以特定格式进行展示或处理,本文将介绍如何获取年月日,并将其格式化为“xxxx年xx月xx日”的形式,帮助你在应用程序中处理日期信息,需要的朋友可以参考下2023-10-10
最新评论