以下是修改后的代码实现模糊查询功能:
实体类(PhoneInfo)保持不变:
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("phone_info")
public class PhoneInfo {
private String model;
private Integer groupId;
private String sn;
private String imei;
private String version;
}
Mapper 接口(继承 BaseMapper)保持不变:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface PhoneInfoMapper extends BaseMapper<PhoneInfo> {
}
服务类(PhoneInfoService)修改如下:
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class PhoneInfoService extends ServiceImpl<PhoneInfoMapper, PhoneInfo> {
public List<PhoneInfo> queryPhoneInfo(String model, List<Integer> groupIds, String sn, String imei, String version) {
QueryWrapper<PhoneInfo> queryWrapper = new QueryWrapper<>();
if (model!= null) {
queryWrapper.like("model", model);
}
if (groupIds!= null &&!groupIds.isEmpty()) {
queryWrapper.in("group_id", groupIds);
}
if (sn!= null) {
queryWrapper.like("sn", sn);
}
if (imei!= null) {
queryWrapper.like("imei", imei);
}
if (version!= null) {
queryWrapper.like("version", version);
}
return baseMapper.selectList(queryWrapper);
}
}
在上述代码中,使用queryWrapper.like()方法进行模糊查询。如果要进行更加复杂的模糊查询,可以使用queryWrapper.likeRight()(后缀模糊查询)或queryWrapper.likeLeft()(前缀模糊查询)等方法。