唯读---图书微服务

唯读---图书微服务

上一期博客已经更新了图书微服务的搭建,现在直接就是照着图书微服务接口补充逻辑,这几天忙的一批,忙着工作室招新,哈哈哈哈,不过还好,有好多优秀学弟学妹来搞这个事情,也是比较放心。

一、依赖导入

话不多说,直接吧pom.xml粘过来

wedo-book-service/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wedo-book</artifactId>
        <groupId>com.wedo.book</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wedo.book</groupId>
    <artifactId>wedo-book-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <!-- 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>
        <!-- 依赖leyou-item-interface -->
        <dependency>
            <groupId>com.wedo.book</groupId>
            <artifactId>wedo-book-interface</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 依赖wedo-common -->
        <dependency>
            <groupId>com.wedo.common</groupId>
            <artifactId>wedo-common</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <!-- Spring AMQP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

</project>

wedo-book-interface/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wedo-book</artifactId>
        <groupId>com.wedo.book</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wedo.book</groupId>
    <artifactId>wedo-book-interface</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wedo.common</groupId>
            <artifactId>wedo-common</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

二、wedo-book代码编写

先简单介绍一下这个模块:

这个模块也就是传说中的图书微服务,唯读项目的核心部分,与书或者内容相关的都在这个微服务中体现,因为之前写好了接口文档,现在只需要照着接口文档补接口就行了,但是哥们好像并不怎么看接口文档,有点桑心,还好可以靠口头对接口,哥们:你获取图书列表咋搞,我:这个url,要这几个参数。虽然比较原始,但还是能用。

项目概览

项目概览

就长这个样子了,对了,还有wedo-book-interface子项目

interface子项目

然后就是一点一点补接口辽

实体类编写

实体类在book-interface项目里了,其他项目需要用到这个实体类直接依赖interface就可以辽

实体类其实在一开始就设计的差不多了,后来有做了些小改动。

首先是我们的主角,Book实体类,这个是图书实体鸭

package com.wedo.book.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Table(name = "book")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String intro;
    private Long aId;
    private String bookComment;
    private String image;
    private Date creatDate;
    private Integer chapterNumber;
    private Date updateDate;
    private Boolean state;
    private String protagonist;
    private Long cId;

    // get和set咱们就略了吧,要不然太长了
}

然后就是不可或缺的角色,Category实体类,这个是分类哈

package com.wedo.book.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "category")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String typeName;
    private String typeInfo;
    private String iconUrl;

    // get和set就继续略了
}

紧接着是章节,Chapter实体类,有章节的信息,还有内容

package com.wedo.book.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "chapter")
public class Chapter {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Integer sequenceNum;
    private String title;
    private String context;
    private Long bId;
    
    // get和set再略
}

下一个是图书热度实体类,虽然我觉得这个好像存在设计问题,BookHot

package com.wedo.book.pojo;

import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "book_hot")
public class BookHot {
    @Id
    private Long bId;
    private Integer clickNum;
    private Integer searchNum;
    private Integer hot;

    // 我再略
}

连接词穷,反正这个是作者实体类,Author

package com.wedo.book.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "author")
public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String introduce;
    private String aPhoto;
    
    // 略略略
}

实体增强类

这个主要用户接收requestbody参数的类,应为前端发送的是json,后台只能用requestbody了,虽然个别情况很麻烦,但是为了哥们前端写的简单,就这吧。

ChapterRequest

package com.wedo.book.bo;

import com.wedo.book.pojo.Chapter;

/**
 * Chapter请求参数实体类
 */
public class ChapterRequest extends Chapter {
    private Integer frontSequenceNum;

    public Integer getFrontSequenceNum() {
        return frontSequenceNum;
    }

    public void setFrontSequenceNum(Integer frontSequenceNum) {
        this.frontSequenceNum = frontSequenceNum;
    }
}

响应结果类

这个就不用多说了吧,就是需要自定义响应结果的情况

ChapterResult(感觉多余)

package com.wedo.book.result;

/**
 * 章节基本信息
 */
public class ChapterResult {
    private Long id;
    private Integer sequenceNum;
    private String title;
    private Long bId;
    
    // get和set略了
}

BookHotResult

package com.wedo.book.result;

import com.wedo.book.pojo.Book;

public class BookHotResult extends Book {
    private String name;
    private Integer hot;

    // get和set又略
}

interface项目基本就这么多,后期需要补上可被其他微服务调用的接口包,等用到在写吧

Mapper层(Dao层)

打的有点累,这个是BookMapper,用了通用mapper基本只需要写特殊方法,我的习惯一般是单表使用通用mapper提供的方法,多表使用自定义方法,手写sql了

package com.wedo.book.mapper;

