Răsfoiți Sursa

0124 mybatis-plus

Qing 1 an în urmă
părinte
comite
3b32909cdc
37 a modificat fișierele cu 1054 adăugiri și 1 ștergeri
  1. 52 1
      springboot-demo/pom.xml
  2. 4 0
      springboot-demo/src/main/java/com/sf/DemoApplication.java
  3. 24 0
      springboot-demo/src/main/java/com/sf/config/MybatisPlusConfig.java
  4. 18 0
      springboot-demo/src/main/java/com/sf/controller/ChapterContentController.java
  5. 18 0
      springboot-demo/src/main/java/com/sf/controller/ChapterController.java
  6. 33 0
      springboot-demo/src/main/java/com/sf/controller/FictionController.java
  7. 18 0
      springboot-demo/src/main/java/com/sf/controller/FictionShelfController.java
  8. 27 0
      springboot-demo/src/main/java/com/sf/mapper/AuthorMapper.java
  9. 16 0
      springboot-demo/src/main/java/com/sf/mapper/ChapterContentMapper.java
  10. 16 0
      springboot-demo/src/main/java/com/sf/mapper/ChapterMapper.java
  11. 21 0
      springboot-demo/src/main/java/com/sf/mapper/FictionMapper.java
  12. 16 0
      springboot-demo/src/main/java/com/sf/mapper/FictionShelfMapper.java
  13. 32 0
      springboot-demo/src/main/java/com/sf/po/Author.java
  14. 103 0
      springboot-demo/src/main/java/com/sf/po/Chapter.java
  15. 49 0
      springboot-demo/src/main/java/com/sf/po/ChapterContent.java
  16. 47 0
      springboot-demo/src/main/java/com/sf/po/Fiction.java
  17. 71 0
      springboot-demo/src/main/java/com/sf/po/FictionShelf.java
  18. 16 0
      springboot-demo/src/main/java/com/sf/service/IChapterContentService.java
  19. 16 0
      springboot-demo/src/main/java/com/sf/service/IChapterService.java
  20. 16 0
      springboot-demo/src/main/java/com/sf/service/IFictionService.java
  21. 16 0
      springboot-demo/src/main/java/com/sf/service/IFictionShelfService.java
  22. 20 0
      springboot-demo/src/main/java/com/sf/service/impl/ChapterContentServiceImpl.java
  23. 20 0
      springboot-demo/src/main/java/com/sf/service/impl/ChapterServiceImpl.java
  24. 20 0
      springboot-demo/src/main/java/com/sf/service/impl/FictionServiceImpl.java
  25. 20 0
      springboot-demo/src/main/java/com/sf/service/impl/FictionShelfServiceImpl.java
  26. 66 0
      springboot-demo/src/main/java/com/sf/utils/GeneUtils.java
  27. 14 0
      springboot-demo/src/main/resources/application.properties
  28. 5 0
      springboot-demo/src/main/resources/mapper/ChapterContentMapper.xml
  29. 5 0
      springboot-demo/src/main/resources/mapper/ChapterMapper.xml
  30. 7 0
      springboot-demo/src/main/resources/mapper/FictionMapper.xml
  31. 5 0
      springboot-demo/src/main/resources/mapper/FictionShelfMapper.xml
  32. 20 0
      springboot-demo/src/main/resources/mapper/authorMapper.xml
  33. 17 0
      springboot-demo/src/main/resources/mybatis-config.xml
  34. 14 0
      springboot-demo/src/main/resources/thymeleaf/entity.java
  35. 8 0
      springboot-demo/src/test/java/com/sf/DemoApplicationTests.java
  36. 128 0
      springboot-demo/src/test/java/com/sf/MybatisPlusTests.java
  37. 56 0
      springboot-demo/src/test/java/com/sf/ThymeleafTests.java

+ 52 - 1
springboot-demo/pom.xml

@@ -6,7 +6,10 @@
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
-        <version>3.2.2</version>
+        <version>3.2.0</version>
+        <!-- 3.2.0 对应 spring 6.1.1 -->
+        <!-- 3.2.1 对应 spring 6.1.2 -->
+        <!-- 3.2.2 对应 spring 6.1.3 -->
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
 
