1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package com.sf.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.sf.dto.resp.BookChapterRespDto;
- import com.sf.entity.BookChapter;
- import com.sf.mapper.BookChapterMapper;
- import com.sf.service.IBookChapterService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * <p>
- * 小说章节 服务实现类
- * </p>
- *
- * @author baomidou
- * @since 2024-05-25
- */
- @Service
- public class BookChapterServiceImpl extends ServiceImpl<BookChapterMapper, BookChapter> implements IBookChapterService {
- @Autowired
- private BookChapterMapper bookChapterMapper;
- @Override
- public List<BookChapterRespDto> getBookChapterByBookId(Long bookId) {
- // select * from book_chapter where book_id = '';
- LambdaQueryWrapper<BookChapter> queryWrapper = new LambdaQueryWrapper();
- queryWrapper.eq(BookChapter::getBookId, bookId);
- List<BookChapter> bookChapters = bookChapterMapper.selectList(queryWrapper);
- // 使用普通的方式转换成DTO
- List<BookChapterRespDto> chapterRespDtos = new ArrayList<>();
- bookChapters.forEach(bookChapter -> {
- BookChapterRespDto chapterRespDto = new BookChapterRespDto();
- BeanUtils.copyProperties(bookChapter, chapterRespDto);
- // 补充不同名的属性
- chapterRespDto.setChapterUpdateTime(bookChapter.getUpdateTime());
- chapterRespDtos.add(chapterRespDto);
- });
- // stream流的写法
- List<BookChapterRespDto> chapterRespDtos2 = bookChapters.stream().map(bookChapter -> {
- return BookChapterRespDto.builder()
- .id(bookChapter.getId())
- .bookId(bookChapter.getBookId())
- .chapterName(bookChapter.getChapterName())
- .chapterNum(bookChapter.getChapterNum())
- .chapterWordCount(bookChapter.getWordCount())
- .chapterUpdateTime(bookChapter.getUpdateTime())
- .isVip(bookChapter.getIsVip().intValue())
- .build();
- }).toList();
- return chapterRespDtos2;
- }
- }
|