Mybatis plus使用实体作为构造参数传入QueryWrapper进行分页模糊查询

记录一次踩坑经历
本想使用IService.page()方法对实体类的字段值进行模糊查询,后来发现内部是eq(),想要对某字段进行模糊查询需在实体类的对应字段上添加注解

StudentController.java

	......
	/**
     * 分页查询所有数据
     *
     * @param paramDto DTO 查询实体
     * @return 所有数据
     */
    @PostMapping("selectByCondition")
    @ApiOperation("按字段条件分页查询学生表")
    public Result<?> selectStudentByCondition(@RequestBody StudentDTO paramDto
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Mybatis-Plus进行分页模糊查询,可以通过以下步骤实现: 1. 在Mapper接口中定义分页查询方法,使用@Select注解指定SQL语句,使用@Param注解指定参数。 2. 在SQL语句中使用LIKE关键字进行模糊查询使用LIMIT关键字进行分页查询。 3. 在Service层调用Mapper接口中定义的分页查询方法,传入分页参数模糊查询参数。 4. 在Controller层中接收前端传来的分页参数模糊查询参数,调用Service层中的方法进行查询。 5. 将查询结果封装成Page对象返回给前端。 示例代码: Mapper接口: @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user WHERE name LIKE CONCAT('%',#{name},'%') LIMIT #{offset},#{limit}") List<User> selectUserList(@Param("name") String name, @Param("offset") int offset, @Param("limit") int limit); } Service层: @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserList(String name, int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); List<User> userList = userMapper.selectUserList(name, page.getOffset(), page.getLimit()); page.setRecords(userList); return page; } } Controller层: @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public Page<User> getUserList(@RequestParam(required = false) String name, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { return userService.getUserList(name, pageNum, pageSize); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值