唯读---图书微服务
上一期博客已经更新了图书微服务的搭建,现在直接就是照着图书微服务接口补充逻辑,这几天忙的一批,忙着工作室招新,哈哈哈哈,不过还好,有好多优秀学弟学妹来搞这个事情,也是比较放心。
一、依赖导入
话不多说,直接吧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子项目
然后就是一点一点补接口辽
实体类编写
实体类在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请求用于删除,业务内容基本跟方法名字面意思一致。
好累啊,今天先到这里了,明天再继续更新吧。
上一篇:唯读---项目基础搭建
下一篇:最后一篇了