mybatis-plus 实现多条件映射关系查询(范围查询)
Posted Yan Yang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-plus 实现多条件映射关系查询(范围查询)相关的知识,希望对你有一定的参考价值。
1. 案例效果图
根据时间、人均花费、出行天数查询
2. 接收前台传过来的参数
package com.yy.springboot.query;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class TravelQuery extends QueryObject{
private String orderBy = "1";
private Integer travelTimeType = -1;
private Integer consumeType = -1;
private Integer dayType = -1;
}
3. 设计条件类
package com.yy.springboot.query;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class TravelCondition {
private Integer min;
private Integer max;
public static final Map<Integer, TravelCondition> DAY_MAP = new HashMap<>(); // 天数
public static final Map<Integer, TravelCondition> CONSUME_MAP = new HashMap<>(); // 花费金额
public static final Map<Integer, TravelCondition> TIME_MAP = new HashMap<>(); // 月份
static {
TIME_MAP.put(1, new TravelCondition(1, 2));
TIME_MAP.put(2, new TravelCondition(3, 4));
TIME_MAP.put(3, new TravelCondition(5, 6));
TIME_MAP.put(4, new TravelCondition(7, 8));
TIME_MAP.put(5, new TravelCondition(9, 10));
TIME_MAP.put(6, new TravelCondition(11, 12));
CONSUME_MAP.put(1, new TravelCondition(0, 999));
CONSUME_MAP.put(2, new TravelCondition(1000, 6000));
CONSUME_MAP.put(3, new TravelCondition(6000, 20000));
CONSUME_MAP.put(4, new TravelCondition(20000, Integer.MAX_VALUE));
DAY_MAP.put(1, new TravelCondition(0, 3));
DAY_MAP.put(2, new TravelCondition(4, 7));
DAY_MAP.put(3, new TravelCondition(8, 14));
DAY_MAP.put(4, new TravelCondition(15, Integer.MAX_VALUE));
}
public TravelCondition(Integer min, Integer max) {
this.min = min;
this.max = max;
}
}
4. Service 业务层查询方法
@Service
@Transactional
public class TravelServiceImpl extends ServiceImpl<TravelMapper, Travel> implements ITravelService {
@Autowired
private IUserInfoService userInfoService;
@Override
public IPage<Travel> queryPage(TravelQuery qo) {
IPage<Travel> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
QueryWrapper<Travel> wrapper = Wrappers.<Travel>query();
// 判断排序是否为空
if (qo.getOrderBy() != null) {
wrapper.orderByDesc(qo.getOrderBy().equals("1") ? "create_time" : qo.getOrderBy());
}
// 出发时间
TravelCondition time = TravelCondition.TIME_MAP.get(qo.getTravelTimeType());
if (time != null) {
wrapper.ge("Month(travel_time)", time.getMin())
.le("Month(travel_time)", time.getMax());
}
// 人均花费
TravelCondition consume = TravelCondition.CONSUME_MAP.get(qo.getConsumeType());
if (consume != null) {
wrapper.ge("avg_consume", consume.getMin())
.le("avg_consume", consume.getMax());
}
// 出行天数
TravelCondition day = TravelCondition.DAY_MAP.get(qo.getDayType());
if (day != null) {
wrapper.ge("day", day.getMin())
.le("day", day.getMax());
}
IPage<Travel> page1 = super.page(page, wrapper);
// 把用户设置到游记详情中去
for (Travel travel : page1.getRecords()) {
travel.setAuthor(userInfoService.getById(travel.getAuthorId()));
}
return page1;
}
}
以上是关于mybatis-plus 实现多条件映射关系查询(范围查询)的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis-Plus:条件构造器(allEq基本比较操作模糊查询排序逻辑查询select)