|
@@ -0,0 +1,128 @@
|
|
|
+package com.sf;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.sf.mapper.ChapterContentMapper;
|
|
|
+import com.sf.mapper.ChapterMapper;
|
|
|
+import com.sf.mapper.FictionMapper;
|
|
|
+import com.sf.po.Chapter;
|
|
|
+import com.sf.po.Fiction;
|
|
|
+import com.sf.service.IFictionService;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+public class MybatisPlusTests {
|
|
|
+
|
|
|
+ // @Autowired
|
|
|
+// private FictionMapper fictionMapper;
|
|
|
+ @Autowired
|
|
|
+ private IFictionService fictionService;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test() {
|
|
|
+ List<Fiction> fictionList = fictionMapper.selectList(null);
|
|
|
+// System.out.println(fictionList);
|
|
|
+// fictionList.forEach(System.out::println);
|
|
|
+
|
|
|
+// System.out.println(fictionMapper.selectById(6));
|
|
|
+ System.out.println("=============");
|
|
|
+ System.out.println(fictionMapper.selectCount(null));
|
|
|
+
|
|
|
+ QueryWrapper wrapper = new QueryWrapper();
|
|
|
+// wrapper.eq("id",6); // where id = 6
|
|
|
+ wrapper.gt("id", 6); // where id > 6
|
|
|
+// wrapper.like("fiction_name","我"); // where fiction_name like "%我%"
|
|
|
+ wrapper.lt("id", 16); // where id < 16
|
|
|
+ List<Fiction> selected = fictionMapper.selectList(wrapper);
|
|
|
+ selected.forEach(System.out::println);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FictionMapper fictionMapper;
|
|
|
+ @Autowired
|
|
|
+ private ChapterMapper chapterMapper;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testChapter() {
|
|
|
+ List<Fiction> fictions = fictionMapper.selectList(null);
|
|
|
+
|
|
|
+ int randomFiction = randomInt(fictions.size());
|
|
|
+ Fiction fiction = fictions.get(randomFiction);
|
|
|
+ System.out.println("随机书籍名字为:" + fiction.getFictionName());
|
|
|
+
|
|
|
+ // 根据书籍id 查询出所有章节
|
|
|
+ QueryWrapper queryWrapper = new QueryWrapper();
|
|
|
+ queryWrapper.eq("fiction_id", fiction.getId());
|
|
|
+ queryWrapper.orderByAsc("sort");
|
|
|
+ List<Chapter> chapters = chapterMapper.selectList(queryWrapper);
|
|
|
+ // 打印全部章节的名字
|
|
|
+// for (Chapter chapter : chapters) {
|
|
|
+// System.out.println(chapter.getChapterTitle());
|
|
|
+// }
|
|
|
+ chapters.forEach(chapter -> {
|
|
|
+ System.out.println(chapter.getChapterTitle());
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println("=============");
|
|
|
+ System.out.println("书籍《" + fiction.getFictionName() + "》的章节数为:" + chapters.size());
|
|
|
+ int randomChapter = randomInt(chapters.size());
|
|
|
+ Chapter chapter = chapters.get(randomChapter);
|
|
|
+ System.out.println("随机章节名字为:" + chapter.getChapterTitle());
|
|
|
+
|
|
|
+ if (randomChapter == 0) {
|
|
|
+ System.out.println("没有上一章");
|
|
|
+ System.out.println("下一章为:" + chapters.get(randomChapter + 1).getChapterTitle());
|
|
|
+ } else if (randomChapter == chapters.size() - 1) {
|
|
|
+ System.out.println("没有下一章");
|
|
|
+ System.out.println("上一章为:" + chapters.get(randomChapter - 1).getChapterTitle());
|
|
|
+ } else {
|
|
|
+ System.out.println("上一章为:" + chapters.get(randomChapter - 1).getChapterTitle());
|
|
|
+ System.out.println("下一章为:" + chapters.get(randomChapter + 1).getChapterTitle());
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("=============");
|
|
|
+ // 根据章节id 查找上一章和下一章的id
|
|
|
+ int current = chapter.getSort();
|
|
|
+ int pre = current - 1;
|
|
|
+ int next = current + 1;
|
|
|
+ QueryWrapper chapterWrapper = new QueryWrapper();
|
|
|
+ chapterWrapper.eq("fiction_id", chapter.getFictionId());
|
|
|
+ chapterWrapper.eq("sort", pre);
|
|
|
+ Chapter preChapter = chapterMapper.selectOne(chapterWrapper);
|
|
|
+
|
|
|
+ QueryWrapper chapterWrapper1 = new QueryWrapper();
|
|
|
+ chapterWrapper1.eq("fiction_id", chapter.getFictionId());
|
|
|
+ chapterWrapper1.eq("sort", next);
|
|
|
+ Chapter nextChapter = chapterMapper.selectOne(chapterWrapper1);
|
|
|
+ System.out.println("上一章为:" + preChapter.getChapterTitle());
|
|
|
+ System.out.println("下一章为:" + nextChapter.getChapterTitle());
|
|
|
+ }
|
|
|
+
|
|
|
+ public int randomInt(int size) {
|
|
|
+ return (int) (Math.random() * size);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testViews() {
|
|
|
+ System.out.println(fictionMapper.selectById(6));
|
|
|
+ fictionMapper.addViews(6);
|
|
|
+ System.out.println("================");
|
|
|
+ System.out.println(fictionMapper.selectById(6));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testPage() {
|
|
|
+ IPage<Fiction> fictionIPage = new Page<>();
|
|
|
+ fictionIPage.setCurrent(2);
|
|
|
+ fictionIPage.setSize(2);
|
|
|
+ IPage<Fiction> selectPage = fictionMapper.selectPage(fictionIPage, null);
|
|
|
+ List<Fiction> records = selectPage.getRecords();
|
|
|
+ records.forEach(System.out::println);
|
|
|
+ }
|
|
|
+}
|