前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >修改mybatis-plus更新策略

修改mybatis-plus更新策略

作者头像
阿超
发布2024-04-05 10:16:41
1240
发布2024-04-05 10:16:41
举报
文章被收录于专栏:快乐阿超快乐阿超

把完善的教育留给子女,乃是最佳的遗产。——斯各特

就像:

https://github.com/apache/incubator-streampark/pull/3615

首先修改dbConfig.setUpdateStrategy(FieldStrategy.IGNORED);

代码语言:javascript
复制
/**
 * mybatis plus setting
 *
 * @return MybatisPlusPropertiesCustomizer
 */
@Bean
public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
  return properties -> {
    properties.setTypeAliasesPackage("org.apache.streampark.console.*.entity");
    properties.setTypeEnumsPackage("org.apache.streampark.console.*.enums");
    properties.setMapperLocations(new String[] {"classpath:mapper/*/*.xml"});
    MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
    mybatisConfiguration.setJdbcTypeForNull(JdbcType.NULL);
    properties.setConfiguration(mybatisConfiguration);
    GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(mybatisConfiguration);
    GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
    dbConfig.setIdType(IdType.AUTO);
    dbConfig.setUpdateStrategy(FieldStrategy.IGNORED);
    // close mybatis-plus banner
    globalConfig.setBanner(false);
    properties.setGlobalConfig(globalConfig);
  };
}

然后对于updateById的地方,进行调整,例如

代码语言:javascript
复制
@Override
public boolean updateById(Application application) {
  Application app = getById(application.getId());
  BeanUtil.copyIgnoreNull(application, app, Application::getId, Application::getCreateTime);
  app.setAppId(application.getAppId());
  app.setJobId(application.getJobId());
  app.setJobManagerUrl(application.getJobManagerUrl());
  app.setRestartSize(application.getRestartSize());
  app.setAlertId(application.getAlertId());
  app.setEndTime(application.getEndTime());
  app.setHotParams(application.getHotParams());
  app.setFlinkClusterId(application.getFlinkClusterId());
  return super.updateById(app);
}

这里用到的是

代码语言:javascript
复制
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.streampark.console.core.utils;

import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.func.Func1;

/** Util class for bean */
public class BeanUtil {

  /**
   * bean copy ignore null field
   *
   * @param source the source object for copy
   * @param target the target object for copy
   */
  @SafeVarargs
  public static <P, R> void copyIgnoreNull(
      Object source, Object target, Func1<P, R>... ignoreProperties) {
    cn.hutool.core.bean.BeanUtil.copyProperties(
        source,
        target,
        CopyOptions.create().ignoreNullValue().setIgnoreProperties(ignoreProperties));
  }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档