mybatis plus page eq 多个条件

65 篇文章 1 订阅
18 篇文章 0 订阅

mybatis plus page eq 多个条件, com.baomidou.mybatisplus.extension.plugins.pagination.Page 好使,
import com.github.pagehelper.Page; 不好使
下面这个好使

  @RequestMapping(value = "/selectPage/equal/byPage", method = RequestMethod.POST)
    public Object selectPageEqualByPage(@RequestBody MultiQuestion multiQuestion,
                                  @RequestParam(required = false, defaultValue = "0") int pageNumber,
                                  @RequestParam(required = false, defaultValue = "10") int pageSize) {
        try {
            //创建一个page对象
//            Page<MultiQuestion> pageTeacher = new Page<>();
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<MultiQuestion> pageTeacher =
                    new   com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNumber,pageSize);

//            com.baomidou.mybatisplus.extension.plugins.pagination.Page

//            PageHelper.startPage(pageNumber,pageSize);

            LambdaQueryWrapper<MultiQuestion> equals = equalWrapper(multiQuestion);
//            LambdaQueryWrapper<MultiQuestion> equals = equalWrapper2(multiQuestion);
            log.info("equals {}",equals);
//            log.info(equals.toString());
            String sqlSelect = equals.getSqlSelect();
            log.info("sqlSelect {}",sqlSelect);

//            List<MultiQuestion> list = multiQuestionService.list(equals);
            IPage<MultiQuestion> page = multiQuestionService.page(pageTeacher, equals);
//            Page writersPage = (Page) list;
//            log.info(" list {}",list);
            return ReturnT.success(page);
        }catch (Exception e){
            log.info("error {}",e.getMessage());
//            return  ApiResultHandler.buildApiResult(ApiResultHandler.error,e.getMessage(),e.getMessage());
            return  ReturnT.error(e.getMessage());
        }
//        finally {
//            PageHelper.clearPage();
//        }
    }
LambdaQueryWrapper<MultiQuestion>  equalWrapper(MultiQuestion multiQuestion){
        LambdaQueryWrapper<MultiQuestion> equals = Wrappers.lambdaQuery();
        equals
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getQuestionId()),
                        MultiQuestion::getQuestionId,
                        multiQuestion.getQuestionId()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getSubject()),
                        MultiQuestion::getSubject,
                        multiQuestion.getSubject()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getQuestion()),
                        MultiQuestion::getQuestion,
                        multiQuestion.getQuestion()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getAnswerA()),
                        MultiQuestion::getAnswerA,
                        multiQuestion.getAnswerA()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getAnswerB()),
                        MultiQuestion::getAnswerB,
                        multiQuestion.getAnswerB()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getAnswerC()),
                        MultiQuestion::getAnswerC,
                        multiQuestion.getAnswerC()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getAnswerD()),
                        MultiQuestion::getAnswerD,
                        multiQuestion.getAnswerD()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getRightAnswer()),
                        MultiQuestion::getRightAnswer,
                        multiQuestion.getRightAnswer()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getAnalysis()),
                        MultiQuestion::getAnalysis,
                        multiQuestion.getAnalysis()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getScore()),
                        MultiQuestion::getScore,
                        multiQuestion.getScore()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getSection()),
                        MultiQuestion::getSection,
                        multiQuestion.getSection()
                )
                .eq(
                        !StringUtils.isNullOrEmpty(multiQuestion.getLevel()),
                        MultiQuestion::getLevel,
                        multiQuestion.getLevel()
                )

        ;
        return equals;
    }
pect         : 开始执行 class com.exam.controller.Mult
iQuestionControllerMbp.selectPageEqualByPage
2022-07-16 14:39:13.019  INFO 44216 --- [nio-8082-exec-6] c.e.c.MultiQuestionControllerMbp         : equals com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper@7b77f137
2022-07-16 14:39:13.019  INFO 44216 --- [nio-8082-exec-6] c.e.c.MultiQuestionControllerMbp         : sqlSelect null
2022-07-16 14:39:13.020 DEBUG 44216 --- [nio-8082-exec-6] c.e.m.MultiQuestionMapper.selectPage     : ==>  Preparing: SELECT COUNT(1) FROM ( SELECT questionId,subject,section,answerA,answerB,answerC,answerD,question,level,rightAnswer,analysis,score FROM multi_question ) TOTAL 
2022-07-16 14:39:13.021 DEBUG 44216 --- [nio-8082-exec-6] c.e.m.MultiQuestionMapper.selectPage     : ==> Parameters: 
2022-07-16 14:39:13.032 DEBUG 44216 --- [nio-8082-exec-6] c.e.m.MultiQuestionMapper.selectPage     : ==>  Preparing: SELECT questionId,subject,section,answerA,answerB,answerC,answerD,question,level,rightAnswer,analysis,score FROM multi_question LIMIT ?,? 
2022-07-16 14:39:13.032 DEBUG 44216 --- [nio-8082-exec-6] c.e.m.MultiQuestionMapper.selectPage     : ==> Parameters: 0(Long), 10(Long)
2022-07-16 14:39:13.044 DEBUG 44216 --- [nio-8082-exec-6] c.e.m.MultiQuestionMapper.selectPage     : <==      Total: 10
2022-07-16 14:39:13.045  INFO 44216 --- [nio-8082-exec-6] com.exam.config.ControllerAspect         : 方法执行完毕, 共用时:26毫秒