@@ -45,6 +48,54 @@
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-thymeleaf</artifactId>
+            <!-- 如果是springboot整合别的框架 名字叫做 spring-boot-starter-* -->
+            <!-- 如果是别的框架整合springboot 名字叫做 *-spring-boot-starter -->
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <!-- 在引入依赖时 如果父依赖的父依赖没有管理版本号 则需要自己引入 -->
+            <!-- 当别的框架整合springboot时  往往需要自己管理版本 -->
+            <version>3.0.3</version>
+        </dependency>
+        <!-- 数据库驱动 这个依赖有版本号  对应8.1.0-->
+        <dependency>
+            <groupId>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+<!--            区别于原来使用的 mysql-connector-java -->
+<!--            <version>8.2.0</version>-->
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.30</version>
+        </dependency>
+
+        <!-- 使用thymeleaf生成代码 -->
+        <dependency>
+            <groupId>ognl</groupId>
+            <artifactId>ognl</artifactId>
+            <version>3.4.2</version>
+        </dependency>
+
+        <!-- 整合mybatis plus -->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+            <version>3.5.4</version>
+        </dependency>
+        <!-- 使用mybatis plus的代码生成工具 使用了模板引擎freemarker -->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-generator</artifactId>
+            <version>3.5.4</version>
+        </dependency>
+        <dependency>
+            <groupId>org.freemarker</groupId>
+            <artifactId>freemarker</artifactId>
+            <version>2.3.32</version>
         </dependency>
 
     </dependencies>

+ 4 - 0
springboot-demo/src/main/java/com/sf/DemoApplication.java

@@ -1,5 +1,6 @@
 package com.sf;
 
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.ComponentScan;
@@ -8,6 +9,9 @@ import org.springframework.context.annotation.ComponentScan;
 // 标志一个类是 springboot的应用程序的主函数入口
 // 默认扫描路径是当前类所在的路径  以及子路径  当前项目中的“com.sf”
 @SpringBootApplication
