Mybatis criteria 多个字段模糊匹配

需求:
搜索框中可输入手机号,姓名,地址查询,后台需要对一个框中的多个字段做匹配查询。在这里插入图片描述

方法一:多个字段模糊查询,写sql

<if test="condition!=null and condition!=''">
AND CONCAT(r.name,a.name,a.phone,a.addr_detail) LIKE  CONCAT('%',#{condition},'%') 
if>

方法二:单个字段模糊查询

select * from user where name like concat("%", #{name},"%")

方法三:多个字段模糊查询,使用criteria

Example example = new Example(OutboundLibraryRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleteFlag", Boolean.FALSE);
        //大于出库时间
        criteria.andGreaterThanOrEqualTo("outTime", queryTime);
        criteria.andEqualTo("storageCode", storageCode);
        Example example2 = new Example(OutboundLibraryRecord.class);
        if (StringUtils.isNotBlank(supplier)) {
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.orLike("supplierName", "%" + supplier + "%");
            criteria2.orLike("supplierCode", "%" + supplier + "%");
            example.and(criteria2);
        }
        if (StringUtils.isNotBlank(material)) {
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.orLike("materialName", "%" + material + "%");
            criteria2.orLike("materialCode", "%" + material + "%");
            example.and(criteria2);
        }
        List<StockEntity> stockEntityList = new ArrayList<>();
        List<OutboundLibraryRecord> recordList = outboundLibraryRecordMapper.selectByExample(example);

方法三会生成的sql

SELECT
	* 
FROM
	`outbound_library_record` 
WHERE
	( `delete_flag` = FALSE AND `out_time` >= '2020-08-29 10:59:13.8' AND `storage_code` = 'P01' ) 
	AND ( `supplier_name` LIKE '%123%' OR `supplier_code` LIKE '%123%' ) 
	AND ( `material_name` LIKE '%5006042XKZ16A%' OR `material_code` LIKE '%5006042XKZ16A%' );

你可能感兴趣的:(MySQL,MyBatis)