mybats-plus 增删改查的语句实例(高级)

QueryWrapper

//查询指定字段
//只查询出表中"id","name","nickname"字段对应的全部数据
public void select1(){
    QueryWrapper qw = new QueryWrapper<>();
    qw.select("id","name","nickname");
    List users = userMapper.selectList(qw);
    users.forEach(System.out::println); }
    
//只查询“name”字段的数据
//查询出不包含“nickname”的数据
public void select2(){
    QueryWrapper qw = new QueryWrapper<>();
    qw.select(User.class,i -> i.getColumn().equals("name"));//查询name
    // qw.select(User.class,i -> !i.getColumn().equals("nickname"));  //排除nickname
    List users = userMapper.selectList(qw);
    users.forEach(System.out::println); }

UpdateWrapper

//修改id为1,3,5的数据中gender字段的数据为false
public void set(){
    UpdateWrapper<User> uw = new UpdateWrapper<>();
    uw.set("gender",false)
            .in("id",1,3,5);
    int res = userMapper.update(null, uw);
    System.out.println(res);
}
//修改
public void setSQL(){
    UpdateWrapper<User> uw = Wrappers.update();
    uw.setSql("password=4321"); // UPDATE tb_user SET password=4321;
    int res = userMapper.update(null, uw);
    System.out.println(res);
}

QueryWrapper # lambda

//查询id在10-20之间数据
public void between(){
    QueryWrapper<User> qw = Wrappers.query();
    qw.lambda().between(User::getId,10,20);
    List<User> users = userMapper.selectList(qw);
    users.forEach(System.out::println);
}
//删除id为30的数据
public void delete(){
    QueryWrapper<User> qw = Wrappers.query();
    qw.lambda().eq(User::getId,30);
    int res = userMapper.delete(qw);
}
//修改id为30的数据的name的值为“张三四”
public void update(){
    UpdateWrapper<User> uw = Wrappers.update();
    uw.lambda().eq(User::getId,30).setSql("name='张三四'");
    int res = userMapper.update(null, uw);
    System.out.println(res);
}
//查询出“nickname”="an"的数据的数量
public void count(){
    QueryWrapper<User> qw = Wrappers.query();
    qw.lambda().like(User::getNickname,"an");
    Integer res = userMapper.selectCount(qw);
    System.out.println(res);
}

LambdaQueryWrapper

//查询出id=2的数据
public void eqDemo() {
    //使用 QueryWrapper 的成员方法方法 lambda 构建 LambdaQueryWrapper
    LambdaQueryWrapper<User> lqw = new QueryWrapper<User>().lambda();
    lqw.eq(User::getId, 2);
    User user = userMapper.selectOne(lqw);
    System.out.println(user);
}
//查询出id分别=1,2,3的数据
public void inDemo() {
    //直接 new 出 LambdaQueryWrapper
    LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
    lqw.in(User::getId, 1, 2, 3);
    List<User> users = userMapper.selectList(lqw);
    users.forEach(System.out::println);
}
//查询出name中含有“an”的数据集
public void likeDemo() {
    //使用 Wrappers 的静态方法 lambdaQuery 构建 LambdaQueryWrapper(推荐)
    LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
    lqw.like(User::getUsername, "an");
    List<User> users = userMapper.selectList(lqw);
    users.forEach(System.out::println);
}
//查出id=10的数据
public void oneDemo() {
    //使用 链式查询 的方法构建 LambdaQueryWrapper
    User user = new LambdaQueryChainWrapper<User>(userMapper)
            .eq(User::getId, 10)
            .one();
    System.out.println(user);
}
//name模糊左查询
public void likeRightDemo() {
    //使用 链式查询 的方法构建 LambdaQueryWrapper
    List<User> users = new LambdaQueryChainWrapper<User>(userMapper)
            //相当于原生 sql 的 like 右通配符模糊查询,比如:like 'an%'
            .likeRight(User::getUsername, "an")
            .list();
    users.forEach(System.out::println);
}
//name 模糊右查询
public void likeLeftDemo() {
    LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
    //相当于原生 sql 的 like 右通配符模糊查询,比如:like '%an'
    lqw.likeLeft(User::getUsername, "an");
    List<User> users = userMapper.selectList(lqw);
    users.forEach(System.out::println);
}
//查询出name ="an"的指定字段数据(id,name,nickname)
public void partColumnsDemo() {
    LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
    //查询指定的字段
    lqw.select(User::getId, User::getName, User::getNickname)
            .eq(User::getUsername, "an");
    List<User> users = userMapper.selectList(lqw);
    users.forEach(System.out::println);
}
//
public void ifDemo() {
    User condition = User.builder().username("zhangsan").build();
    LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
    lqw.eq(condition.getGender() != null, User::getGender, condition.getGender())
            .eq(condition.getUsername() != null, User::getUsername, condition.getUsername());
    List<User> users = userMapper.selectList(lqw);
    users.forEach(System.out::println);
}
//
public void orAndDemo() {
    LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
    lqw.gt(User::getCredit, 10000)
            .and(wrapper -> wrapper.lt(User::getCredit, 100000))
            .or()
            .eq(User::getGender, true);
    List<User> users = userMapper.selectList(lqw);
    users.forEach(System.out::println);
}
//
public void pageDemo() {
    Page<User> page = new Page<>(4, 3);//查询出第4页的3行数据
    LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
    lqw.orderByAsc(User::getId);//按照id排序
    Page<User> res = userMapper.selectPage(page, lqw);
    res.getRecords().forEach(System.out::println);
}
//修改id为5的数据的name 和 UserName数据
public void updateDemo1() {
    LambdaUpdateWrapper<User> luw = Wrappers.lambdaUpdate();
    luw.eq(User::getId, 5)
            .set(User::getName, "大中国")
            .set(User::getUsername, "老大");
    userMapper.update(null, luw);
}
//修改id=3的数据的NickName值
public void updateDemo2() {
    LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
    luw.eq(User::getId, 3)
            .set(User::getNickname, "wanger");
    int res = userMapper.update(null, luw);
    System.out.println(res);
}
//修改id=4的数据的NickName的值
public void updateDemo3() {
    boolean res = new LambdaUpdateChainWrapper<User>(userMapper)
            .eq(User::getId, 4)
            .set(User::getNickname, "mazi")
            .update();
    System.out.println(res);
}


你可能感兴趣的:(A_mybatis-plus,mybatis-plus)