Mybatis-Plus条件构造器

Mybatis-Plus条件构造器

详细见官网:https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

@SpringBootTest
public class WrapperTests {

    @Autowired
    mapper mapper;

    //名字和邮箱不为空,年龄大于12
    @Test
    void contextLoads(){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        wrapper
                .isNotNull("name")
                .isNotNull("email")
                .ge("age",12);
        mapper.selectList(wrapper).forEach(System.out::println);
    }

    //查询名字
    @Test
    void test1(){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        wrapper.eq("name","c");
        user user = mapper.selectOne(wrapper);
        System.out.println("user====>"+user);
    }

    //查询年龄在12-15之间的用户
    @Test
    void test2(){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        wrapper.between("age","12","15");//区间
        Integer count = mapper.selectCount(wrapper);//查询结果数
        System.out.println("查询结果数:"+count);
    }

    //模糊查询
    @Test
    void test3(){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        //左和右 c%
        wrapper
                .notLike("name","d")
                .likeRight("email","c");
        List<Map<String, Object>> maps = mapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

    @Test
    void test4(){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        //id在子查询中查出来
        wrapper.inSql("id","select id from user where id<13");
        List<Object> users = mapper.selectObjs(wrapper);
        users.forEach(System.out::println);
    }

    @Test
    void test5(){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        //通过id进行排序
        wrapper.orderByDesc("id");
        List<user> users = mapper.selectList(wrapper);
        users.forEach(System.out::println);
    }

}

你可能感兴趣的:(#,Mybatis-plus,java,mybatis)