github page不好使 PageHelper.startPage(pageNumber,pageSize);

/**
     *
     let  data= {
     "questionId":null ,
     "subject":null ,
     "question":null ,
     "answerA":null ,
     "answerB":null ,
     "answerC":null ,
     "answerD":null ,
     "rightAnswer":null ,
     "analysis":null ,
     "score":null ,
     "section":null ,
     "level":null
     }
     axios.post(Common.baseUrl + `/multiQuestion/selectPage/equal?pageNumber=${this.pageNumber}&pageSize=${this.pageSize}`,data).then(res => {
     console.log("res");
     console.log(res);
     });
     */
    @RequestMapping(value = "/selectPage/equal", method = RequestMethod.POST)
    public Object selectPageEqual(@RequestBody MultiQuestion multiQuestion,
                                  @RequestParam(required = false, defaultValue = "0") int pageNumber,
                                  @RequestParam(required = false, defaultValue = "10") int pageSize) {
        try {
            PageHelper.startPage(pageNumber,pageSize);
            LambdaQueryWrapper<MultiQuestion> equals = Wrappers.lambdaQuery();
            equals
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getQuestionId()),
                            MultiQuestion::getQuestionId,
                            multiQuestion.getQuestionId()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getSubject()),
                            MultiQuestion::getSubject,
                            multiQuestion.getSubject()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getQuestion()),
                            MultiQuestion::getQuestion,
                            multiQuestion.getQuestion()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerA()),
                            MultiQuestion::getAnswerA,
                            multiQuestion.getAnswerA()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerB()),
                            MultiQuestion::getAnswerB,
                            multiQuestion.getAnswerB()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerC()),
                            MultiQuestion::getAnswerC,
                            multiQuestion.getAnswerC()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerD()),
                            MultiQuestion::getAnswerD,
                            multiQuestion.getAnswerD()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getRightAnswer()),
                            MultiQuestion::getRightAnswer,
                            multiQuestion.getRightAnswer()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnalysis()),
                            MultiQuestion::getAnalysis,
                            multiQuestion.getAnalysis()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getScore()),
                            MultiQuestion::getScore,
                            multiQuestion.getScore()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getSection()),
                            MultiQuestion::getSection,
                            multiQuestion.getSection()
                    )
                    .eq(
                            !StringUtils.isNullOrEmpty(multiQuestion.getLevel()),
                            MultiQuestion::getLevel,
                            multiQuestion.getLevel()
                    )

            ;

            log.info("equals {}",equals);
//            log.info(equals.toString());
            String sqlSelect = equals.getSqlSelect();
            log.info("sqlSelect {}",sqlSelect);

            List<MultiQuestion> list = multiQuestionService.list(equals);
            Page writersPage = (Page) list;
            log.info(" list {}",list);
            return ReturnT.success(writersPage);
        }catch (Exception e){
            log.info("error {}",e.getMessage());
//            return  ApiResultHandler.buildApiResult(ApiResultHandler.error,e.getMessage(),e.getMessage());
            return  ReturnT.error(e.getMessage());
        }
        finally {
            PageHelper.clearPage();
        }
    }
      : 传入参数: [MultiQuestion(questionId=null, subject=null, section=null, answerA=null, answerB=null, answerC=null, answerD=null, question=null, level=null, rightAnswer=null, analysis=null, score=null), 0, 10]
2022-07-16 14:20:39.439  INFO 49776 --- [nio-8082-exec-2] com.exam.config.ControllerAspect         : 开始执行 class com.exam.controller.MultiQuestionControllerMbp.selectPageEqual
2022-07-16 14:20:39.446  INFO 49776 --- [nio-8082-exec-2] c.e.c.MultiQuestionControllerMbp         : equals com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper@4aab80f8
2022-07-16 14:20:39.446  INFO 49776 --- [nio-8082-exec-2] c.e.c.MultiQuestionControllerMbp         : sqlSelect null
2022-07-16 14:20:39.484 DEBUG 49776 --- [nio-8082-exec-2] c.e.m.M.selectList_COUNT                 : ==>  Preparing: SELECT count(0) FROM multi_question 
2022-07-16 14:20:39.493 DEBUG 49776 --- [nio-8082-exec-2] c.e.m.M.selectList_COUNT                 : ==> Parameters: 
2022-07-16 14:20:39.517 DEBUG 49776 --- [nio-8082-exec-2] c.e.m.M.selectList_COUNT                 : <==      Total: 1
2022-07-16 14:20:39.517  INFO 49776 --- [nio-8082-exec-2] c.e.c.MultiQuestionControllerMbp         :  list Page{count=true, pageNum=0, pageSize=10, startRow=0, endRow=0, total=34, pages=4, reasonable=false, pageSizeZero=false}[]
2022-07-16 14:20:39.518  INFO 49776 --- [nio-8082-exec-2] com.exam.config.ControllerAspect         : 方法执行完毕, 共用时:80毫秒

