SpringBoot实现分页功能

网友投稿 282 2022-10-01


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 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

"-//mybatis.org//DTD com.example.Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT * FROM user

SELECT * FROM user

where userName like concat(concat('%',#{userName}),'%')

order by userId desc

select * from user

order by userId desc

limit #{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 user

set 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;

Service

public class UserService {

@Autowired

private 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;

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 ListUser() {

return userService.ListUser();

}

@RequestMapping("/ListByName")

@ResponseBody

public List ListUserByName(String userName) {

return userService.findByName(userName);

}

/**

* 分页

* @return

*/

@RequestMapping(value="/page")

@ResponseBody

public 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="/rows")

@ResponseBody

swfdSj public int rows(){

return userService.getRowCount();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:服务器运维环境安全体系(下篇)(服务器安全管理)
下一篇:零基础学网络:网闸(GAP)调试实战
相关文章

 发表评论

暂时没有评论,来抢沙发吧~