动态查询
/*动态查询*/
@Test
public void testDynamicSelect(){
Item query = new Item();
// query.setName("手机"); // like '%手机%'
query.setBrand("华为"); // brand = '华为';
LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
qw.like(query.getName()!=null, Item::getName,query.getName());
qw.eq(query.getBrand()!=null, Item::getBrand,query.getBrand());
IPage<Item> page= new Page<>(1,5);
itemMapper.selectPage(page,qw);
System.out.println(page.getRecords());
投影查询 :select groupby having 之类的
@Test
public void testTY(){
QueryWrapper<Item> qw2 = new QueryWrapper<>();
qw2.select("count(*) as nums,category");
qw2.groupBy("category");
qw2.having("count(*)>10000");
List<Map<String, Object>> maps = itemMapper.selectMaps(qw2);
for (Map<String, Object> map : maps) {
System.out.println("分类名称"+map.get("category")+" "+"商品数量"+map.get("nums"));
}
}
实体类映射过程 和细节
雪花生成id 规避两台服务器同时生成自动的风险
例如 :删一下表i提前 ps : 别删这是我自己弄的 !!!!
truncate table `tb_address`
全局配置:
项目中常用到的mybatis 语句 其中分页查询
Page<InteractionQuestion> page = this.lambdaQuery()
.select(tableFieldInfo -> !tableFieldInfo.equals("description"))
// 不添加"description" 这个 字段信息
.eq(ObjectUtils.isNotEmpty(query.getStatus()),InteractionQuestion::getStatus, query.getStatus())
.in(ObjectUtils.isNotEmpty(courseIds),InteractionQuestion::getCourseId, courseIds)
.lt(ObjectUtils.isNotEmpty(query.getBeginTime()),InteractionQuestion::getCreateTime, query.getEndTime())
.ge(ObjectUtils.isNotEmpty(query.getEndTime()),InteractionQuestion::getCreateTime, query.getBeginTime())
.page(query.toMpPageDefaultSortByCreateTimeDesc());
List<InteractionQuestion> recordLists = page.getRecords();
if (CollUtils.isEmpty(recordLists)){
return PageDTO.empty(page); // 返回一个空集合
}
我们可以用this 指代要进行逻辑select mybatis-plus 的业务层级 里面page 可以最后用 .page 主要结构其实是:
this.lambdaQuery().page(query.toMpPageDefaultSortByCreateTimeDesc());
或者用mapper 进行书写 例如: 先 new QueryWrapper
// 本周总学习小节数量
QueryWrapper<LearningRecord> qw = new QueryWrapper<>();
qw.select("lesson_id as id,count(1) as num");
qw.eq("user_id",true);
qw.eq("finish",true);
qw.between("finish_time",begin,end);
qw.groupBy("lesson_id");
List<Map<String, Object>> list1 = recordMapper.selectMaps(qw);
//list1.stream().collect(Collectors.toMap(o -> (Long) o.get("id"), o -> (Long) o.get("num")));
Map<Long, Long> weekFinished = list1.stream().collect(Collectors.toMap(map -> (Long) map.get("id"), map -> (Long) map.get("num")));
Long weekfinals = weekFinished.get("userid");
另外得到的数据找出来之后最好封装成map集合 找起来方便
// 3、将课程集合数据List<CourseSimpleInfoDTO> 封装成 Map<课程ID,课程对象>。并返回
Map<Long, CourseSimpleInfoDTO> courseMap = courseList.stream().collect(Collectors.toMap(o -> o.getId(), o -> o));
"" 直接用querywrapper "" 用select语句操作
// 本周总学习小节数量
QueryWrapper<LearningRecord> qw = new QueryWrapper<>();
qw.select("lesson_id as id,count(1) as num");
qw.eq("user_id",true);
qw.eq("finish",true);
qw.between("finish_time",begin,end);
qw.groupBy("lesson_id");
List<Map<String, Object>> list1 = recordMapper.selectMaps(qw);
//list1.stream().collect(Collectors.toMap(o -> (Long) o.get("id"), o -> (Long) o.get("num")));
Map<Long, Long> weekFinished = list1.stream().collect(Collectors.toMap(map -> (Long) map.get("id"), map -> (Long) map.get("num")));
Long weekfinals = weekFinished.get("userid");
删除: this.remove(qw); qw就使用 LamdaQueryWrapper 进行条件封
QueryWrapper<LearningLesson> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
qw.eq("course_id",courseId);
this.remove(qw);
更新: set(判断条件,放入地方,数据)
// 如果课表存在则 如果当前小节已完成,则更新已经学习小节数+1
// 这里需要使用 setSql方法,指定更新sql如下 learned_sections = learned_sections + 1 比较特表直接利用sql 语句更新操作
boolean update = learningLessonService.lambdaUpdate()
.set(lesson.getLearnedSections() == 0, LearningLesson::getStatus, LessonStatus.LEARNING.getValue())
.set(allFinish, LearningLesson::getStatus, LessonStatus.FINISHED.getValue())
.set(!isFinish,LearningLesson::getLatestSectionId ,dTO.getSectionId())
.setSql(!isFinish, "learned_sections=learning_section+1")
.set(!isFinish,LearningLesson::getLatestLearnTime,dTO.getCommitTime())
.eq(LearningLesson::getId, lesson.getId())
.update();
CardEntity cardEntity = this.baseMapper.selectone(candQuenywrapper);
用baseMappper 当前的操作对象来执行操作