电商系统 常用代码 MyBatis-Plus
2023-09-11 14:22:26 时间
字段为空校验
@NotNull
@Min(1)
修改某字段为null
/**
* 收款编号
*/
@TableField(updateStrategy = FieldStrategy.IGNORED)
@ApiModelProperty(value = "收款编号")
判断是否存在 / 判断名称是否重复
原版
LambdaQueryWrapper<Dict> lqw = Wrappers.<Dict>query().lambda().eq(Dict::getCode, dict.getCode()).eq(Dict::getDictKey, dict.getDictKey());
Integer cnt = baseMapper.selectCount((Func.isEmpty(dict.getId())) ? lqw : lqw.notIn(Dict::getId, dict.getId()));
if (cnt > 0) {
throw new ApiException("当前字典键值已存在!");
}
修改版
LambdaQueryChainWrapper<Brand> query = new LambdaQueryChainWrapper<>(baseMapper)
.eq(Brand::getName, entity.getName());
query = (Objects.isNull(entity.getId()) || (entity.getId() <= 0))
? query
: query.notIn(Brand::getId, entity.getId());
if (query.count() > 0) {
throw new ServiceException("名称重复!");
}
通过ids查询数据
List<Material> materials = listByIds(ids);
查询object
Optional<Brand> brand = new LambdaQueryChainWrapper<>(baseMapper)
.eq(Brand::getName, brandName)
.oneOpt();
查询list
List<ProductAttributeValue> entities = new LambdaQueryChainWrapper<>(baseMapper)
.eq(ProductAttributeValue::getProductId, 1)
.list();
获取最大排序
private int getMaxSort(){
List<Store> stores = new LambdaQueryChainWrapper<>(baseMapper)
.orderByDesc(Store::getSort)
.list();
if (stores.size() <= 0) {
return 1;
}
return stores.get(0).getSort() + 1;
}
相关文章
- mybatis和mybatiplus中Error attempting to get column ‘xx‘ from result set
- Mybatis plus注解使用
- mybatis 引入java 常量
- 基于Spring+Spring MVC+Mybatis的B2C购物网站
- MyBatis里面用了多少种设计模式?
- Mybatis-Plus多数据源解析
- mybatis使用记录:#{}与${}区别、缓存机制、传递数组参数和List参数时if-test判空和判断长度的写法、Mybatis查询数据返回基本类型数组
- 【MyBatis】使用#{}与${}获取参数
- MyBatis_ibatis和mybatis的区别【转】
- Mybatis-Plus设置全局或者局部ID自增