mirror of
https://github.com/201206030/novel-plus.git
synced 2025-06-24 04:46:37 +00:00
增加小说内容TXT文本存储方案(一行配置切换)
This commit is contained in:
@ -5,16 +5,14 @@ import com.java2nb.novel.core.bean.PageBean;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.utils.ThreadLocalUtil;
|
||||
import com.java2nb.novel.entity.*;
|
||||
import com.java2nb.novel.service.AuthorService;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.service.NewsService;
|
||||
import com.java2nb.novel.service.UserService;
|
||||
import com.java2nb.novel.service.*;
|
||||
import com.java2nb.novel.vo.BookCommentVO;
|
||||
import com.java2nb.novel.vo.BookSettingVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -36,6 +34,9 @@ import java.util.concurrent.ThreadPoolExecutor;
|
||||
@Controller
|
||||
public class PageController extends BaseController {
|
||||
|
||||
@Value("${txt.save.path}")
|
||||
private String fileSavePath;
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
private final NewsService newsService;
|
||||
@ -44,6 +45,8 @@ public class PageController extends BaseController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final BookContentService bookContentService;
|
||||
|
||||
private final ThreadPoolExecutor threadPoolExecutor;
|
||||
|
||||
|
||||
@ -174,7 +177,7 @@ public class PageController extends BaseController {
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RequestMapping("/book/{bookId}/{bookIndexId}.html")
|
||||
public String indexList(@PathVariable("bookId") Long bookId, @PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, Model model) {
|
||||
public String bookContent(@PathVariable("bookId") Long bookId, @PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, Model model) {
|
||||
//加载小说基本信息线程
|
||||
CompletableFuture<Book> bookCompletableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
//查询书籍
|
||||
@ -210,7 +213,7 @@ public class PageController extends BaseController {
|
||||
//加载小说内容信息线程
|
||||
CompletableFuture<BookContent> bookContentCompletableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
//查询内容
|
||||
BookContent bookContent = bookService.queryBookContent(bookIndexId);
|
||||
BookContent bookContent = bookContentService.queryBookContent(bookId, bookIndexId);
|
||||
log.debug("加载小说内容信息线程结束");
|
||||
return bookContent;
|
||||
}, threadPoolExecutor);
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.java2nb.novel.service;
|
||||
|
||||
import com.java2nb.novel.entity.BookContent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BookContentService {
|
||||
|
||||
BookContent queryBookContent(Long bookId, Long bookIndexId);
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.java2nb.novel.entity.BookContent;
|
||||
import com.java2nb.novel.mapper.BookContentDynamicSqlSupport;
|
||||
import com.java2nb.novel.mapper.BookContentMapper;
|
||||
import com.java2nb.novel.service.BookContentService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.bookContent;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.update;
|
||||
import static org.mybatis.dynamic.sql.select.SelectDSL.select;
|
||||
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "txt.save", name = "storage", havingValue = "db")
|
||||
public class DbBookContentServiceImpl implements BookContentService {
|
||||
|
||||
private final BookContentMapper bookContentMapper;
|
||||
|
||||
@Override
|
||||
public BookContent queryBookContent(Long bookId, Long bookIndexId) {
|
||||
SelectStatementProvider selectStatement = select(BookContentDynamicSqlSupport.id, BookContentDynamicSqlSupport.content)
|
||||
.from(bookContent)
|
||||
.where(BookContentDynamicSqlSupport.indexId, isEqualTo(bookIndexId))
|
||||
.limit(1)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookContentMapper.selectMany(selectStatement).get(0);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.java2nb.novel.core.utils.FileUtil;
|
||||
import com.java2nb.novel.entity.BookContent;
|
||||
import com.java2nb.novel.service.BookContentService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "txt.save", name = "storage", havingValue = "file")
|
||||
public class FileBookContentServiceImpl implements BookContentService {
|
||||
|
||||
@Value("${txt.save.path}")
|
||||
private String fileSavePath;
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public BookContent queryBookContent(Long bookId, Long bookIndexId) {
|
||||
BufferedReader in = new BufferedReader(new FileReader(fileSavePath + "/" + bookId + "/" + bookIndexId + ".txt"));
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String str;
|
||||
while ((str = in.readLine()) != null) {
|
||||
sb.append(str);
|
||||
}
|
||||
in.close();
|
||||
return new BookContent() {{
|
||||
setIndexId(bookIndexId);
|
||||
setContent(sb.toString());
|
||||
}};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user