Mybatis Plus中使用LambdaQueryWrapper进行分页以及模糊查询对比传统XML方式进行分页

传统的XML分页以及模糊查询操作

传统的XML方式只能使用limit以及offset进行分页,通过判断name和bindState是否为空,不为空则拼接条件。

java 复制代码
List<SanitationCompanyStaff> getSanitationStaffInfo(@Param("name") String name,
													@Param("bindState") String bindState,
													@Param("current") Long current,
													@Param("size") Long size);
xml 复制代码
<select id="getSanitationStaffInfo" resultType="com.yutu.garden.entity.SanitationCompanyStaff">
    select * from sanitation_company_staff
    where 0 = 0
    <if test="name != null and name != ''">
        and name like concat('%', #{name}, '%')
    </if>
    <if test="bindState != null and bindState != ''">
        and bind_state like concat('%', #{bindState}, '%')
    </if>
    limit 5 offset 0
</select>

Mybatis Plus分页以及模糊查询操作

只需要在Service实现类中直接调用Mybatis Plus的方法即可进行操作。

java 复制代码
/**
 * 获取环卫工列表数据
 * @param name:名字
 * @param bindState:绑定状态
 * @param current:页码
 * @param size:每页大小
 * @return
 */
@Override
public Page<SanitationCompanyStaff> getSanitationStaffInfo(String name,String bindState,Long current,Long size) {
	LambdaQueryWrapper<SanitationCompanyStaff> wrapper = new LambdaQueryWrapper<>();
	//如果前端传的name不为空,则进行like模糊查询
	if (StringUtils.isNotEmpty(name)){
		wrapper.like(SanitationCompanyStaff::getName,name);
	}
	//如果前端传的bindState不为空,则进行eq匹配
	if (StringUtils.isNotEmpty(bindState)){
		wrapper.eq(SanitationCompanyStaff::getBindState,bindState);
	}
	//通过current、size进行分页
	Page<SanitationCompanyStaff> sanitationStaffInfoPage = new Page<>(current,size);
	this.page(sanitationStaffInfoPage,wrapper);
	return sanitationStaffInfoPage;
}

return Page<SanitationCompanyStaff>类型可以得到数据的总数,你也可以通过.getRecords()方式获取List集合

这样子,我们就可以通过Mybatis Plus得到分页数据了!

相关推荐
小坏讲微服务8 小时前
Spring Boot整合Redis注解,实战Redis注解使用
spring boot·redis·分布式·后端·spring cloud·微服务·mybatis
xie_pin_an8 小时前
MyBatis-Plus 实战:MPJLambdaWrapper 多表联查用法全解析
java·spring boot·spring·mybatis
华仔啊17 小时前
MyBatis-Plus 让你开发效率翻倍!新手也能5分钟上手!
java·后端·mybatis
计算机学姐18 小时前
基于SpringBoot的新闻管理系统【协同过滤推荐算法+可视化统计】
java·vue.js·spring boot·后端·spring·mybatis·推荐算法
梓沂19 小时前
MyBatis的默认对象工厂org.apache.ibatis.reflection.factory.ObjectFactory
apache·mybatis
java1234_小锋20 小时前
MyBatis如何处理懒加载和预加载?
java·开发语言·mybatis
星光一影21 小时前
SpringBoot+Vue3无人机AI巡检系统
人工智能·spring boot·websocket·mysql·intellij-idea·mybatis·无人机
秋月的私语1 天前
批量格式化XML与JSON文件小工具
xml·json
小马爱打代码1 天前
MyBatis:性能优化实战 - 从 SQL 优化到索引设计
sql·性能优化·mybatis
烤麻辣烫1 天前
黑马程序员苍穹外卖(新手)Day1
java·数据库·spring boot·学习·mybatis