广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis-plus Wrapper条件构造器updateForSet更新方式
  • 478
分享到

mybatis-plus Wrapper条件构造器updateForSet更新方式

2024-04-02 19:04:59 478人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录Wrapper条件构造器updateForSet更新简单示例条件参数说明mybatis-plus中update更新使用注意事项update(pojo,Wrapper)方

Wrapper条件构造器updateForSet更新

官方文档:https://baomidou.gitee.io/mybatis-plus-doc/#/wrapper

实体包装器,用于处理 sql 拼接,排序,实体参数查询等!

补充说明: 使用的是数据库字段,不是Java属性!

实体包装器 EntityWrapper 继承 Wrapper

简单示例

翻页查询

public Page<T> selectPage(Page<T> page, EntityWrapper<T> entityWrapper) {
  if (null != entityWrapper) {
      entityWrapper.orderBy(page.getOrderByField(), page.isAsc());
  }
  page.setRecords(baseMapper.selectPage(page, entityWrapper));
  return page;
}

拼接sql方式 一

@Test
public void testTSQL11() {
    
    EntityWrapper<User> ew = new EntityWrapper<User>();
    ew.setEntity(new User(1));
    ew.where("user_name={0}", "'zhangsan'").and("id=1")
            .orNew("user_status={0}", "0").or("status=1")
            .notLike("user_nickname", "notvalue")
            .andNew("new=xx").like("hhh", "DDD")
            .andNew("pwd=11").isNotNull("n1,n2").isNull("n3")
            .groupBy("x1").groupBy("x2,x3")
            .having("x1=11").having("x3=433")
            .orderBy("dd").orderBy("d1,d2");
    System.out.println(ew.getSqlSegment());
}

括号拼接

.isNull("removor").andNew("idCard="+ idCard+" or phone="+ phone)

sql 

(removor IS NULL) AND (idCard=666 or phone=15866958266) 

拼接sql方式二

int buyCount = selectCount(Condition.create()
                .setSqlSelect("sum(quantity)")
                .isNull("order_id")
                .eq("user_id", 1)
                .eq("type", 1)
                .in("status", new Integer[]{0, 1})
                .eq("product_id", 1)
                .between("created_time", startDate, currentDate)
                .eq("weal", 1));

更新:updateForSet根据条件更新字段

.updateForSet("hits=hits+1", new EntityWrapper<T>().eq("id", id));

自定义SQL方法如何使用 Wrapper

mapper java接口方法

List<User> selectMyPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);

mapper xml定义

<select id="selectMyPage" resultType="User">
  SELECT * FROM user 
  <where>
  ${ew.sqlSegment}
  </where>
</select>

关于${ew.sqlSegment} 使用了 $ 不要误以为就会被 sql 注入,请放心使用 mp 内部对 wrapper 进行了字符转义处理!

条件参数说明

查询方式说明
setSqlSelect设置 SELECT 查询字段
whereWHERE 语句,拼接 + WHERE 条件
andAND 语句,拼接 + AND 字段=值
andNewAND 语句,拼接 + AND (字段=值)
orOR 语句,拼接 + OR 字段=值
orNewOR 语句,拼接 + OR (字段=值)
eq等于=
allEq基于 map 内容等于=
ne不等于<>
gt大于>
ge大于等于>=
lt小于<
le小于等于<=
like模糊查询 LIKE
notLike模糊查询 NOT LIKE
inIN 查询
notInNOT IN 查询
isNullNULL 值查询
isNotNullIS NOT NULL
groupBy分组 GROUP BY
havingHAVING 关键词
orderBy排序 ORDER BY
orderAscASC 排序 ORDER BY
orderDescDESC 排序 ORDER BY
existsEXISTS 条件语句
notExistsNOT EXISTS 条件语句
betweenBETWEEN 条件语句
notBetweenNOT BETWEEN 条件语句
addFilter自由拼接 SQL
last拼接在最后,例如:last("LIMIT 1")

注意! xxNew 都是另起 ( ... ) 括号包裹。

mybatis-plus中update更新使用注意事项

update(pojo,Wrapper)方法

封装一个对象mcTemplate,使用update(pojo,Wrapper) ,该方法仅仅修改mcTemplate中不为空的字段,别的字段不更新,在数据库中保持不变,如下:

mcTemplate.setStatus(TemplateStatusEnum.PASSED.getStatus());//待更新的字段
LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper();
lambdaUpdateWrapper.eq(McTemplate::getId,mcTemplate.getId() ); //限定条件
Integer result = mcTemplateMapper.update(mcTemplate, lambdaUpdateWrapper); //更新mcTemplate中不为空的字段

更新的字段比较少

不想封装成一个对象的时候,可以采取便捷方式,该方式同上只会更新设定的字段,对于其他字段不更新。

LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper();
lambdaUpdateWrapper.eq(McTemplate::getId,mcTemplate.getId() )
.set(McTemplate::getStatus,1); //更新的值
Integer result = mcTemplateMapper.update(null, lambdaUpdateWrapper);

updateById(mcTemplate)方法

该方法会将所有的字段都更新,在对象mcTemplate中没有的字段,会字段赋值null

int result = mcTemplateMapper.updateById(mcTemplate);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: mybatis-plus Wrapper条件构造器updateForSet更新方式

本文链接: https://www.lsjlt.com/news/143444.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作