@SpringBootTest(classes = LearningApplication.class)//找不到启动类或引导类
public class MPTest {
@Autowired
private ILearningLessonService lessonService;
@Test
public void test1(){
Page<LearningLesson> page = new Page<>(1,2);//查询第一页的数据,每一页有2条。
//在page中排序:
List<OrderItem> itemList = new ArrayList<>();
OrderItem orderItem = new OrderItem();
orderItem.setAsc(false); //升序还是降序
orderItem.setColumn("latest_learn_time");//按照什么字段排序
itemList.add(orderItem);
page.addOrder(itemList);
LambdaQueryWrapper<LearningLesson> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(LearningLesson::getUserId,"2");
//wrapper.orderByDesc(LearningLesson::getLatestLearnTime); //在wrapper中排序,按照什么字段排序
lessonService.page(page,wrapper );
//打印结果
System.out.println("total:"+page.getTotal());//总条数
System.out.println("pages:"+page.getPages());//总页数
List<LearningLesson> records = page.getRecords();
for (LearningLesson record : records) {
System.out.println(record); //打印出来每一条信息
}
}
@Test
public void test2(){
Page<LearningLesson> page = new Page<>(1,2);//查询第一页的数据,每一页有2条。
//排序:
List<OrderItem> itemList = new ArrayList<>();
OrderItem orderItem = new OrderItem();
orderItem.setAsc(false); //升序还是降序
orderItem.setColumn("latest_learn_time");//按照什么字段排序
itemList.add(orderItem);
page.addOrder(itemList);
lessonService.lambdaQuery().eq(LearningLesson::getUserId,"2")
.page(page);
//打印结果
System.out.println("total:"+page.getTotal());//总条数
System.out.println("pages:"+page.getPages());//总页数
List<LearningLesson> records = page.getRecords();
for (LearningLesson record : records) {
System.out.println(record); //打印出来每一条信息
}
}
@Test
public void test3(){
PageQuery query = new PageQuery();
query.setPageNo(1);
query.setPageSize(2);
query.setIsAsc(false);
query.setSortBy("latest_learn_time");
Page<LearningLesson> page = lessonService.lambdaQuery()
.eq(LearningLesson::getUserId, "2")
.page(query.toMpPage("latest_learn_time", false));
//打印结果
System.out.println("total:"+page.getTotal());//总条数
System.out.println("pages:"+page.getPages());//总页数
List<LearningLesson> records = page.getRecords();
for (LearningLesson record : records) {
System.out.println(record); //打印出来每一条信息
}
}
@Test //复习stream流
public void test4(){
List<LearningLesson> list = new ArrayList<>();
LearningLesson lesson1 = new LearningLesson();
lesson1.setId(1L);
lesson1.setCourseId(1L);
LearningLesson lesson2 = new LearningLesson();
lesson2.setId(2L);
lesson2.setCourseId(2L);
list.add(lesson1);
list.add(lesson2);
//使用stream流 获取集合中所有的LearningLesson的id值
List<Long> ids = list.stream().map(LearningLesson::getId).collect(Collectors.toList());
Set<Long> ids2 = list.stream().map(LearningLesson::getId).collect(Collectors.toSet());
System.out.println(ids);
System.out.println(ids2);
//使用stream流 对list转map <id ,LearningLesson>
Map<Long, LearningLesson> collect = list.stream().collect(Collectors.toMap(LearningLesson::getId, c -> c));
System.out.println(collect);
}
}
//排除查询某个字段的方法:
Page<InteractionQuestion> page = this.lambdaQuery()
//排除查询某个字段的方法:
//方法1: .select(InteractionQuestion::getId,InteractionQuestion::getTitle,InteractionQuestion::getAnswerTimes)
//方法2:使用接口Predicate:
.select(InteractionQuestion.class, new Predicate<TableFieldInfo>() {
@Override
public boolean test(TableFieldInfo tableFieldInfo) {
// tableFieldInfo.getProperty()//获取InteractionQuestion实体类属性名称
return !tableFieldInfo.getProperty().equals("description"); //取反就是排除
}
})
.eq(InteractionQuestion::getCourseId, query.getCourseId())
.eq(query.getOnlyMine(), InteractionQuestion::getUserId, userId)
.eq(query.getSectionId() != null, InteractionQuestion::getSectionId, query.getSectionId())
.eq(InteractionQuestion::getHidden, false)
.page(query.toMpPageDefaultSortByCreateTimeDesc());