MyBatis-Plus实现2种分页⽅法(QueryWrapper查询分页
和SQL查询分页)
⽬录
1 MyBatisPlusConfig
2 UserPagination
3 Mapper
3.1 UserMapper.java
3.l
4 Service
4.1 UserService
4.2 UserServiceImpl
5 UserController
6 调试结果
6.1 QueryWrapper查询分页
6.2 SQL查询分页
1 MyBatisPlusConfig
MyBatisPlus配置类。
fig;
import batisplus.annotation.DbType;
import sion.plugins.MybatisPlusInterceptor;
import sion.plugins.inner.PaginationInnerInterceptor;
import t.annotation.*;
/
**
* MyBatisPlus配置类
*/
@Configuration
public class MyBatisPlusConfig {
/**
* MyBatisPlus(⽤于分页)
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
/
/添加MySQL的分页
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
2 UserPagination
⽤户查询条件类。
ity;
import lombok.Data;
/**
* 查询条件
*/
@Data
public class UserPagination {
/**
* 当前页号
*/
private int currentPage;
/**
* 每页显⽰条数
}
3 Mapper
3.1 UserMapper.java
package com.mapper;
import apper.BaseMapper;
import sion.plugins.pagination.Page;
ity.UserEntity;
ity.UserPagination;
import org.apache.ibatis.annotations.Mapper;
/**
* ⽤户信息dao层
*/
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
/**
* 获取⽤户信息(SQL查询分页)
*
* @param page 分页条件
* @return
*/
Page<UserEntity> getUserListBySQLPage(Page<UserEntity> page);
}
3.l
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mapper.UserMapper">
<select id="getUserListBySQLPage" resultType="ity.UserEntity">
SELECT *
from users
</select>
</mapper>
4 Service
4.1 UserService
package com.service;
import sion.plugins.pagination.Page;
import sion.service.IService;
ity.*;
public interface UserService extends IService<UserEntity> {
/**
* 获取⽤户信息(QueryWrapper查询分页)
*
* @param pagination 查询条件
* @return
*/
Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination);
/**
* 获取⽤户信息(SQL查询分页)
*
* @param pagination 查询条件
* @return
*/
Page<UserEntity> getUserListBySQLPage(UserPagination pagination);
}
4.2 UserServiceImpl
package com.service.impl;
import ditions.query.QueryWrapper;
import sion.plugins.pagination.Page;
import sion.service.impl.ServiceImpl;
mybatis和springmvc
ity.*;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {  @Autowired
private UserMapper userMapper;
/**
* 获取⽤户信息(QueryWrapper查询分页)
*
* @param pagination 查询条件
* @return
*/
public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) {
QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
Page<UserEntity> page = new Page<>(CurrentPage(), PageSize());        return this.page(page, queryWrapper);
}
/**
* 获取⽤户信息(SQL查询分页)
*
* @param pagination 查询条件
* @return
*/
@Override
public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) {
Page<UserEntity> page = new Page<>(CurrentPage(), PageSize());        UserListBySQLPage(page);
}
}
5 UserController
调试代码。
ller;
import sion.plugins.pagination.Page;
ity.*;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 获取⽤户信息(QueryWrapper查询分页)
*
* @return
*/
@GetMapping("/getUserListByQueryWrapperPage")
public Page<UserEntity> getUserListByQueryWrapperPage(UserPagination pagination) {
UserListByQueryWrapperPage(pagination);
}
/**
* 获取⽤户信息(SQL查询分页)
*
* @return
*/
@GetMapping("/getUserListBySQLPage")
public Page<UserEntity> getUserListBySQLPage(UserPagination pagination) {
UserListBySQLPage(pagination);
}
}
6 调试结果
6.1 QueryWrapper查询分页
6.2 SQL查询分页
注:
更多MyBatis-Plus的配置请查看以下博客。
到此这篇关于MyBatis-Plus实现2种分页⽅法(QueryWrapper查询分页和SQL查询分页)的⽂章就介绍到这了,更多相关MyBatis-Plus 分页内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。