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

相关推荐
哆啦A梦15887 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
弹简特15 小时前
【JavaEE19-后端部分】 MyBatis 入门第三篇:使用XML完成增删改查
xml·mybatis
小江的记录本20 小时前
【VO、DTO、Entity】VO、DTO、Entity三大核心数据对象全解析(附核心对比表 + 代码示例)
java·数据库·spring boot·spring·架构·mybatis·数据库架构
计算机学姐21 小时前
基于SpringBoot的流浪动物救助收养系统
vue.js·spring boot·后端·mysql·java-ee·intellij-idea·mybatis
spencer_tseng21 小时前
Tomcat server.xml <Connector> address=“0.0.0.0“
xml·tomcat
计算机学姐1 天前
基于SpringBoot的蛋糕烘焙销售服务系统
java·spring boot·后端·spring·tomcat·intellij-idea·mybatis
zdl6861 天前
Mybatis控制台打印SQL执行信息(执行方法、执行SQL、执行时间)
数据库·sql·mybatis
敲代码的嘎仔1 天前
Java后端面试——SSM框架面试题
java·面试·职场和发展·mybatis·ssm·springboot·八股
ruanyongjing1 天前
SpringBoot3 整合 Mybatis 完整版
mybatis
小江的记录本2 天前
【MyBatis-Plus】Spring Boot + MyBatis-Plus 进行各种数据库操作(附完整 CRUD 项目代码示例)
java·前端·数据库·spring boot·后端·sql·mybatis