MyBatisPlus分页查询
2021/4/15 18:28:56
本文主要是介绍MyBatisPlus分页查询,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.example.ep.test.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.ep.test.entity.TUser; import com.example.ep.test.service.ITUserService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; //import 你自己的父类控制器,没有就不用设置!; /** * <p> * 前端控制器 * </p> * * @author wensm * @since 2021-04-15 */ @RestController @RequestMapping("/test/t-user") //public class TUserController extends 你自己的父类控制器,没有就不用设置! { public class TUserController { @Resource ITUserService userService; // public List<TUser> pageListUserByCondition(long current, Long limit, UserQuery userQuery){ public Page<TUser> pageListUserByCondition(long current, Long limit){ //创建Page对象 Page<TUser> userPage = new Page<>(current,limit); //构建条件 QueryWrapper<TUser> wrapper = new QueryWrapper<>(); //直接传wrapper,或不传wrapper传一个实体或Map,wrapper在Service中定义 return userService.page(userPage,wrapper); } public Page<TUser> pageListUserByCondition2(long current, Long limit,Object obj){ //创建Page对象 Page<TUser> userPage = new Page<>(current,limit); return userService.pageListUserByCondition2(userPage,obj); } }
package com.example.ep.test.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.ep.test.entity.TUser; import com.baomidou.mybatisplus.extension.service.IService; /** * <p> * 服务类 * </p> * * @author wensm * @since 2021-04-15 */ public interface ITUserService extends IService<TUser> { Page<TUser> pageListUserByCondition2(Page page, Object obj); }
package com.example.ep.test.service.impl; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.ep.test.entity.TUser; import com.example.ep.test.mapper.TUserMapper; import com.example.ep.test.service.ITUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Map; /** * <p> * 服务实现类 * </p> * * @author jobob * @since 2021-04-15 */ @Service public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService { @Resource TUserMapper userMapper; @Override public Page<TUser> pageListUserByCondition2(Page page, Object obj) { // IPage<Employee> employeeIPage = employeeMapper.pageEmployee2(page, employee); Page<TUser> userPage = userMapper.pageListUserByCondition2(page, obj); // return employeeIPage; return userPage; // return userMapper.selectPageByMap(page, map); } }
package com.example.ep.test.mapper; import java.util.List; import java.util.Map; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.ibatis.annotations.Param; import com.example.ep.test.entity.TUser; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * <p> * Mapper 接口 * </p> * * @author jobob * @since 2021-04-15 */ public interface TUserMapper extends BaseMapper<TUser> { Page<TUser> pageListUserByCondition2(Page page, @Param("query") Object obj); }
<?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="com.example.ep.test.mapper.TUserMapper"> <select id="pageListUserByCondition2" resultType="com.example.ep.test.entity.TUser"> SELECT * FROM t_user <where> <if test="query.username !=null"> and username = #{query.username} </if> </where> </select> </mapper>
一开始没有总数显示出来,就是在配置类中没有指明数据库类型!!!!
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisConfig { /** * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //注释下面的可能出现获取不到总数的效果 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(false); } }
这篇关于MyBatisPlus分页查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器
- 2024-11-26Java云原生资料:新手入门教程与实战指南
- 2024-11-26JAVA云原生资料入门教程
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程