MyBatis-Plus基础用法笔记(只记录了我个人需要注意的地方)

本文详细记录了MyBatis-Plus的CRUD接口使用,包括Service层的Save、SaveOrUpdate、Remove、List、Page方法,以及Chain链式查询。重点介绍了Mapper接口中的Insert、Delete、Update、Select操作,并讲解了如何利用AbstractWrapper实现条件构造器,进行单一条件查询。特别提到了在实际应用中如何结合articleService、Article实体和自定义的findByCode方法进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:文中articleService为Service实例,Article article为具体实体对象,list为存储实体对象的集合,article以code属性为主键 findByCode方法为自己封装的wrapper实现的,HmPage为IPage的具体实现类

  • CRUD接口
    • Service CRUD接口

      • Save
        articleService.saveBatch(list,3);//批量插入 每批次插入3条 3条3条的插入
        
      • SaveOrUpdate
        articleService.saveOrUpdate(article);//当数据库中已经存在改code记录时,报错 code已经存在
        
      • Remove
        //第一个删除不了 第二个和第三个删除效果是一样的 但是方法里边参数要求的是id 可以自动识别出数据库中查询出来带id属性的实体类 获取id对应值  包括getById方法 同理
        articleService.removeById(article);
        articleService.removeById(articleService.findByCode(article.getCode()));
        articleService.removeById(articleService.findByCode(article.getCode()).getId());
        
      • List
        Map<String,Object> params = new HashMap<>();
        params.put("type",3);
        params.put("company_code",2);
        //查询所有
        articleService.list();
        //根据条件查询
        articleService.listByMap(params);
        //返回map集合 不包含空值
        articleService.listMaps();
        //obj数组,只有id
        articleService.listObjs();
        
      • Page
        //无条件翻页查询 返回值为HmPage对象
        articleService.page(new HmPage());
        //带筛选条件翻页查询 返回值为HmPage对象
        articleService.page(new HmPage(),queryWrapper);
        
      • Chain链式查询
        //链式查询
        articleService.query().eq("code", 1234567L).eq("type", 1).list();
        
    • Mapper CRUD接口

      • Insert Delete Update Select
  • 条件构造器
    • AbstractWrapper
      • allEq

        //allEq最多有四个参数,第一个参数表示是否将该条件加入到最后生成的sql语句中 默认是true 
        //第二个参数为过滤函数,表示是否允许筛选字段传入对比条件中,用于在查询条件中再次进行过滤
        //第四个参数表示是否考虑值为null的情况 默认为true 当参数的值为null的时候 最后的sql语句会有 and xx is null
        //第三个参数为查询条件集合,为所有重载方法中的必填项
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.allEq(boolean condition,BiPredicate<R,V> filter,Map<R,V> params,boolean null2IsNull);
        
      • 单一条件查询

        方法含义sql
        eq等于key = ‘value’
        ne不等于key <> ‘value’
        gt大于key > value
        ge大于等于key >= value
        lt小于key < value
        le小于等于key <= value
        betweenBETWEEN 值1 AND 值2key between value1 and value2
        notBetweenNOT BETWEEN 值1 AND 值2key not between value1 and value2
        likeLIKE ‘%值%’key like ‘%value%’
        notLikeNOT LIKE ‘%值%’key not like ‘%value%’
        likeLeftLIKE ‘%值’key like ‘%value’
        likeRightLIKE ‘值%’key like ‘value%’
        isNullIS NULLkey is null
        isNotNullIS NOT NULLkey is not null
        inIN(value.get(0),value.get(1),…)key in (value1,value2,value3)
        notInkey not in (value1,value2,value3)
        inSqlIN(sql语句)key in (select key1 from table where key1 < 3)
        notInSqlNOT IN (sql语句)key not in (select key1 from table where key1 < 3)
        grounpBy分组 GROUNP BY 字段grounp by key1,key2
        orderByAscORDER BY 字段,… ASCorder by key1 ASC,key2 ASC
        orderByDescORDER BY 字段,…DESCorder by key1 DESC,key2 DESC
        orderByORDER BY 字段,…
        havingHAVING (sql语句)having (sql语句)
        orOR 默认连接为and,or需要主动调用or
        andANDand
        nested正常嵌套(不带AND或者OR)nested()
        apply拼接sqlapply()
        last无视规则直接拼接到sql最后,多次调用执行最后一次,有sql注入风险
        existsEXISTS(sql语句)exists ()
        notExistsNOT EXISTS(sql语句)not exists ()
        123
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值