Przeglądaj źródła

0601 新闻接口实现

Qing 1 rok temu
rodzic
commit
f26334798b

+ 38 - 0
novel-demo/src/main/java/com/sf/controller/BookChapterController.java

@@ -0,0 +1,38 @@
+package com.sf.controller;
+
+import com.sf.dto.RestResp;
+import com.sf.dto.resp.BookChapterRespDto;
+import com.sf.service.IBookChapterService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 小说章节 前端控制器
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-05-25
+ */
+@RestController
+public class BookChapterController {
+
+    @Autowired
+    private IBookChapterService bookChapterService;
+
+    // http://127.0.0.1:8888/api/front/book/chapter/list?bookId=1337872597996539904
+    // @RequestParam注解 用于从请求参数中获取参数
+    @GetMapping("/api/front/book/chapter/list")
+    public RestResp<List<BookChapterRespDto>> listChapters(@RequestParam("bookId") Long bookId){
+        // 根据传进来的参数 bookId 去查询对应书籍的章节信息
+        List<BookChapterRespDto> chapterRespDtos = bookChapterService.getBookChapterByBookId(bookId);
+        return RestResp.ok(chapterRespDtos);
+    }
+
+}

+ 43 - 0
novel-demo/src/main/java/com/sf/controller/BookContentController.java

@@ -0,0 +1,43 @@
+package com.sf.controller;
+
+import com.sf.dto.RestResp;
+import com.sf.dto.resp.BookContentAboutRespDto;
+import com.sf.service.IBookContentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ * 小说内容 前端控制器
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-05-25
+ */
+@RestController
+public class BookContentController {
+
+    @Autowired
+    private IBookContentService bookContentService;
+
+    // http://127.0.0.1:8888/api/front/book/content/1445988184596992002  真正的请求
+    // http://127.0.0.1:8888/api/front/book/content/{chapterId}  请求接口的声明
+    // 当请求的参数在url中时,使用@PathVariable注解 注解会从{}所在的位置提取参数
+    // 编写功能的思路
+    // 明确接口的路径和参数接收方式(controller层) -> 明确接口的返回结果(controller层 -> 明确实现的逻辑(service层)
+
+    // 所有返回结果都是RestResp  不同的是 其中data的类型  所有的data都是dto(data transfer object 数据传输对象)
+    // 当实体类和dto大致相同时  也不能返回实体类本身  因为实体类是对应数据库中的表  而dto是对外提供的接口
+    // 复杂的接口  不会只对应一张表中的数据  而是将多张表中查询的数据进行组合
+    @GetMapping("/api/front/book/content/{chapterId}")
+    public RestResp<BookContentAboutRespDto> getBookContentAbout(
+            @PathVariable("chapterId") Long chapterId) {
+        // controller打包service层的处理结果
+        BookContentAboutRespDto bookContentAbout = bookContentService.getBookContentAbout(chapterId);
+        return RestResp.ok(bookContentAbout);
+    }
+}

+ 3 - 25
novel-demo/src/main/java/com/sf/controller/BookInfoController.java

@@ -24,27 +24,21 @@ import java.util.List;
  * @since 2024-05-25
  * @since 2024-05-25
  */
  */
 @RestController
 @RestController
