com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-plus-extension Show documentation
Show all versions of mybatis-plus-extension Show documentation
An enhanced toolkit of Mybatis to simplify development.
/*
* Copyright (c) 2011-2022, baomidou ([email protected]).
*
* Licensed 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 com.baomidou.mybatisplus.extension.injector.methods;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.type.resolved.ResolvedType;
import java.util.function.Predicate;
/**
* 根据 ID 更新固定的那几个字段(但是不包含逻辑删除)
*
*
* 自己的通用 mapper 如下使用:
*
* int alwaysUpdateSomeColumnById(@Param(Constants.ENTITY) T entity);
*
*
*
* 如何筛选字段参考请 {@link InsertBatchSomeColumn} 里面的注释
*
* @author hubin
* @since 2019-04-12
*/
@SuppressWarnings("serial")
public class AlwaysUpdateSomeColumnById extends AbstractMethod {
/**
* 字段筛选条件
*/
@Setter
@Accessors(chain = true)
private Predicate predicate;
/**
* @param name 方法名
* @param predicate 筛选条件
* @since 3.5.0
*/
public AlwaysUpdateSomeColumnById(String name, Predicate predicate) {
super(name);
this.predicate = predicate;
}
public AlwaysUpdateSomeColumnById() {
super("alwaysUpdateSomeColumnById");
}
/**
* @param predicate 筛选条件
*/
public AlwaysUpdateSomeColumnById(Predicate predicate) {
super("alwaysUpdateSomeColumnById");
this.predicate = predicate;
}
@Override
public MappedStatement injectMappedStatement(ResolvedType mapperClass, ResolvedType modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);
String sqlSet = this.filterTableFieldInfo(tableInfo.getFieldList(), getPredicate(),
i -> i.getSqlSet(true, ENTITY_DOT), NEWLINE);
sqlSet = SqlScriptUtils.convertSet(sqlSet);
String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet,
tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return addUpdateMappedStatement(mapperClass, modelClass, getMethod(sqlMethod), sqlSource);
}
private Predicate getPredicate() {
Predicate noLogic = t -> !t.isLogicDelete();
if (predicate != null) {
return noLogic.and(predicate);
}
return noLogic;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy