MybatisPlus实现模糊分页查询

本文介绍了如何在MyBatisPlus中设置模糊查询条件,配合分页功能,实现在Java项目中高效检索Complain实体。通过示例展示了如何使用`@Param`注解和动态SQL进行条件筛选,包括字符串匹配和日期范围查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据需要设置模糊查询条件
在这里插入图片描述

上图对应的是mapper类,代码如下

public Page<Complain> selectAllByCode(@Param("page") IPage<Complain> page,@Param("c_comp_code")String c_comp_code,@Param("c_comp_person")String c_comp_person,@Param("c_comp_name")String c_comp_name,@Param("c_comp_message")String c_comp_message,@Param("c_person_type")String c_person_type,@Param("c_begin_date")String c_begin_date,@Param("c_end_date")String c_end_date);

mapper实现类

<?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.mybatisplus.mapper.ComplainMapper">

    <select id="selectAllByCode" resultType="com.mybatisplus.entity.Complain" parameterType="map">
        select * from tb_complain
        <where>
            <if test="c_comp_code!=null and c_comp_code!=''">
                <bind name="c_comp_codeBind" value="'%'+c_comp_code+'%'" />
                <![CDATA[ and c_comp_code like #{c_comp_codeBind} ]]>
            </if>
            <if test="c_comp_person!=null and c_comp_person!=''">
                <bind name="c_comp_personBind" value="'%'+c_comp_person+'%'" />
                <![CDATA[ and c_comp_person like #{c_comp_personBind} ]]>
            </if>
            <if test="c_comp_name!=null and c_comp_name!=''">
                <![CDATA[ and c_comp_name = #{c_comp_name} ]]>
            </if>
            <if test="c_comp_message!=null and c_comp_message!=''">
                <bind name="c_comp_messageBind" value="'%'+c_comp_message+'%'" />
                <![CDATA[ and c_comp_message like #{c_comp_messageBind} ]]>
            </if>
            <if test="c_person_type!=null and c_person_type!=''">
                <![CDATA[ and c_person_type = #{c_person_type} ]]>
            </if>
            <if test="c_begin_date!=null and c_begin_date!='' and c_end_date!=null and c_end_date!=''">
                <![CDATA[
            and c_comp_date between #{c_begin_date} and #{c_end_date}]]>
            </if>
            and c_comp_state = '1'
        </where>
    </select>

</mapper>

其中模糊查询一般都是伴随的是分页查询,利用mybatisPlus自带的类可以实现分页功能

		Page<Complain> userPage = new Page<>();
		// 设置分页的大小
        userPage.setSize(input.getPageSize());
        // 设置当前页
        userPage.setCurrent(input.getPageNo());
        // 这里调用的mapper和上面是对应的,直接使用就行,需要注意的是返回的类要对应,不然会出现查到的数据和返回的不对应
        Page<Complain> complainPage = complainMapper.selectAllByCode(userPage, input.getC_comp_code(),input.getC_comp_person(), input.getC_comp_name(), input.getC_comp_message(), input.getC_person_type(), input.getBegin_date(), input.getEnd_date());
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值