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得到分页数据了!

相关推荐
小江的记录本1 天前
【Spring Boot—— .yml(YAML)】Spring Boot中.yml文件的基础语法、高级特性、实践技巧
xml·java·spring boot·后端·spring·spring cloud·架构
zzb15801 天前
RAG from Scratch-优化-routing
java·前端·网络·人工智能·后端·python·mybatis
MegaDataFlowers1 天前
使用注解开发
mybatis
常利兵1 天前
Spring Boot + MyBatis,给数据穿上“隐形盔甲”
java·spring boot·mybatis
Java程序之猿2 天前
SpringBoot + camel+IBM MQ实现消息队列处理
java·spring boot·mybatis
青槿吖2 天前
第二篇:告别XML臃肿配置!Spring注解式IOC/DI保姆级教程,从入门到真香
xml·java·开发语言·数据库·后端·sql·spring
罗小爬EX2 天前
MyBatis Interceptor执行顺序详解(plugin机制、责任链模式)
mybatis·interceptor
weixin_704266052 天前
Spring整合MyBatis(一)
java·spring·mybatis
寻见9032 天前
10 分钟吃透 MyBatis 核心|从底层原理到实战技巧,Java 开发者必藏(无废话干货)
java·mysql·mybatis
TTc_2 天前
对于子查询语句多条sql报错排查
数据库·sql·mybatis