手动分页
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " cn.mayanan.restapi.mapper.UserMapper" > < resultMap id = " userResultMap" type = " cn.mayanan.restapi.bean.UserBean" > < id column = " id" property = " id" /> < result column = " name" property = " username" /> < result column = " password" property = " password" /> </ resultMap> < select id = " getAllUsers" parameterType = " map" resultMap = " userResultMap" > select id, name, password from userorder by idlimit #{pageSize}offset #{offset}</ select>
</ mapper>
UserMapper.java
java">package cn. mayanan. restapi. mapper ; import org. apache. ibatis. annotations. Mapper ;
import cn. mayanan. restapi. bean. UserBean ;
import org. apache. ibatis. annotations. Param ; import java . util. List ;
import java . util. Map ; @Mapper
public interface UserMapper { List < UserBean > getAllUsers ( Map < String , Object > params) ;
}
UserService.java
java">package cn. mayanan. restapi. service ; import cn. mayanan. restapi. bean. UserBean ; import java . util. List ;
import java . util. Map ; public interface UserService { UserBean queryUserById ( int id) ; List < UserBean > queryAllUser ( Map < String , Object > params) ;
}
UserServiceImpl.java
java">package cn. mayanan. restapi. serviceImpl ; import cn. mayanan. restapi. bean. UserBean ;
import cn. mayanan. restapi. mapper. UserMapper ;
import cn. mayanan. restapi. service. UserService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ; import java . util. List ;
import java . util. Map ; @Service
public class UserServiceImpl implements UserService { private final UserMapper userMapper; @Autowired public UserServiceImpl ( UserMapper userMapper) { this . userMapper = userMapper; } @Override public List < UserBean > queryAllUser ( Map < String , Object > params) { return userMapper. getAllUsers ( params) ; }
}
UserController.java
java">package cn. mayanan. restapi. controller ; import cn. mayanan. restapi. bean. UserBean ;
import cn. mayanan. restapi. service. UserService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. http. ResponseEntity ;
import org. springframework. http. HttpStatus ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. * ; import java . util. HashMap ;
import java . util. List ;
import java . util. Map ;
@RestController
@RequestMapping ( "/api" )
public class MyRestController { private final UserService userService; @Autowired public MyRestController ( UserService userService) { this . userService = userService; } @GetMapping ( "/users" ) public List < UserBean > showUsers ( @RequestParam ( value = "pageSize" , defaultValue = "1" ) int pageSize, @RequestParam ( value = "pageNum" , defaultValue = "1" ) int pageNum) { System . out. println ( "pageSize: " + pageSize + ", pageNum: " + pageNum) ; Map < String , Object > map = new HashMap < > ( ) ; int offset = ( pageNum - 1 ) * pageSize; map. put ( "pageSize" , pageSize) ; map. put ( "offset" , offset) ; return userService. queryAllUser ( map) ; }
}