import com.wedo.book.pojo.Book;
import com.wedo.book.result.BookHotResult;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface BookMapper extends Mapper<Book> {
    @Select("select b.id,b.title,b.intro,b.image,bh.hot,a.name from book b join book_hot bh,author a where b.c_id = #{cid} and bh.b_id = b.id and a.id = b.a_id order by bh.hot desc limit #{rows};")
    List<BookHotResult> queryBooksByCidAndHot(@Param("cid") Long cid, @Param("rows") Integer rows);
}

CategoryMapper

package com.wedo.book.mapper;

import com.wedo.book.pojo.Category;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface CategoryMapper extends Mapper<Category> {

    @Delete("delete from category_book where c_id = #{cid}")
    void deleteCategoryAndBook(@Param("cid") Long cid);

    @Select("select c.* from book b join category c where b.id = #{bid} and b.c_id = c.id;")
    List<Category> queryCategoryByBid(@Param("bid") Long bid);
}

ChapterMapper

package com.wedo.book.mapper;

import com.wedo.book.pojo.Chapter;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

public interface ChapterMapper extends Mapper<Chapter> {

    @Select("SELECT max(sequence_num) FROM `chapter` where b_id = #{bid}")
    Integer queryMaxSequenceNumByBid(@Param("bid") Long bid);

    @Update("UPDATE chapter SET sequence_num=sequence_num+1 WHERE sequence_num > #{frontSequenceNum} and b_id = #{bid}")
    void updateSequenceNumLaterAdd(@Param("frontSequenceNum") Integer frontSequenceNum,@Param("bid") Long bid);

    @Update("UPDATE chapter SET sequence_num=sequence_num-1 WHERE sequence_num > #{frontSequenceNum} and b_id = #{bid}")
    void updateSequenceNumLaterSub(@Param("frontSequenceNum") Integer frontSequenceNum,@Param("bid") Long bid);
}

BookHotMapper

package com.wedo.book.mapper;

import com.wedo.book.pojo.BookHot;
import tk.mybatis.mapper.common.Mapper;

public interface BookHotMapper extends Mapper<BookHot> {
}

AuthorMapper

package com.wedo.book.mapper;

import com.wedo.book.pojo.Author;
import tk.mybatis.mapper.common.Mapper;

public interface AuthorMapper extends Mapper<Author> {
}

controller层

首先是BookController

package com.wedo.book.controller;

import com.wedo.book.pojo.Book;
import com.wedo.book.pojo.BookHot;
import com.wedo.book.result.BookHotResult;
import com.wedo.book.service.BookService;
import com.wedo.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("books")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Book>> queryBookByPage(
            @RequestParam(value = "title",required = false) String title,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Book> result = this.bookService.queryBookByPage(title,cid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 根据分类id查询
     * @param cid
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("pageByCid")
    public ResponseEntity<PageResult<Book>> queryBookPageByCid(
            @RequestParam(value = "cid",required = true) Long cid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Book> result = this.bookService.queryBookPageByCid(cid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 根据分类id查询
     * @param cid
     * @param rows
     * @return
     */
    @GetMapping("pageByCidAndHot")
    public ResponseEntity<List<BookHotResult>> queryBooksByCidAndHot(
            @RequestParam(value = "cid") Long cid,
            @RequestParam(value = "rows",defaultValue = "4") Integer rows
    ) {
        List<BookHotResult> books = this.bookService.queryBooksByCidAndHot(cid,rows);
        if (CollectionUtils.isEmpty(books)) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(books);
    }

    /**
     * 根据作者id查询
     * @param aid
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("pageByAid")
    public ResponseEntity<PageResult<Book>> queryBookPageByAid(
            @RequestParam(value = "aid",required = true) Long aid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Book> result = this.bookService.queryBookPageByAid(aid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 根据图书id查找
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<Book> queryBookByBid(@PathVariable("bid")Long bid) {
        Book result = this.bookService.queryBookByBid(bid);
        if (result == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    @PutMapping
    public ResponseEntity<Void> updateBook(@RequestBody Book book) {
        if (book.getId() == null) {
            return ResponseEntity.badRequest().build();
        }
        this.bookService.updateBookById(book);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping
    public ResponseEntity<Void> addBook(@RequestBody Book book) {
        this.bookService.saveBook(book);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteBook(@RequestBody Book book) {
        if (book.getId() == null) {
            return ResponseEntity.badRequest().build();
        }
        this.bookService.deleteBook(book.getId());
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}

GET请求我一般用于查询辽,Post一般用来添加,Put请求用于修改,Delete请求用于删除,业务内容基本跟方法名字面意思一致。

好累啊,今天先到这里了,明天再继续更新吧。
上一篇:唯读---项目基础搭建

下一篇:最后一篇了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343