-@RequestMapping("/api/front/book")
+//@RequestMapping("/api/front/book")
 public class BookInfoController {
 public class BookInfoController {
 
 
     @Autowired
     @Autowired
     private IBookInfoService bookInfoService;
     private IBookInfoService bookInfoService;
 
 
-    @Autowired
-    private IBookChapterService bookChapterService;
-
-    @Autowired
-    private IBookContentService bookContentService;
-
     // http://localhost:8888/api/front/book/list
     // http://localhost:8888/api/front/book/list
-    @GetMapping("/list")
+    @GetMapping("/api/front/book/list")
     public List<BookInfo> list(){
     public List<BookInfo> list(){
         // 不需要编写sql语句  直接进行增删改查操作
         // 不需要编写sql语句  直接进行增删改查操作
         return bookInfoService.list();
         return bookInfoService.list();
     }
     }
 
 
     // http://127.0.0.1:8888/api/front/book/{id}
     // http://127.0.0.1:8888/api/front/book/{id}
-    @GetMapping("/{id}")
+    @GetMapping("/api/front/book/{id}")
     public RestResp<BookInfoRespDto> getBookInfoById(@PathVariable Long id){
     public RestResp<BookInfoRespDto> getBookInfoById(@PathVariable Long id){
         // getById是mybatis plus提供的方法
         // getById是mybatis plus提供的方法
         // 从对应表中 where id = '' 中的数据取出
         // 从对应表中 where id = '' 中的数据取出
@@ -61,20 +55,4 @@ public class BookInfoController {
         BeanUtils.copyProperties(bookInfo,bookInfoRespDto);
         BeanUtils.copyProperties(bookInfo,bookInfoRespDto);
         return RestResp.ok(bookInfoRespDto);
         return RestResp.ok(bookInfoRespDto);
     }
     }
-
-    // http://127.0.0.1:8888/api/front/book/chapter/list?bookId=1337872597996539904
-    @GetMapping("/chapter/list")
-    public RestResp<List<BookChapterRespDto>> listChapters(@RequestParam("bookId") Long bookId){
-        // 根据传进来的参数 bookId 去查询对应书籍的章节信息
-        List<BookChapterRespDto> chapterRespDtos = bookChapterService.getBookChapterByBookId(bookId);
-        return RestResp.ok(chapterRespDtos);
-    }
-
-    // http://127.0.0.1:8888/api/front/book/content/{chapterId}
-    @GetMapping("/content/{chapterId}")
-    public RestResp<BookContentAboutRespDto> getBookContentAbout(
-            @PathVariable("chapterId") Long chapterId) {
-        BookContentAboutRespDto bookContentAbout = bookContentService.getBookContentAbout(chapterId);
-        return RestResp.ok(bookContentAbout);
-    }
 }
 }

+ 34 - 0
novel-demo/src/main/java/com/sf/controller/NewsContentController.java

@@ -0,0 +1,34 @@
+package com.sf.controller;
+
+import com.sf.dto.RestResp;
+import com.sf.dto.resp.NewsInfoRespDto;
+import com.sf.service.INewsContentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ * 新闻内容 前端控制器
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+@RestController
+public class NewsContentController {
+
+    @Autowired
+    private INewsContentService newsContentService;
+
+    @GetMapping("/api/front/news/{id}")
+    public RestResp<NewsInfoRespDto> getNews(
+            @PathVariable Long id) {
+        NewsInfoRespDto newsInfoRespDto = newsContentService.getNews(id);
+        return RestResp.ok(newsInfoRespDto);
+    }
+
+}

+ 34 - 0
novel-demo/src/main/java/com/sf/controller/NewsInfoController.java

@@ -0,0 +1,34 @@
+package com.sf.controller;
+
+import com.sf.dto.RestResp;
+import com.sf.dto.resp.NewsInfoRespDto;
+import com.sf.service.INewsInfoService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 新闻信息 前端控制器
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+@RestController
+public class NewsInfoController {
+
+    @Autowired
+    private INewsInfoService newsInfoService;
+
+    // http://127.0.0.1:8888/api/front/news/latest_list
+    @GetMapping("/api/front/news/latest_list")
+    public RestResp<List<NewsInfoRespDto>> listLatestNews() {
+        List<NewsInfoRespDto> newsInfoRespDtos = newsInfoService.listLatestNews();
+        return RestResp.ok(newsInfoRespDtos);
+    }
+}

+ 3 - 3
novel-demo/src/main/java/com/sf/dto/resp/BookContentAboutRespDto.java

@@ -15,19 +15,19 @@ import lombok.Data;
 public class BookContentAboutRespDto {
 public class BookContentAboutRespDto {
 
 
     /**
     /**
-     * 小说信息
+     * 小说信息  book_info表中获取的信息
      */
      */
     @Schema(description = "小说信息")
     @Schema(description = "小说信息")
     private BookInfoRespDto bookInfo;
     private BookInfoRespDto bookInfo;
 
 
     /**
     /**
-     * 章节信息
+     * 章节信息 book_chapter表中获取的信息
      */
      */
     @Schema(description = "章节信息")
     @Schema(description = "章节信息")
     private BookChapterRespDto chapterInfo;
     private BookChapterRespDto chapterInfo;
 
 
     /**
     /**
-     * 章节内容
+     * 章节内容 book_content表中获取的信息
      */
      */
     @Schema(description = "章节内容")
     @Schema(description = "章节内容")
     private String bookContent;
     private String bookContent;

+ 60 - 0
novel-demo/src/main/java/com/sf/dto/resp/NewsInfoRespDto.java

@@ -0,0 +1,60 @@
+package com.sf.dto.resp;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.time.LocalDateTime;
+
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class NewsInfoRespDto {
+
+    /**
+     * ID
+     */
+    @Schema(description = "新闻ID")
+    private Long id;
+
+    /**
+     * 类别ID
+     */
+    @Schema(description = "类别ID")
+    private Long categoryId;
+
+    /**
+     * 类别名
+     */
+    @Schema(description = "类别名")
+    private String categoryName;
+
+    /**
+     * 新闻来源
+     */
+    @Schema(description = "新闻来源")
+    private String sourceName;
+
+    /**
+     * 新闻标题
+     */
+    @Schema(description = "新闻标题")
+    private String title;
+
+    /**
+     * 更新时间
+     */
+    @Schema(description = "更新时间")
+    @JsonFormat(pattern = "yyyy-MM-dd")
+    private LocalDateTime updateTime;
+
+    /**
+     * 新闻内容
+     * */
+    @Schema(description = "新闻内容")
+    private String content;
+}

+ 98 - 0
novel-demo/src/main/java/com/sf/entity/NewsContent.java

@@ -0,0 +1,98 @@
+package com.sf.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ * 新闻内容
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+@TableName("news_content")
+public class NewsContent implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 新闻ID
+     */
+    private Long newsId;
+
+    /**
+     * 新闻内容
+     */
+    private String content;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createTime;
+
+    /**
+     * 更新时间
+     */
+    private LocalDateTime updateTime;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getNewsId() {
+        return newsId;
+    }
+
+    public void setNewsId(Long newsId) {
+        this.newsId = newsId;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public LocalDateTime getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(LocalDateTime createTime) {
+        this.createTime = createTime;
+    }
+
+    public LocalDateTime getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(LocalDateTime updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    @Override
+    public String toString() {
+        return "NewsContent{" +
+            "id = " + id +
+            ", newsId = " + newsId +
+            ", content = " + content +
+            ", createTime = " + createTime +
+            ", updateTime = " + updateTime +
+        "}";
+    }
+}

+ 126 - 0
novel-demo/src/main/java/com/sf/entity/NewsInfo.java

@@ -0,0 +1,126 @@
+package com.sf.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ * 新闻信息
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+@TableName("news_info")
+public class NewsInfo implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 类别ID
+     */
+    private Long categoryId;
+
+    /**
+     * 类别名
+     */
+    private String categoryName;
+
+    /**
+     * 新闻来源
+     */
+    private String sourceName;
+
+    /**
+     * 新闻标题
+     */
+    private String title;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createTime;
+
+    /**
+     * 更新时间
+     */
+    private LocalDateTime updateTime;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getCategoryId() {
+        return categoryId;
+    }
+
+    public void setCategoryId(Long categoryId) {
+        this.categoryId = categoryId;
+    }
+
+    public String getCategoryName() {
+        return categoryName;
+    }
+
+    public void setCategoryName(String categoryName) {
+        this.categoryName = categoryName;
+    }
+
+    public String getSourceName() {
+        return sourceName;
+    }
+
+    public void setSourceName(String sourceName) {
+        this.sourceName = sourceName;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public LocalDateTime getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(LocalDateTime createTime) {
+        this.createTime = createTime;
+    }
+
+    public LocalDateTime getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(LocalDateTime updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    @Override
+    public String toString() {
+        return "NewsInfo{" +
+            "id = " + id +
+            ", categoryId = " + categoryId +
+            ", categoryName = " + categoryName +
+            ", sourceName = " + sourceName +
+            ", title = " + title +
+            ", createTime = " + createTime +
+            ", updateTime = " + updateTime +
+        "}";
+    }
+}

+ 16 - 0
novel-demo/src/main/java/com/sf/mapper/NewsContentMapper.java

@@ -0,0 +1,16 @@
+package com.sf.mapper;
+
+import com.sf.entity.NewsContent;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ * 新闻内容 Mapper 接口
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+public interface NewsContentMapper extends BaseMapper<NewsContent> {
+
+}

+ 16 - 0
novel-demo/src/main/java/com/sf/mapper/NewsInfoMapper.java

@@ -0,0 +1,16 @@
+package com.sf.mapper;
+
+import com.sf.entity.NewsInfo;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ * 新闻信息 Mapper 接口
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+public interface NewsInfoMapper extends BaseMapper<NewsInfo> {
+
+}

+ 17 - 0
novel-demo/src/main/java/com/sf/service/INewsContentService.java

@@ -0,0 +1,17 @@
+package com.sf.service;
+
+import com.sf.dto.resp.NewsInfoRespDto;
+import com.sf.entity.NewsContent;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ * 新闻内容 服务类
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+public interface INewsContentService extends IService<NewsContent> {
+    NewsInfoRespDto getNews(Long id);
+}

+ 19 - 0
novel-demo/src/main/java/com/sf/service/INewsInfoService.java

@@ -0,0 +1,19 @@
+package com.sf.service;
+
+import com.sf.dto.resp.NewsInfoRespDto;
+import com.sf.entity.NewsInfo;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 新闻信息 服务类
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+public interface INewsInfoService extends IService<NewsInfo> {
+    List<NewsInfoRespDto> listLatestNews();
+}

+ 17 - 11
novel-demo/src/main/java/com/sf/service/impl/BookContentServiceImpl.java

@@ -39,19 +39,24 @@ public class BookContentServiceImpl extends ServiceImpl<BookContentMapper, BookC
 
 
     @Override
     @Override
     public BookContentAboutRespDto getBookContentAbout(Long chapterId) {
     public BookContentAboutRespDto getBookContentAbout(Long chapterId) {
+        // 在service的具体实现里  先写sql 然后根据sql编写mybatis plus的代码
         // 查询章节信息
         // 查询章节信息
         // select * from book_chapter where id = 'chapterId';
         // select * from book_chapter where id = 'chapterId';
         BookChapter bookChapter = bookChapterMapper.selectById(chapterId);
         BookChapter bookChapter = bookChapterMapper.selectById(chapterId);
-        BookChapterRespDto bookChapterDto = BookChapterRespDto.builder()
-                .id(chapterId)
-                .bookId(bookChapter.getBookId())
-                .chapterNum(bookChapter.getChapterNum().intValue())
-                .chapterName(bookChapter.getChapterName())
-                .chapterWordCount(bookChapter.getWordCount())
-                .chapterUpdateTime(bookChapter.getUpdateTime())
-                .isVip(bookChapter.getIsVip().intValue())
-                .build();
-//        BeanUtils.copyProperties(bookChapter, bookChapterDto);
+        BookChapterRespDto bookChapterDto = BookChapterRespDto.builder().build();
+//        BookChapterRespDto bookChapterDto = BookChapterRespDto.builder()
+//                .id(chapterId)
+//                .bookId(bookChapter.getBookId())
+//                .chapterNum(bookChapter.getChapterNum().intValue())
+//                .chapterName(bookChapter.getChapterName())
+//                .chapterWordCount(bookChapter.getWordCount())
+//                .chapterUpdateTime(bookChapter.getUpdateTime())
+//                .isVip(bookChapter.getIsVip().intValue())
+//                .build();
+        // 使用反射把bookChapter对象的属性值取出  赋值给bookChapterDto对象
+        // 遍历bookChapterDto对象的属性  如果bookChapter对象中有同名属性  取出赋值给bookChapterDto对象
+        BeanUtils.copyProperties(bookChapter, bookChapterDto);
+        bookChapterDto.setChapterUpdateTime(bookChapter.getUpdateTime());
 
 
         // 查询章节内容
         // 查询章节内容
         // select * from book_content where chapter_id = 'chapterId' limit 1;
         // select * from book_content where chapter_id = 'chapterId' limit 1;
@@ -85,10 +90,11 @@ public class BookContentServiceImpl extends ServiceImpl<BookContentMapper, BookC
 //                .build();
 //                .build();
 
 
         // 组装数据并返回
         // 组装数据并返回
-        return BookContentAboutRespDto.builder()
+        BookContentAboutRespDto result = BookContentAboutRespDto.builder()
                 .bookInfo(bookInfoDto)
                 .bookInfo(bookInfoDto)
                 .chapterInfo(bookChapterDto)
                 .chapterInfo(bookChapterDto)
                 .bookContent(content)
                 .bookContent(content)
                 .build();
                 .build();
+        return result;
     }
     }
 }
 }

+ 44 - 0
novel-demo/src/main/java/com/sf/service/impl/NewsContentServiceImpl.java

@@ -0,0 +1,44 @@
+package com.sf.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.sf.dto.resp.NewsInfoRespDto;
+import com.sf.entity.NewsContent;
+import com.sf.entity.NewsInfo;
+import com.sf.mapper.NewsContentMapper;
+import com.sf.mapper.NewsInfoMapper;
+import com.sf.service.INewsContentService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ * 新闻内容 服务实现类
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+@Service
+public class NewsContentServiceImpl extends ServiceImpl<NewsContentMapper, NewsContent> implements INewsContentService {
+
+    @Autowired
+    private NewsInfoMapper newsInfoMapper;
+    @Autowired
+    private NewsContentMapper newsContentMapper;
+
+    @Override
+    public NewsInfoRespDto getNews(Long id) {
+        NewsInfo newsInfo = newsInfoMapper.selectById(id);
+        LambdaQueryWrapper<NewsContent> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(NewsContent::getId, id)
+                .last("limit 1");
+        NewsContent newsContent = newsContentMapper.selectOne(queryWrapper);
+        return NewsInfoRespDto.builder()
+                .title(newsInfo.getTitle())
+                .sourceName(newsInfo.getSourceName())
+                .updateTime(newsInfo.getUpdateTime())
+                .content(newsContent.getContent())
+                .build();
+    }
+}

+ 43 - 0
novel-demo/src/main/java/com/sf/service/impl/NewsInfoServiceImpl.java

@@ -0,0 +1,43 @@
+package com.sf.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.sf.dto.resp.NewsInfoRespDto;
+import com.sf.entity.NewsInfo;
+import com.sf.mapper.NewsInfoMapper;
+import com.sf.service.INewsInfoService;
+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.List;
+
+/**
+ * <p>
+ * 新闻信息 服务实现类
+ * </p>
+ *
+ * @author baomidou
+ * @since 2024-06-01
+ */
+@Service
+public class NewsInfoServiceImpl extends ServiceImpl<NewsInfoMapper, NewsInfo> implements INewsInfoService {
+
+    @Autowired
+    private NewsInfoMapper newsInfoMapper;
+
+    @Override
+    public List<NewsInfoRespDto> listLatestNews() {
+        // 从新闻信息表中查询出最新发布的两条新闻
+        LambdaQueryWrapper<NewsInfo> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.orderByDesc(NewsInfo::getCreateTime).last("limit 2");
+        List<NewsInfo> newsInfos = newsInfoMapper.selectList(queryWrapper);
+        // map是映射处理 将list中每个newsInfo对象转换为NewsInfoRespDto对象
+        // toList是将stream流的处理结果接收为list
+        return newsInfos.stream().map(newsInfo -> {
+            NewsInfoRespDto newsInfoRespDto = NewsInfoRespDto.builder().build();
+            BeanUtils.copyProperties(newsInfo, newsInfoRespDto);
+            return newsInfoRespDto;
+        }).toList();
+    }
+}

+ 3 - 1
novel-demo/src/main/java/com/sf/util/GeneUtils.java

@@ -18,7 +18,9 @@ public class GeneUtils {
 //        list.add("home_book");
 //        list.add("home_book");
 //        list.add("book_info");  // 书籍表
 //        list.add("book_info");  // 书籍表
 //        list.add("book_chapter"); // 书籍章节表
 //        list.add("book_chapter"); // 书籍章节表
-        list.add("book_content"); // 章节内容表
+//        list.add("book_content"); // 章节内容表
+        list.add("news_info"); // 新闻列表
+        list.add("news_content"); // 新闻内容表
 
 
         // 快速生成器
         // 快速生成器
         FastAutoGenerator.create("jdbc:mysql://localhost:3306/novel-cloud?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai",
         FastAutoGenerator.create("jdbc:mysql://localhost:3306/novel-cloud?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai",

+ 5 - 0
novel-demo/src/main/resources/mapper/NewsContentMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sf.mapper.NewsContentMapper">
+
+</mapper>

+ 5 - 0
novel-demo/src/main/resources/mapper/NewsInfoMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sf.mapper.NewsInfoMapper">
+
+</mapper>