一.mybatis-plus实现
1.添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
2.分页实现
public Result findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
@RequestParam String name){
QueryWrapper<sys_menu> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
queryWrapper.like("name",name);
return Result.success(menuService.page(new Page<>(pageNum,pageSize),queryWrapper));
}
二.mybatis实现
1.添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
2.动态分页查询sql编写(在mapper.xml文件编写,通过方法名字绑定)
<select id="page_users" resultType="com.example.pt_springboot.entity.sys_user">
select * from sys_user
<where>
<if test="username!=null and username!=''">
username like concat('%',#{username},'%')
</if>
<if test="email!=null and email!=''">
and email like concat('%',#{email},'%')
</if>
<if test="address!=null and address!=''">
and address like concat('%',#{address},'%')
</if>
</where>
order by id desc limit #{pageNum},#{pageSize}
</select>
<!--分页数量查询-->
<select id="usersNum" resultType="int">
select count(*) from sys_user
<where>
<if test="username!=null and username!=''">
username like concat('%',#{username},'%')
</if>
<if test="email!=null and email!=''">
and email like concat('%',#{email},'%')
</if>
<if test="address!=null and address!=''">
and address like concat('%',#{address},'%')
</if>
</where>
</select>
<!--!!!!!注意,可以在前面加and关键字,避免拼接错误!!!!!!!!!!!-->