但是github page PageHelper.startPage(pageNumber,pageSize); like的多条件 他又是好使的
这里搞错了 其实也不行,估计是分页插件的问题,一个项目 只能用同一种分页插件吧
只能用同一种分页插件吧

  @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public Object selectPage(@RequestBody MultiQuestion multiQuestion,
                             @RequestParam(required = false, defaultValue = "0") int pageNumber,
                             @RequestParam(required = false, defaultValue = "10") int pageSize) {
        try {


//            boolean save = multiQuestionService.save(multiQuestion);

            List<MultiQuestion>multiQuestions=new ArrayList<>();
//            multiQuestionService.saveOrUpdate(multiQuestions);
//            multiQuestionService.saveOrUpdateBatch(multiQuestions);
            mybatis.configuration.mapUnderscoreToCamelCase
//            multiQuestionService.deleteBatch(multiQuestions);
//            multiQuestionService.removeById()
//            mapUnderscoreToCamelCase false
            log.info("mapUnderscoreToCamelCase {}",mapUnderscoreToCamelCase);
            log.info("selectPage");
            PageHelper.startPage(pageNumber,pageSize);
            LambdaQueryWrapper<MultiQuestion> like = Wrappers.lambdaQuery();
            like                    .like(
                    !StringUtils.isNullOrEmpty(multiQuestion.getQuestionId()),
                    MultiQuestion::getQuestionId,
                    multiQuestion.getQuestionId()
            )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getSubject()),
                            MultiQuestion::getSubject,
                            multiQuestion.getSubject()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getQuestion()),
                            MultiQuestion::getQuestion,
                            multiQuestion.getQuestion()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerA()),
                            MultiQuestion::getAnswerA,
                            multiQuestion.getAnswerA()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerB()),
                            MultiQuestion::getAnswerB,
                            multiQuestion.getAnswerB()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerC()),
                            MultiQuestion::getAnswerC,
                            multiQuestion.getAnswerC()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnswerD()),
                            MultiQuestion::getAnswerD,
                            multiQuestion.getAnswerD()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getRightAnswer()),
                            MultiQuestion::getRightAnswer,
                            multiQuestion.getRightAnswer()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getAnalysis()),
                            MultiQuestion::getAnalysis,
                            multiQuestion.getAnalysis()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getScore()),
                            MultiQuestion::getScore,
                            multiQuestion.getScore()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getSection()),
                            MultiQuestion::getSection,
                            multiQuestion.getSection()
                    )
                    .like(
                            !StringUtils.isNullOrEmpty(multiQuestion.getLevel()),
                            MultiQuestion::getLevel,
                            multiQuestion.getLevel()
                    )
            ;
            List<MultiQuestion> list = multiQuestionService.list(like);
            Page writersPage = (Page) list;
//            return ApiResultHandler.buildApiResult(writersPage);
            return ApiResultHandler.buildApiResult(200,"添加成功",writersPage);
        }
        catch (Exception e){
            log.info("error {}",e.getMessage());
            return  ApiResultHandler.buildApiResult(ApiResultHandler.error,e.getMessage(),e.getMessage());
        }
        finally {
            PageHelper.clearPage();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 中进行多条件查询可以使用 `Wrapper` 类来构建查询条件。而在进行多表关联查询时,可以使用 `Joiner` 类来构建 JOIN 语句。 下面是一个示例,演示如何使用 MyBatis-Plus 进行多条件查询和多表关联查询: ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public Page<User> queryUsersByConditions(String name, Integer age) { // 构建查询条件 QueryWrapper<User> queryWrapper = Wrappers.query(); queryWrapper.like("name", name); queryWrapper.eq("age", age); // 构建分页对象 Page<User> page = new Page<>(1, 10); // 查询第一页,每页10条记录 // 执行查询 return userMapper.selectPage(page, queryWrapper); } public Page<User> queryUsersWithJoin(String roleName) { QueryWrapper<User> queryWrapper = Wrappers.query(); queryWrapper.apply("role r, user u"); queryWrapper.eq("u.role_id", "r.id"); queryWrapper.eq("r.name", roleName); Page<User> page = new Page<>(1, 10); return userMapper.selectPage(page, queryWrapper); } } ``` 在上述示例中,`queryUsersByConditions` 方法演示了如何使用 `QueryWrapper` 来构建多条件查询。其中使用了 `like` 方法来模糊查询 `name` 字段,使用了 `eq` 方法来精确查询 `age` 字段。 `queryUsersWithJoin` 方法演示了如何使用 MyBatis-Plus 进行多表关联查询。在 `QueryWrapper` 中使用了 `apply` 方法来拼接 JOIN 语句,并使用了 `eq` 方法来设置关联条件。 你可以根据自己的需求进行相应的调整和扩展。希望对你有所帮助!如有更多问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值