mybatisplus where语句里面的条件用括号括起来

这标题通俗易懂吧,说正事

queryWrapper.and(wrapper -> wrapper.eq("batch_name", prodBatch.getBatchName()).or().eq("batch_code", prodBatch.getBatchCode()));

	public Result add(@RequestBody ProdBatch prodBatch, HttpServletRequest request) {
		Result result = new Result();
		try {
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("is_delete", 0);
			queryWrapper.and(wrapper -> wrapper.eq("batch_name", prodBatch.getBatchName()).or().eq("batch_code", prodBatch.getBatchCode()));
			queryWrapper.eq("company_id", prodBatch.getCompanyId());
			queryWrapper.eq("plat_id", prodBatch.getPlatId());
			queryWrapper.orderByDesc("create_date");
			List prodBatchList = prodBatchService.list(queryWrapper);
			if (prodBatchList != null && prodBatchList.size() > 0) {
				result.setSuccess(false);
				result.setMessage("产品批次名称或产品批次编号重复");
				return result;
			}
			prodBatch.setCreateDate(new Date());
			prodBatch.setModifiedDate(new Date());
			prodBatchService.save(prodBatch); 
			result.success("添加成功!");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}

mybatisplus where语句里面的条件用括号括起来_第1张图片

切记不能丢了and 

打印出来的sql为

SELECT
	id,
	batch_name,
	batch_code,
	create_type,
	sort,
	come_into_season_date,
	video_monitoring_url,
	company_video,
	shop_url,
	vr_video,
	remark,
	prod_id,
	matrix_id,
	info_show,
	info_show_model_id,
	qr_url,
	company_id,
	create_date,
	modified_date,
	create_pei_id,
	modified_pei_id,
	is_delete,
	delete_date,
	plat_id
FROM
	tb_prod_batch
WHERE
	is_delete = 0
AND (
	batch_name = '20190723次'
	OR batch_code = '190723P000004000140'
)
AND company_id = '000001'
AND plat_id = '000001'
ORDER BY
	create_date DESC

mybatisplus where语句里面的条件用括号括起来_第2张图片

上面圈出来的就是我们想要的where里面带括号条件询,下面是不带括号的条件查询mybatisplus where语句里面的条件用括号括起来_第3张图片

 

你可能感兴趣的:(JAVA,mybatisplus,mybatisplus,where括号)