• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java随笔 | mybatis-plus条件构造器QueryWrapper、UpdateWrapper的基本使用展示

武飞扬头像
程序猿林仔
帮助2


本文面向初学者和小白,介绍QueryWrapper和UpdateWrapper的简单使用。
QueryWrapper和UpdateWrapper都是mybatis-plus中Wrapper类的子类,用于构造SQL条件语句,QueryWrapper用于查询语句中,UpdateWrapper用于更新或删除语句中。
注:本文示例基于Springboot框架

实体(model)类:图书类型

// 实体类 图书类型
public class BookType {
    @TableId(value = "type_id", type = IdType.AUTO)
    private Integer typeId;// 类型ID
    private Integer parentId;// 父类型ID
    private String typeName;// 类型名称 例如科幻类...
}

一、QueryWrapper示例

Service层

通过QueryWrapper等效实现以下sql语句
SELECT * FROM book_type WHERE parent_id<>0 AND type_id>4 OR type_id<2

@Service
public class BookTypeServiceImpl extends ServiceImpl<BookTypeDao, BookType> implements BookTypeService {

    @Resource
    private BookTypeDao bookTypeDao;

    // 演示条件查询 使用QueryWrapper<>
    public List<BookType> finds(){
    	// 1.创建条件构造器 泛型为数据库表对应model类
        QueryWrapper<BookType> wrapper = new QueryWrapper<>();
        // 2.设置查询条件:ne不等于 ge大于 or或者 lt小于
        wrapper.ne("parent_id", 0).ge("type_id", 4).or().lt("type_id", 2);
        // 3.使用条件构造器
        List<BookType> bookTypes = bookTypeDao.selectList(wrapper);
        return bookTypes;
    }
}
学新通

二、UpdateWrapper示例

Service层

通过UpdateWrapper等效实现以下sql语句
DELETE FROM book_type WHERE type_id=8;

@Service
public class BookTypeServiceImpl extends ServiceImpl<BookTypeDao, BookType> implements BookTypeService {

    @Resource
    private BookTypeDao bookTypeDao;

    // 演示条件删除 使用UpdateWrapper<>
    public Integer wrapperDel(){
    	// 1.创建条件构造器 泛型为数据库表对应model类
        UpdateWrapper<BookType> wrapper = new UpdateWrapper<>();
        // 2.设置查询条件:eq 等于
        wrapper.eq("type_id", 8);
        // 3.使用条件构造器
        Integer rows = bookTypeDao.delete(wrapper);
        return rows;
    }
}
学新通

详细用法可参考:mybatis-plus中wrapper的用法

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgabgjb
系列文章
更多 icon
同类精品
更多 icon
继续加载