+//@ComponentScan("com.hello")
+//@ComponentScan("com.sf")
+@MapperScan("com.sf.mapper")   // 如果不希望每次都在Mapper类上增加@Mapper 可以使用包扫描注解
 public class DemoApplication {
 
     public static void main(String[] args) {

+ 24 - 0
springboot-demo/src/main/java/com/sf/config/MybatisPlusConfig.java

@@ -0,0 +1,24 @@
+package com.sf.config;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@MapperScan("com.sf.mapper")
+public class MybatisPlusConfig {
+
+    /**
+     * 添加分页插件
+     */
+    @Bean
+    public MybatisPlusInterceptor mybatisPlusInterceptor() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
+        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
+        return interceptor;
+    }
+}

+ 18 - 0
springboot-demo/src/main/java/com/sf/controller/ChapterContentController.java

@@ -0,0 +1,18 @@
+package com.sf.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Controller
+@RequestMapping("/chapterContent")
+public class ChapterContentController {
+
+}

+ 18 - 0
springboot-demo/src/main/java/com/sf/controller/ChapterController.java

@@ -0,0 +1,18 @@
+package com.sf.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Controller
+@RequestMapping("/chapter")
+public class ChapterController {
+
+}

+ 33 - 0
springboot-demo/src/main/java/com/sf/controller/FictionController.java

@@ -0,0 +1,33 @@
+package com.sf.controller;
+
+import com.sf.po.Fiction;
+import com.sf.service.IFictionService;
+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.ResponseBody;
+
+import java.util.List;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Controller
+@RequestMapping("/fiction")
+public class FictionController {
+
+    @Autowired
+    private IFictionService fictionService;
+
+    @GetMapping("/list")
+    @ResponseBody
+    public List<Fiction> list(){
+        return fictionService.list();
+    }
+}

+ 18 - 0
springboot-demo/src/main/java/com/sf/controller/FictionShelfController.java

@@ -0,0 +1,18 @@
+package com.sf.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Controller
+@RequestMapping("/fictionShelf")
+public class FictionShelfController {
+
+}

+ 27 - 0
springboot-demo/src/main/java/com/sf/mapper/AuthorMapper.java

@@ -0,0 +1,27 @@
+package com.sf.mapper;
+
+import com.sf.po.Author;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Result;
+import org.apache.ibatis.annotations.Results;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.List;
+
+/**
+ * 我们如果需要让spring能够注入当前的mapper类
+ * 那么使用一个注解  @Mapper
+ * 当前层级 以前叫dao 后来更多叫mapper
+ */
+//@Mapper
+public interface AuthorMapper {
+
+//    @Select("select * from author")
+//    @Results(id = "authorMap", value = {
+//            @Result(id = true, column = "id", property = "id"),
+//            @Result(column = "author_id", property = "authorId"),
+//            @Result(column = "author_name", property = "authorName"),
+//            @Result(column = "author_desc", property = "authorDesc")
+//    })
+    List<Author> findAll();
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/mapper/ChapterContentMapper.java

@@ -0,0 +1,16 @@
+package com.sf.mapper;
+
+import com.sf.po.ChapterContent;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface ChapterContentMapper extends BaseMapper<ChapterContent> {
+
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/mapper/ChapterMapper.java

@@ -0,0 +1,16 @@
+package com.sf.mapper;
+
+import com.sf.po.Chapter;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface ChapterMapper extends BaseMapper<Chapter> {
+
+}

+ 21 - 0
springboot-demo/src/main/java/com/sf/mapper/FictionMapper.java

@@ -0,0 +1,21 @@
+package com.sf.mapper;
+
+import com.sf.po.Fiction;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Update;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface FictionMapper extends BaseMapper<Fiction> {
+    void addViews(int id);  //@Param("id")
+
+//    @Update()  注解和配置是可以混用的 但是不建议!!!
+//    void addViewsOther(int id);  //@Param("id")
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/mapper/FictionShelfMapper.java

@@ -0,0 +1,16 @@
+package com.sf.mapper;
+
+import com.sf.po.FictionShelf;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface FictionShelfMapper extends BaseMapper<FictionShelf> {
+
+}

+ 32 - 0
springboot-demo/src/main/java/com/sf/po/Author.java

@@ -0,0 +1,32 @@
+package com.sf.po;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * 当前的类 是代码中和数据库中表字段进行映射的类
+ * 这个层级 以前叫entity  domain
+ * 现在更多叫 po 持久化对象
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+public class Author implements Serializable {
+
+//    private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = 1501323542349052088L;
+
+    private Long id;
+
+    private String authorId; // author_id
+
+    private String authorName; // author_name
+
+    private String authorDesc; // author_desc
+
+}

+ 103 - 0
springboot-demo/src/main/java/com/sf/po/Chapter.java

@@ -0,0 +1,103 @@
+package com.sf.po;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public class Chapter implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    private Integer fictionId;
+
+    private String chapterTitle;
+
+    private Integer contentId;
+
+    private LocalDateTime createDate;
+
+    private Integer sort;
+
+    private String chapterUrl;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getFictionId() {
+        return fictionId;
+    }
+
+    public void setFictionId(Integer fictionId) {
+        this.fictionId = fictionId;
+    }
+
+    public String getChapterTitle() {
+        return chapterTitle;
+    }
+
+    public void setChapterTitle(String chapterTitle) {
+        this.chapterTitle = chapterTitle;
+    }
+
+    public Integer getContentId() {
+        return contentId;
+    }
+
+    public void setContentId(Integer contentId) {
+        this.contentId = contentId;
+    }
+
+    public LocalDateTime getCreateDate() {
+        return createDate;
+    }
+
+    public void setCreateDate(LocalDateTime createDate) {
+        this.createDate = createDate;
+    }
+
+    public Integer getSort() {
+        return sort;
+    }
+
+    public void setSort(Integer sort) {
+        this.sort = sort;
+    }
+
+    public String getChapterUrl() {
+        return chapterUrl;
+    }
+
+    public void setChapterUrl(String chapterUrl) {
+        this.chapterUrl = chapterUrl;
+    }
+
+    @Override
+    public String toString() {
+        return "Chapter{" +
+            "id = " + id +
+            ", fictionId = " + fictionId +
+            ", chapterTitle = " + chapterTitle +
+            ", contentId = " + contentId +
+            ", createDate = " + createDate +
+            ", sort = " + sort +
+            ", chapterUrl = " + chapterUrl +
+        "}";
+    }
+}

+ 49 - 0
springboot-demo/src/main/java/com/sf/po/ChapterContent.java

@@ -0,0 +1,49 @@
+package com.sf.po;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@TableName("chapter_content")
+public class ChapterContent implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    private String content;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    @Override
+    public String toString() {
+        return "ChapterContent{" +
+            "id = " + id +
+            ", content = " + content +
+        "}";
+    }
+}

+ 47 - 0
springboot-demo/src/main/java/com/sf/po/Fiction.java

@@ -0,0 +1,47 @@
+package com.sf.po;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Data
+public class Fiction implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    private String fictionName;
+
+    private LocalDateTime createDate;
+
+    private String author;
+
+    private String type;
+
+    private String newest;
+
+    private String state;
+
+    private String number;
+
+    private String brief;
+
+    private String imgUrl;
+
+    private String fictionUrl;
+
+    private Integer views;
+}

+ 71 - 0
springboot-demo/src/main/java/com/sf/po/FictionShelf.java

@@ -0,0 +1,71 @@
+package com.sf.po;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@TableName("fiction_shelf")
+public class FictionShelf implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    private Integer fictionId;
+
+    private Integer sort;
+
+    private Integer userId;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getFictionId() {
+        return fictionId;
+    }
+
+    public void setFictionId(Integer fictionId) {
+        this.fictionId = fictionId;
+    }
+
+    public Integer getSort() {
+        return sort;
+    }
+
+    public void setSort(Integer sort) {
+        this.sort = sort;
+    }
+
+    public Integer getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Integer userId) {
+        this.userId = userId;
+    }
+
+    @Override
+    public String toString() {
+        return "FictionShelf{" +
+            "id = " + id +
+            ", fictionId = " + fictionId +
+            ", sort = " + sort +
+            ", userId = " + userId +
+        "}";
+    }
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/service/IChapterContentService.java

