MyBatis-Plus 的 QueryWrapper 应用以及在内存中处理JSON数组字符串匹配
需求分析:
咨询师筛选:
列表浏览:展示“全部咨询师”列表,包含姓名、从业时长、擅长领域、用户好评率等基础信息;
精准筛选:支持按“困扰类型(如焦虑、婚姻家庭)”“性别”“咨询方式”“流派”标签筛选;
关键词搜索:通过搜索框输入咨询师姓名、擅长领域等关键词查找目标咨询师;
具体实现:
直接把搜索和筛选条件放到couselorDTO中
// 搜索和筛选条件
private String keyword;
private List<String> specializationTags;
private List<String> therapeuticApproachTags;
private List<String> serviceTypeTags;
private String genderFilter;
/**
* 搜索和筛选咨询师(支持关键词搜索和多维度筛选)
* 实现策略:数据库查询 + 内存筛选
*
* @param counselorDTO 包含搜索条件和筛选条件的DTO对象
* @return 符合条件的咨询师DTO列表
*/
@Override
public List<CounselorDTO> searchAndFilterCounselors(CounselorDTO counselorDTO) {
// 第一步:构建基础查询条件(使用MyBatis-Plus的QueryWrapper)
QueryWrapper<Counselors> queryWrapper = new QueryWrapper<>();
// 关键词搜索:在多个字段中进行模糊匹配
if (StringUtils.hasText(counselorDTO.getKeyword())) {
String keyword = "%" + counselorDTO.getKeyword() + "%";
// 使用AND连接主条件,内部使用OR连接多个字段的模糊查询
queryWrapper.and(wrapper -> wrapper
.like("real_name", keyword) // 真实姓名匹配
.or().like("specialization", keyword) // 擅长领域匹配
.or().like("therapeutic_approach", keyword) // 治疗流派匹配
.or().like("introduction", keyword) // 个人介绍匹配
);
}
// 状态筛选:默认只查询已通过审核的咨询师
// 确保只有审核通过的咨询师才会出现在搜索结果中
queryWrapper.eq("status", "APPROVED");
// 执行数据库查询,获取基础结果集
List<Counselors> counselorsList = baseMapper.selectList(queryWrapper);
// 第二步:将实体对象转换为DTO对象,并关联用户信息和服务设置
List<CounselorDTO> result = new ArrayList<>();
for (Counselors counselor : counselorsList) {
CounselorDTO dto = convertToDTO(counselor);
result.add(dto);
}
// 第三步:在内存中进行复杂的标签筛选(因为涉及JSON字段解析)
// 注意:这里没有使用XML中定义的filterByTags方法,而是在内存中处理
if (!CollectionUtils.isEmpty(counselorDTO.getSpecializationTags()) ||
!CollectionUtils.isEmpty(counselorDTO.getTherapeuticApproachTags()) ||
!CollectionUtils.isEmpty(counselorDTO.getServiceTypeTags()) ||
StringUtils.hasText(counselorDTO.getGenderFilter())) {
List<CounselorDTO> filteredResult = new ArrayList<>();
for (CounselorDTO dto : result) {
boolean match = true; // 标记当前咨询师是否匹配所有筛选条件
// 擅长领域筛选:检查咨询师的擅长领域是否包含任一指定的标签
if (match && !CollectionUtils.isEmpty(counselorDTO.getSpecializationTags()) && StringUtils.hasText(dto.getSpecialization())) {
match = counselorDTO.getSpecializationTags().stream().anyMatch(tag ->
dto.getSpecialization().contains(tag) // 使用contains进行简单匹配
);
}
// 治疗流派筛选:检查咨询师的治疗流派是否包含任一指定的标签
if (match && !CollectionUtils.isEmpty(counselorDTO.getTherapeuticApproachTags()) && StringUtils.hasText(dto.getTherapeuticApproach())) {
match = counselorDTO.getTherapeuticApproachTags().stream().anyMatch(tag ->
dto.getTherapeuticApproach().contains(tag)
);
}
// 服务类型筛选:检查咨询师的服务类型是否包含任一指定的标签
if (match && !CollectionUtils.isEmpty(counselorDTO.getServiceTypeTags()) && StringUtils.hasText(dto.getServiceTypes())) {
match = counselorDTO.getServiceTypeTags().stream().anyMatch(tag ->
dto.getServiceTypes().contains(tag)
);
}
// 性别筛选:精确匹配性别
if (match && StringUtils.hasText(counselorDTO.getGenderFilter()) && StringUtils.hasText(dto.getGender())) {
match = counselorDTO.getGenderFilter().equals(dto.getGender());
}
// 如果所有条件都匹配,则加入最终结果集
if (match) {
filteredResult.add(dto);
}
}
result = filteredResult; // 更新结果为筛选后的列表
}
return result;
}
接口文档:
**请求URL**:`/api/counselors/search`
**请求方法**:POST
**请求参数**:
```json
{
"keyword": "string", // 搜索关键词(姓名、擅长领域等)
"specializationTags": ["string"], // 擅长领域标签列表
"therapeuticApproachTags": ["string"], // 治疗流派标签列表
"serviceTypeTags": ["string"], // 服务类型标签列表
"genderFilter": "string" // 性别筛选(MALE/FEMALE/UNKNOWN)
}
响应参数:
[
{
"counselorId": 1, // 咨询师ID
"userId": 10, // 用户ID
"realName": "张医生", // 真实姓名
"username": "doctor_zhang", // 用户名
"phone": "13800138000", // 手机号
"email": "zhang@example.com", // 邮箱
"gender": "MALE", // 性别
"age": 35, // 年龄
"qualificationCertificateUrl": "https://example.com/certificate/1", // 资质证书URL
"practiceCertificateUrl": "https://example.com/practice/1", // 执业证书URL
"photoUrl": "https://example.com/photo/1", // 证件照URL
"yearsOfExperience": 10, // 从业年限
"specialization": "[\"焦虑\",\"抑郁\"]", // 擅长领域
"therapeuticApproach": "[\"认知行为\",\"人本主义\"]", // 治疗流派
"introduction": "从事心理咨询工作10年...", // 个人介绍
"consultationFee": 200.00, // 咨询费用
"rating": 4.80, // 平均评分
"totalSessions": 200, // 总咨询次数
"counselorStatus": "APPROVED", // 咨询师状态
"serviceTypes": "[\"文字\",\"语音\",\"视频\"]", // 服务类型
"availableDays": "[\"周一\",\"周三\",\"周五\"]", // 可用日期
"workingHours": "{\"morning\":[\"9:00\",\"12:00\"],\"afternoon\":[\"14:00\",\"18:00\"]}", // 工作时间段
"sessionDurations": "[30,60,90]", // 支持的咨询时长
"maxDailySessions": 5, // 每日最大咨询次数
"createdTime": "2023-01-01T10:00:00", // 创建时间
"updatedTime": "2023-01-01T10:00:00" // 更新时间
}
]
状态码:
- 200:查询成功
浙公网安备 33010602011771号