DBTests.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.sf;
  2. import com.sf.mapper.BookChapterMapper;
  3. import com.sf.mapper.BookContentMapper;
  4. import com.sf.mapper.BookInfoMapper;
  5. import com.sf.po.BookChapter;
  6. import com.sf.po.BookContent;
  7. import com.sf.po.BookInfo;
  8. import com.sf.util.EpubUtils;
  9. import com.sf.util.SpiderUtils;
  10. import org.junit.jupiter.api.Test;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.data.util.Pair;
  14. import java.time.LocalDateTime;
  15. import java.util.List;
  16. @SpringBootTest
  17. public class DBTests {
  18. @Autowired
  19. private BookInfoMapper bookInfoMapper;
  20. @Autowired
  21. private BookChapterMapper bookChapterMapper;
  22. @Autowired
  23. private BookContentMapper bookContentMapper;
  24. @Test
  25. public void test() {
  26. // select * from book_content where chapter_id in
  27. //(select id from book_chapter where book_id = '1431630596354977796')
  28. // book_info -> 获得book_id
  29. Pair<String, String> bookPair = Pair.of("长夜难明", """
  30. 该小说讲述了江阳、朱伟等人为了替侯贵平沉冤昭雪,十年来受到重重阻碍但不断收集案件相关证据,最终设局引发社会关注的故事。作者以弱势儿童群体的生存境遇为切口,用“性侵未成年少女”案件将矛头直指偏远地区法治落后的问题,反映出其对寻求司法公正和维护社会秩序的强烈期盼,折射出一个作家的社会责任感
  31. """);
  32. BookInfo bookInfo = toBookInfo(bookPair);
  33. bookInfoMapper.insert(bookInfo);
  34. // System.out.println(bookInfo.getId());
  35. List<Pair<String, String>> pairList = EpubUtils.getContent("epub/长夜难明.epub", "长夜难明");
  36. // try {
  37. // String url = "https://www.qidian.com/book/68223/";
  38. // List<Pair<String, String>> pairAnotherList = SpiderUtils.getContent(url);
  39. // System.out.println();
  40. // } catch (Exception e) {
  41. // throw new RuntimeException(e);
  42. // }
  43. // Long bookId = 1431630596354977799L;
  44. Long bookId = bookInfo.getId();
  45. int allWordCount = 0;
  46. long lastChapterId = 0;
  47. String lastChapterName = "";
  48. for (int i = 0; i < pairList.size(); i++) {
  49. Pair<String, String> pair = pairList.get(i);
  50. int wordCount = pair.getSecond().length();
  51. allWordCount += wordCount;
  52. BookChapter bookChapter = toBookChapter(bookId, i, pair.getFirst(), wordCount);
  53. bookChapterMapper.insert(bookChapter);
  54. BookContent bookContent = BookContent.builder()
  55. .chapterId(bookChapter.getId())
  56. .content(pair.getSecond())
  57. .createTime(LocalDateTime.now())
  58. .updateTime(LocalDateTime.now())
  59. .build();
  60. bookContentMapper.insert(bookContent);
  61. System.out.println();
  62. lastChapterId = bookChapter.getId();
  63. lastChapterName = pair.getFirst();
  64. }
  65. BookInfo newBookInfo = new BookInfo();
  66. newBookInfo.setId(bookId);
  67. newBookInfo.setWordCount(allWordCount);
  68. newBookInfo.setLastChapterId(lastChapterId);
  69. newBookInfo.setLastChapterName(lastChapterName);
  70. newBookInfo.setUpdateTime(LocalDateTime.now());
  71. bookInfoMapper.updateById(newBookInfo);
  72. System.out.println();
  73. }
  74. public BookInfo toBookInfo(Pair<String, String> pair) {
  75. BookInfo bookInfo = new BookInfo();
  76. // 设置作家信息
  77. bookInfo.setAuthorId(1L);
  78. bookInfo.setAuthorName("111");
  79. // 设置 出版物
  80. bookInfo.setWorkDirection((byte) 2);
  81. // 出版频道
  82. bookInfo.setCategoryId(8L);
  83. bookInfo.setCategoryName("出版频道");
  84. bookInfo.setBookName(pair.getFirst());
  85. bookInfo.setBookDesc(pair.getSecond());
  86. bookInfo.setPicUrl("/images/default.gif");
  87. bookInfo.setBookStatus((byte) 1);
  88. bookInfo.setIsVip((byte) 0);
  89. bookInfo.setScore((byte) 0);
  90. bookInfo.setCreateTime(LocalDateTime.now());
  91. bookInfo.setUpdateTime(LocalDateTime.now());
  92. return bookInfo;
  93. }
  94. public BookChapter toBookChapter(Long bookId, Integer chapterNum, String chapterName, Integer wordCount) {
  95. BookChapter bc = BookChapter.builder()
  96. .bookId(bookId)
  97. .chapterNum(chapterNum)
  98. .chapterName(chapterName)
  99. .wordCount(wordCount)
  100. .isVip((byte) 0)
  101. .createTime(LocalDateTime.now())
  102. .updateTime(LocalDateTime.now())
  103. .build();
  104. return bc;
  105. }
  106. }