@@ -0,0 +1,16 @@
+package com.sf.service;
+
+import com.sf.po.ChapterContent;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface IChapterContentService extends IService<ChapterContent> {
+
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/service/IChapterService.java

@@ -0,0 +1,16 @@
+package com.sf.service;
+
+import com.sf.po.Chapter;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface IChapterService extends IService<Chapter> {
+
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/service/IFictionService.java

@@ -0,0 +1,16 @@
+package com.sf.service;
+
+import com.sf.po.Fiction;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface IFictionService extends IService<Fiction> {
+
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/service/IFictionShelfService.java

@@ -0,0 +1,16 @@
+package com.sf.service;
+
+import com.sf.po.FictionShelf;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+public interface IFictionShelfService extends IService<FictionShelf> {
+
+}

+ 20 - 0
springboot-demo/src/main/java/com/sf/service/impl/ChapterContentServiceImpl.java

@@ -0,0 +1,20 @@
+package com.sf.service.impl;
+
+import com.sf.po.ChapterContent;
+import com.sf.mapper.ChapterContentMapper;
+import com.sf.service.IChapterContentService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Service
+public class ChapterContentServiceImpl extends ServiceImpl<ChapterContentMapper, ChapterContent> implements IChapterContentService {
+
+}

+ 20 - 0
springboot-demo/src/main/java/com/sf/service/impl/ChapterServiceImpl.java

@@ -0,0 +1,20 @@
+package com.sf.service.impl;
+
+import com.sf.po.Chapter;
+import com.sf.mapper.ChapterMapper;
+import com.sf.service.IChapterService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Service
+public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {
+
+}

+ 20 - 0
springboot-demo/src/main/java/com/sf/service/impl/FictionServiceImpl.java

@@ -0,0 +1,20 @@
+package com.sf.service.impl;
+
+import com.sf.po.Fiction;
+import com.sf.mapper.FictionMapper;
+import com.sf.service.IFictionService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Service
+public class FictionServiceImpl extends ServiceImpl<FictionMapper, Fiction> implements IFictionService {
+
+}

+ 20 - 0
springboot-demo/src/main/java/com/sf/service/impl/FictionShelfServiceImpl.java

@@ -0,0 +1,20 @@
+package com.sf.service.impl;
+
+import com.sf.po.FictionShelf;
+import com.sf.mapper.FictionShelfMapper;
+import com.sf.service.IFictionShelfService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author Qing
+ * @since 2024-01-24
+ */
+@Service
+public class FictionShelfServiceImpl extends ServiceImpl<FictionShelfMapper, FictionShelf> implements IFictionShelfService {
+
+}

+ 66 - 0
springboot-demo/src/main/java/com/sf/utils/GeneUtils.java

@@ -0,0 +1,66 @@
+package com.sf.utils;
+
+import com.baomidou.mybatisplus.generator.FastAutoGenerator;
+import com.baomidou.mybatisplus.generator.config.OutputFile;
+import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
+
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class GeneUtils {
+
+    public static void main(String[] args) {
+
+        FastAutoGenerator
+                .create("jdbc:mysql://localhost:3306/novels?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai",
+                        "root", "root123456")
+                .globalConfig(builder -> {
+                    builder.author("Qing") // 设置作者
+//                            .fileOverride() // 覆盖已生成文件
+                            .outputDir("src/main/java/"); // 指定输出目录 java文件的整体地址
+//                            .outputDir("src\\main\\java\\"); // windows中修改为\\
+                })
+                .dataSourceConfig(builder ->
+                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
+                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
+                            if (typeCode == Types.SMALLINT) {
+                                // 自定义类型转换
+                                // tinyInt  smallInt  < int  < bigInt
+                                // byte      short      int    long
+                                return DbColumnType.INTEGER;
+                            }
+                            return typeRegistry.getColumnType(metaInfo);
+
+                        }))
+                .packageConfig(builder -> {
+                    builder.parent("com.sf") // 设置父包名 自定义的源代码地址 对应Application主程序入口的包名
+                            .moduleName("") // 设置父包模块名
+                            .pathInfo(Collections.singletonMap(
+                                    OutputFile.xml, "src/main/resources/mapper")) // 设置mapperXml生成路径
+                            .entity("po")  // 实体类对应的包名 entity->po
+                            .service("service")
+                            .serviceImpl("service.impl")  // 服务层实现类对应的位置
+                            .mapper("mapper")  // dao层对应的包名  dao->mapper
+                            .xml("mapper.xml")
+                            .controller("controller")
+                            //                            .other("other")
+                            //                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://"))
+                            .build();
+
+                })
+                .strategyConfig(builder -> {
+                    // sys_user  t_user   User  UserMapper  UserService
+                    List<String>  tableList = new ArrayList<>();
+                    tableList.add("chapter_content");
+                    tableList.add("fiction_shelf");
+//                    builder.addInclude(tableList); // 设置需要生成的表名
+                    builder.addInclude("chapter_content","fiction_shelf"); // 设置需要生成的表名
+                    //                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
+                })
+                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
+                .execute();
+    }
+}

+ 14 - 0
springboot-demo/src/main/resources/application.properties

@@ -1 +1,15 @@
 server.port=18080
+
+#spring.thymeleaf.prefix=classpath:/test/
+#spring.thymeleaf.prefix=classpath:/templates
+#spring.thymeleaf.suffix=.html
+#spring.thymeleaf.encoding=utf-8
+#spring.thymeleaf.cache=true
+
+spring.datasource.url=jdbc:mysql://localhost:3306/novels?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
+spring.datasource.username=root
+spring.datasource.password=root123456
+#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
+
+mybatis.config-location=classpath:mybatis-config.xml
+mybatis.mapper-locations=classpath*:mapper/**/*.xml

+ 5 - 0
springboot-demo/src/main/resources/mapper/ChapterContentMapper.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.ChapterContentMapper">
+
+</mapper>

+ 5 - 0
springboot-demo/src/main/resources/mapper/ChapterMapper.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.ChapterMapper">
+
+</mapper>

+ 7 - 0
springboot-demo/src/main/resources/mapper/FictionMapper.xml

@@ -0,0 +1,7 @@
+<?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.FictionMapper">
+    <update id="addViews">
+        update fiction set views = views + 1 where id = #{id}
+    </update>
+</mapper>

+ 5 - 0
springboot-demo/src/main/resources/mapper/FictionShelfMapper.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.FictionShelfMapper">
+
+</mapper>

+ 20 - 0
springboot-demo/src/main/resources/mapper/authorMapper.xml

@@ -0,0 +1,20 @@
+<?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.AuthorMapper">
+
+<!--    <resultMap id="roleResultMap" type="com.sf.entity.Author">-->
+<!--        <id property="id" column="id" />-->
+<!--        <result property="authorId" column="author_id"/>-->
+<!--        <result property="authorName" column="author_name"/>-->
+<!--        <result property="authorDesc" column="author_desc"/>-->
+<!--    </resultMap>-->
+<!--     查询 -->
+    <select id="findAll" resultType="com.sf.po.Author">
+    <!-- <select id="findAll" resultMap="roleResultMap"> -->
+        select *
+        from author
+    </select>
+
+</mapper>

+ 17 - 0
springboot-demo/src/main/resources/mybatis-config.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-config.dtd">
+
+<configuration>
+    <!-- 开启延迟加载 该项默认为false,即所有关联属性都会在初始化时加载
+        true表示延迟按需加载 -->
+    <settings>
+        <setting name="lazyLoadingEnabled" value="true"/>
+        <!-- 开启二级缓存 -->
+        <setting name="cacheEnabled" value="true"/>
+        <!-- 配置mybatis自动转换为驼峰式命名 -->
+        <setting name="mapUnderscoreToCamelCase" value="true"/>
+    </settings>
+
+</configuration>

+ 14 - 0
springboot-demo/src/main/resources/thymeleaf/entity.java

@@ -0,0 +1,14 @@
+import lombok.Data;
+import org.springframework.context.annotation.Primary;
+
+@Data
+public class [(${className})] implements Serializable{
+        private static final long serialVersionUID = 1L;
+
+        [# th:if="${flag}"]@Primary[/]
+        private String [(${name})] ;
+
+        [# th:each="vo:${lists}"]
+        private [(${vo.fieldType})] [(${vo.fieldName})] ;
+        [/]
+ }

+ 8 - 0
springboot-demo/src/test/java/com/sf/DemoApplicationTests.java

@@ -1,6 +1,7 @@
 package com.sf;
 
 import com.sf.controller.HelloController;
+import com.sf.mapper.AuthorMapper;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -16,4 +17,11 @@ class DemoApplicationTests {
         helloController.hello();
     }
 
+    @Autowired
+    private AuthorMapper authorMapper;
+    @Test
+    public void testMapper(){
+        System.out.println(authorMapper.findAll());
+    }
+
 }

+ 128 - 0
springboot-demo/src/test/java/com/sf/MybatisPlusTests.java

@@ -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);
+    }
+}

