zoukankan      html  css  js  c++  java
  • mybatis-plus 高级搜索分页功能的实现 sql语句 QueryWrapper 条件判断

    mybatis-plus分页功能的实现
    创建一个mybatisplus的项目。

    配置类mybatisConfig.java 开启。

    写service层的方法。

    mybatisConfig.java
    package com.ppl.mybatispage.config;

    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;

    /**
     * @author ppliang
     * @date 2020/12/24 19:57
     */
    @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);
        }

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    EmployeeServiceImpl.java
    package com.ppl.mybatispage.service.impl;

    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.ppl.mybatispage.dao.EmployeeMapper;
    import com.ppl.mybatispage.entity.Employee;
    import com.ppl.mybatispage.service.EmployeeService;
    import org.springframework.stereotype.Service;

    import javax.annotation.Resource;

    /**
     * @author ppliang
     * @date 2020/12/24 19:51
     */
    @Service
    public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService {

        @Resource
        private EmployeeMapper employeeMapper;

        /**
         * 分页自带包装类
         *   QueryWrapper 进行条件判断
         * @param page
         * @param employee
         * @return
         */
        @Override
        public IPage<Employee> pageEmployee(IPage<Employee> page, Employee employee) {
            // https://baomidou.com/guide/wrapper.html
            // Page<Employee> page=new Page<>(pageNum,pageSize); 设置页数
            QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
            //设置查询性别
            employeeQueryWrapper.eq("gender", employee.getGender());
            IPage<Employee> employeeIPage = employeeMapper.selectPage(page, employeeQueryWrapper);
            return employeeIPage;
        }

        /**
         * 分页自带包装类
         *   QueryWrapper 自定义SQL语句
         * @param page
         * @param employee
         * @return
         */
        @Override
        public IPage<Employee> pageEmployee2(IPage<Employee> page,Employee employee) {
            IPage<Employee> employeeIPage = employeeMapper.pageEmployee2(page, employee);
            return employeeIPage;
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    EmployeeMapper.java
    /**
     * @author ppliang
     * @date 2020/12/24 19:50
     */
    public interface EmployeeMapper extends BaseMapper<Employee> {

        IPage<Employee> pageEmployee2(IPage<Employee> page, Employee employee);
    }
    1
    2
    3
    4
    5
    6
    7
    8
    EmployeeMapper.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="com.ppl.mybatispage.dao.EmployeeMapper">

         <select id="pageEmployee2" resultType="com.ppl.mybatispage.entity.Employee">
            SELECT *
            FROM t_employee
            <where>
                <if test="param2.gender !=null and param2.gender != '' ">
                   and  gender = #{param2.gender}
                </if>
            </where>
        </select>
    </mapper>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    一开始没有总数显示出来,就是在配置类中没有指明数据库类型!!!!

    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    ————————————————
    版权声明:本文为CSDN博主「黄二马」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/xiaoliang98/article/details/111657612

  • 相关阅读:
    Spring AOP 代理创建方式
    VS全局搜索
    uni-app中添加AES加密
    中国菜刀"连接被重置"问题解决
    Windows go get下载包失败问题解决
    搭建vulhub 漏洞环境及安装Docker和docker-compose
    DedeCMS 5.7通用重装漏洞分析
    各就各位,预备,GO!
    ARL资产灯塔系统分组资产导出脚本
    玩《巫师3》需要了解的背景知识
  • 原文地址:https://www.cnblogs.com/csjoz/p/15305429.html
Copyright © 2011-2022 走看看