/*
* Copyright (c) 2011-2020, hubin ([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.core.conditions.query;
import static java.util.stream.Collectors.joining;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
import com.baomidou.mybatisplus.core.toolkit.support.Property;
/**
*
* Lambda 语法使用 Wrapper
*
*
* @author hubin miemie HCL
* @since 2017-05-26
*/
@SuppressWarnings("serial")
public class LambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, LambdaQueryWrapper<T>> {
/**
* 查询字段
*/
private List<String> queryColumn = new ArrayList<>();
/**
* 排除字段
*/
private List<String> excludeColumn = new ArrayList<>();
public LambdaQueryWrapper() {
// TO DO NOTHING
}
public LambdaQueryWrapper(T entity) {
this.entity = entity;
}
@SuppressWarnings(value = "unchecked")
public LambdaQueryWrapper(T entity, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs,
MergeSegments mergeSegments) {
this.entity = entity;
if (entity != null) {
this.entityClass = (Class<T>) entity.getClass();
}
this.paramNameSeq = paramNameSeq;
this.paramNameValuePairs = paramNameValuePairs;
this.expression = mergeSegments;
}
@Override
public String getSqlSelect() {
if (CollectionUtils.isEmpty(queryColumn)) {
if (entityClass != null) {
queryColumn = Arrays.asList(TableInfoHelper.getTableColumns(entityClass, excludeColumn.toArray(new String[0])));
}
} else {
return SqlUtils.stripSqlInjection(queryColumn.stream()
.filter(i -> !excludeColumn.contains(i)).collect(joining(StringPool.COMMA)));
}
return CollectionUtils.isEmpty(queryColumn) ? null : String.join(StringPool.COMMA, queryColumn);
}
/**
*
* SELECT 部分 SQL 设置
*
*
* @param columns 查询字段
*/
@SafeVarargs
public final LambdaQueryWrapper<T> select(Property<T, ?>... columns) {
for (Property<T, ?> column : columns) {
queryColumn.add(this.columnToString(column));
}
return typedThis;
}
/**
*
* SELECT 部分 SQL 设置
*
*
* @param excludeColumns 排除的查询字段
*/
@SafeVarargs
public final LambdaQueryWrapper<T> excludeColumns(Class<T> entityClass, Property<T, ?>... excludeColumns) {
Assert.notNull(entityClass, "entityClass is not null");
Assert.notEmpty(excludeColumns, "excludeColumns is not empty");
this.entityClass = entityClass;
for (Property<T, ?> column : excludeColumns) {
excludeColumn.add(this.columnToString(column));
}
return typedThis;
}
/**
*
* 排除字段,该方法请在 setEntity 之后使用,否则无法获知表实体类型
*
*
* @param excludeColumns 排除字段列表
*/
@SafeVarargs
@SuppressWarnings(value = "unchecked")
public final LambdaQueryWrapper<T> excludeColumns(Property<T, ?>... excludeColumns) {
Assert.notNull(entity, "Unable to find entity type, please use method `excludeColumns(Class entityClass, String... excludeColumns)`" );
return excludeColumns((Class<T>) entity.getClass(), excludeColumns);
}
@Override
protected LambdaQueryWrapper<T> instance(AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
return new LambdaQueryWrapper<>(entity, paramNameSeq, paramNameValuePairs, new MergeSegments());
}
}
1.自己手动添加的逻辑删除方法
settlementModeVOList.stream()
.filter(settlementModeVO -> settlementModeVO != null)
.map(SettlementModeVO::getCode)
.forEach(code -> {
// 逻辑删除
settlementModeDao.logicDeleteByWrapper(new SettlementMode()
.setUpdateBy(userSessionVO
.getUserId())
.setUpdateTime(date), new UpdateWrapper<SettlementMode>()
.lambda()
.eq(SettlementMode::getEntId, userSessionVO.getCurrentEntId())
.eq(SettlementMode::getCode, code));
});
2.新增方法
businessTypeVOList.stream().filter(businessTypeVO -> businessTypeVO != null).forEach(businessTypeVO -> {
BusinessType businessType = BusinessTypeTransfer.getBusinessType(businessTypeVO);
businessType.setCreateBy(userSessionInfo.getUserId()).setUpdateBy(userSessionInfo.getUserId())
.setCreateTime(date).setUpdateTime(date);
businessTypeDao.insert(businessType);
});
3.修改方法
businessTypeVOList.stream().filter(businessTypeVO -> businessTypeVO != null).forEach(businessTypeVO -> {
BusinessType businessType = BusinessTypeTransfer.getBusinessType(businessTypeVO);
businessType.setUpdateBy(userSessionInfo.getUserId()).setUpdateTime(date);
businessTypeDao.updateById(businessType);
});
4.查询方法
BusinessType businessType=businessTypeDao.selectOne(businessTypeQueryWrapper.lambda().eq(BusinessType::getId, condition.getId()));
5.带分页的查询(mybatis-plus自带查询)
QueryWrapper<ProductPrice> productPriceQueryWrapper = new QueryWrapper<>();
productPriceQueryWrapper.lambda().eq(ProductPrice::getIsDeleted, Constants.NO).eq(ProductPrice::getProductId, condition.getProductId()).orderByDesc(ProductPrice::getPriceTime).orderByDesc(ProductPrice::getId);
PageInfo pagingQuery = productPriceQueryVO.getPagingQuery();
List<ProductPrice> productPrices = null;
// 分页查询
if (pagingQuery != null) {
// 查询列表
IPage<ProductPrice> productPriceIPage = productPriceDao.selectPage(new Page<>(pagingQuery.getPageIndex(), pagingQuery.getPageSize()), productPriceQueryWrapper);
// 获取总记录
pagingQuery.setTotal(productPriceIPage.getTotal());
// 放入当前页面
pagingQuery.setPageIndex(productPriceIPage.getCurrent());
// 获取查询的数据
productPrices = productPriceIPage.getRecords();
}