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);
    }

}

 

posted @ 2021-04-15 17:26  残星  阅读(13292)  评论(0编辑  收藏  举报