Mybatis自带的Example自定义查询条件

    xiaoxiao2022-07-02  166

    在mybatis我们在写查询语句时,如果有多个查询条件一般都会先写个1=1,以避免查询条件为空时报错,mybatis使用Example时可以这样写:

    Example example = new Example(CourseElementTask.class); Example.Criteria criteria = example.createCriteria(); criteria.andCondition("1=1") .andEqualTo("id", po.getId()) .andEqualTo("elementId", po.getElementId()) .andEqualTo("status", StatusEnum.EFFECTIVE.getId()); if(Objects.nonNull(po.getName()) && !StringUtils.isEmpty(po.getName())){ example.and().andLike("name","%"+po.getName()+"%"); } example.orderBy("id").desc();

    可以看出在这里是可以通过andCondition来自定义自己的sql的

    最新回复(0)