【code】偶尔取值、判空、查表、验证等

想了想还是记录一下,后续不断补充,方便后续查阅

获取List<String>

// 获取表IDs去重
List<String> tIds = list.stream()
.map(TableColumn::getTableParentId)
.distinct()
.collect(Collectors.toList());

// 获取表IDs
List<String> tIds = tables.stream()
.map(Table::getId)
.collect(Collectors.toList());

// 使用List<Map> 大小写注意
List<String> ids = params.stream()
.map(e -> e.get("id"))
.collect(Collectors.toList());

// 获取Parameter.getCheckFormat
List<CheckFormat> checkFormat = Optional.ofNullable(parameter)
.map(u -> u.getCheckFormat())
.orElse(new ArrayList<>());

// 获取最大
FieldConfig maxFieldConfig = configs.stream()
.max(Comparator.comparing(FieldConfig::getOrderId))
.orElse(null);

判断参数

// 判断:类型
if (type == null || !Arrays.asList("40", "60", "80").contains(type)) {
    String msg = "选择参数类型 40.库 60.表 80.字段";
    return TableUtil.getInstance().errorReturn(code, msg, msg);
}

// 判断:使用List<Map>,if(list != null && list.size() > 0) ===》》 CollectionUtils.isNotEmpty(list);
if (CollectionUtils.isEmpty(ruleList)) {
    String msg = "未找到到规则";
    return apiErrorReturn(500, msg, msg);
}

表查询

// 修改
LambdaUpdateWrapper<Dir> dirUpdate = Wrappers.lambdaUpdate();
dirUpdate.set(Dir::getName, source.getCnName());
dirUpdate.eq(Dir::getType, 13);
dirUpdate.eq(Dir::getCode, source.getCode());
directoryMapper.update(new SsDir(), dirUpdate);

// 查询
List<Integer> ids = params.stream().map(QuotaBO::getId).collect(Collectors.toList());
LambdaQueryWrapper<QuotaBO> queryQuota = Wrappers.lambdaQuery();
queryQuota.eq(QuotaBO::getIsDelete, 0);
queryQuota.in(QuotaBO::getId, ids);
List<QuotaBO> quotaList = quotaMapper.getList(queryQuota);

// 查询
QueryWrapper qw = new QueryWrapper<>();
qw.eq("COLUMN_NAME", columnName);
List<ColumnBO> list = colService.getListByPage(qw, pIndex, pSize);

// 修改
UpdateWrapper<ColumnBO> uw = new UpdateWrapper<>();
uw.eq("ID", column.getId());
List<ColumnBO> list = colService.getListByPage(column, uw);

数据去重

// 使用id去重处理
bloodList = bloodList.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(() 
-> new TreeSet<>(Comparator.comparing(BloodBO::getId))), ArrayList::new));

验证是否存在

有数据时候就不要重复添加数据,在验证该数据是否已存在时候,需要考虑新增、修改(修改时候需要忽略本身已存在条件)

select count(1) as valid, concat('分类编号【' ,#{itemId}, '】已存在') as message from pig_item 
where IS_DELETE = 0 
and ITEM_ID = #{itemId} 
and (ID !=  #{idOld} or #{idOld} is null) 
<select id="checkVerifyItemNum" resultType="java.lang.Integer">
	<![CDATA[
	select count(1) as valid from pig_item 
		where IS_DELETE=0 
			and ITEM_ID = #{itemId} 
			
	]]>
	<if test="id != null">
	<![CDATA[
		and (ID !=  #{id} or #{id} is null)
	]]>
	</if>
</select>

<![CDATA[ 内容 ]]> 这个写法好处就是大于小于就不要转义,不方便的是判断条件时候就要分开写

多表查询

多张表查询,left join下去,数据是有了,当数据量多了以后,查询性能降低.

with tmp_tab as ( 
	select 先查询数据
),
tmp_desc as (
	select 再查询数据 
) select * from tmp_desc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

掘金者说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值