+ 56 - 0
springboot-demo/src/test/java/com/sf/ThymeleafTests.java

@@ -0,0 +1,56 @@
+package com.sf;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.thymeleaf.TemplateEngine;
+import org.thymeleaf.context.Context;
+import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@SpringBootTest
+public class ThymeleafTests {
+
+    @Autowired
+    private TemplateEngine templateEngine;
+
+    @Test
+    public void test(){
+        // 先创建一个模版解析器 不再是处理html页面  而是处理 .java文件  它在thymeleaf文件夹下
+        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
+        resolver.setPrefix("thymeleaf/");
+        resolver.setSuffix(".java");
+        resolver.setTemplateMode("TEXT");
+        // 将模版解析器 设置到模版引擎中
+        templateEngine.setTemplateResolver(resolver);
+
+        // 准备数据 创建一个存放数据的容器
+        Context context = new Context();
+        // 设置变量  是model和view的对应关系
+        context.setVariable("className","Role");
+        context.setVariable("flag",true);
+        context.setVariable("name","id");
+
+        List<Map<String,String>> list = new ArrayList<>();
+        Map<String,String> map = new HashMap<>();
+        map.put("fieldName","name");
+        map.put("fieldType","String");
+
+        Map<String,String> map1 = new HashMap<>();
+        map1.put("fieldName","desc");
+        map1.put("fieldType","Integer");
+
+        list.add(map);
+        list.add(map1);
+        context.setVariable("lists",list);
+
+        // 调用模版引擎 进行数据渲染
+        // 渲染 需要模版文件 和 文件需要的数据
+        String out = templateEngine.process("entity", context);
+        System.out.println(out);
+    }
+}