您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“SpringBoot怎么實(shí)現(xiàn)分頁功能”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
本文實(shí)例為大家分享了SpringBoot實(shí)現(xiàn)分頁功能的具體代碼,供大家參考,具體內(nèi)容如下
新建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怎么實(shí)現(xiàn)分頁功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。