Browse Source

finish book search

master
朱天祥 1 year ago
parent
commit
5844fdf4f9
5 changed files with 240 additions and 4 deletions
  1. +93
    -0
      src/main/java/cn/edu/ecnu/stu/bookstore/controller/BookController.java
  2. +13
    -3
      src/main/java/cn/edu/ecnu/stu/bookstore/mapper/BookMapper.java
  3. +1
    -1
      src/main/java/cn/edu/ecnu/stu/bookstore/pojo/Book.java
  4. +39
    -0
      src/main/java/cn/edu/ecnu/stu/bookstore/service/impl/BookService.java
  5. +94
    -0
      src/main/resources/mapper/BookMapper.xml

+ 93
- 0
src/main/java/cn/edu/ecnu/stu/bookstore/controller/BookController.java View File

@ -0,0 +1,93 @@
package cn.edu.ecnu.stu.bookstore.controller;
import cn.edu.ecnu.stu.bookstore.component.AppException;
import cn.edu.ecnu.stu.bookstore.component.Constants;
import cn.edu.ecnu.stu.bookstore.component.Result;
import cn.edu.ecnu.stu.bookstore.service.impl.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/title")
public Result getBookByTitle(@RequestParam("title") String title,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(title))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByTitle(title, null, pageNum, pageSize));
}
@GetMapping("/title_in_store")
public Result getBookByTitleInStore(@RequestParam("title") String title,
@RequestParam("storeId") String storeId,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(title))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByTitle(title, storeId, pageNum, pageSize));
}
@GetMapping("/author")
public Result getBookByAuthor(@RequestParam("author") String author,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(author))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByAuthor(author, null, pageNum, pageSize));
}
@GetMapping("/author_in_store")
public Result getBookByAuthorInStore(@RequestParam("author") String author,
@RequestParam("storeId") String storeId,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(author))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByAuthor(author, storeId, pageNum, pageSize));
}
@GetMapping("/tag")
public Result getBookByTag(@RequestParam("tag") String tag,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(tag))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByTag(tag, null, pageNum, pageSize));
}
@GetMapping("/tag_in_store")
public Result getBookByTagInStore(@RequestParam("tag") String tag,
@RequestParam("storeId") String storeId,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(tag))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByTag(tag, storeId, pageNum, pageSize));
}
@GetMapping("/content_in_store")
public Result getBookByContentInStore(@RequestParam("content") String content,
@RequestParam("storeId") String storeId,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(content))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByContent(content, storeId, pageNum, pageSize));
}
@GetMapping("/content")
public Result getBookByContent(@RequestParam("content") String content,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
if(!StringUtils.hasText(content))
throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
return Result.success(bookService.getBookByContent(content, null, pageNum, pageSize));
}
}

+ 13
- 3
src/main/java/cn/edu/ecnu/stu/bookstore/mapper/BookMapper.java View File

@ -17,13 +17,11 @@ public interface BookMapper {
"#{book.translator}, #{book.pubYear}, #{book.pages}, #{book.price}, #{book.binding}, #{book.isbn}, #{book.stockLevel})")
int insert(@Param("book") Book book);
@Select("select tag from t_book_tag where book_id = #{book_id}")
List<String> selectTags(@Param("book_id") String bookId);
int insertTags(@Param("book_id") String bookId, @Param("tags") List<String> tags);
@Select("select picture from t_book_picture where book_id = #{book_id}")
List<String> selectPictureUrl(@Param("book_id") String bookId);
List<String> selectPictures(@Param("book_id") String bookId);
int insertPictures(@Param("book_id") String bookId, @Param("pictures") List<String> pictures);
@ -41,4 +39,16 @@ public interface BookMapper {
@Update("update t_book set stock_level = stock_level - #{count} where book_id = #{bookId}")
int minusStockLevel(@Param("bookId") String bookId, @Param("count") int count);
List<Book> getBookByTitle(@Param("title") String title, @Param("storeId") String storeId, @Param("start") Integer start, @Param("size") Integer size);
List<Book> getBookByAuthor(@Param("author") String author, @Param("storeId") String storeId, @Param("start") Integer start, @Param("size") Integer size);
List<Book> getBookByTag(@Param("tag") String tag, @Param("storeId") String storeId, @Param("start") Integer start, @Param("size") Integer size);
List<Book> getBookByContent(@Param("content") String content, @Param("storeId") String storeId, @Param("start") Integer start, @Param("size") Integer size);
}

+ 1
- 1
src/main/java/cn/edu/ecnu/stu/bookstore/pojo/Book.java View File

@ -36,7 +36,7 @@ public class Book {
private String isbn;
private int stockLevel;
private Integer stockLevel;
private List<String> tags;

+ 39
- 0
src/main/java/cn/edu/ecnu/stu/bookstore/service/impl/BookService.java View File

@ -0,0 +1,39 @@
package cn.edu.ecnu.stu.bookstore.service.impl;
import cn.edu.ecnu.stu.bookstore.mapper.BookMapper;
import cn.edu.ecnu.stu.bookstore.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
private BookMapper bookMapper;
public List<Book> getBookByTitle(String title, String storeId, Integer pageNum, Integer pageSize) {
int start = (pageNum - 1) * pageSize;
return bookMapper.getBookByTitle(title, storeId, start, pageSize);
}
public List<Book> getBookByAuthor(String author, String storeId, Integer pageNum, Integer pageSize) {
int start = (pageNum - 1) * pageSize;
return bookMapper.getBookByAuthor(author, storeId, start, pageSize);
}
public List<Book> getBookByTag(String tag, String storeId, Integer pageNum, Integer pageSize) {
int start = (pageNum - 1) * pageSize;
return bookMapper.getBookByTag(tag, storeId, start, pageSize);
}
public List<Book> getBookByContent(String content, String storeId, Integer pageNum, Integer pageSize) {
int start = (pageNum - 1) * pageSize;
return bookMapper.getBookByContent(content, storeId, start, pageSize);
}
}

+ 94
- 0
src/main/resources/mapper/BookMapper.xml View File

@ -25,4 +25,98 @@
</foreach>
)
</select>
<resultMap id="bookResultMap" type="cn.edu.ecnu.stu.bookstore.pojo.Book">
<id property="bookId" column="book_id"/>
<result property="storeId" column="store_id"/>
<result property="author" column="author"/>
<result property="publisher" column="publisher"/>
<result property="originalTitle" column="original_title"/>
<result property="translator" column="translator"/>
<result property="pages" column="pages"/>
<result property="price" column="price"/>
<result property="binding" column="binding"/>
<result property="isbn" column="isbn"/>
<result property="stockLevel" column="stock_level"/>
<result property="authorIntro" column="author_intro"/>
<result property="content" column="content"/>
<result property="bookIntro" column="book_intro"/>
<collection property="tags" ofType="String" select="selectTags" column="book_id"/>
<collection property="pictures" ofType="String" select="selectPictures" column="book_id"/>
</resultMap>
<select id="selectTags" resultType="String">
select tag from t_book_tag where book_id = #{book_id}
</select>
<select id="selectPictures" resultType="String">
select picture from t_book_picture where book_id = #{book_id}
</select>
<select id="getBookByTitle" resultMap="bookResultMap">
select b1.book_id, b1.store_id, b1.title, b1.author, b1.publisher, b1.original_title, b1.translator, b1.pub_year,
b1.pages, b1.price, b1.binding, b1.isbn, b1.stock_level, b2.book_intro, b2.author_intro, b2.content
from t_book b1
join t_book_detail b2 on b1.book_id = b2.book_id
where b1.title like "%"#{title}"%"
<if test="storeId != null">
and b1.store_id = #{storeId}
</if>
limit #{start}, #{size}
</select>
<select id="getBookByAuthor" resultMap="bookResultMap">
select b1.book_id, b1.store_id, b1.title, b1.author, b1.publisher, b1.original_title, b1.translator, b1.pub_year,
b1.pages, b1.price, b1.binding, b1.isbn, b1.stock_level, b2.book_intro, b2.author_intro, b2.content
from t_book b1
join t_book_detail b2 on b1.book_id = b2.book_id
where b1.author = #{author}
<if test="storeId != null">
and b1.store_id = #{storeId}
</if>
limit #{start}, #{size}
</select>
<resultMap id="bookByTagResultMap" type="cn.edu.ecnu.stu.bookstore.pojo.Book">
<id property="bookId" column="book_id"/>
<result property="storeId" column="store_id"/>
<result property="author" column="author"/>
<result property="publisher" column="publisher"/>
<result property="originalTitle" column="original_title"/>
<result property="translator" column="translator"/>
<result property="pages" column="pages"/>
<result property="price" column="price"/>
<result property="binding" column="binding"/>
<result property="isbn" column="isbn"/>
<result property="stockLevel" column="stock_level"/>
<result property="authorIntro" column="author_intro"/>
<result property="content" column="content"/>
<result property="bookIntro" column="book_intro"/>
<collection property="tags" ofType="String">
<constructor><arg column="tag"/> </constructor>
</collection>
<collection property="pictures" ofType="String" select="selectPictures" column="book_id"/>
</resultMap>
<select id="getBookByTag" resultMap="bookByTagResultMap">
select b1.book_id, b1.store_id, b1.title, b1.author, b1.publisher, b1.original_title, b1.translator, b1.pub_year,
b1.pages, b1.price, b1.binding, b1.isbn, b1.stock_level, b2.book_intro, b2.author_intro, b2.content, t.tag
from t_book_tag t
join t_book b1 on t.book_id = b1.book_id
join t_book_detail b2 on t.book_id = b2.book_id
where t.tag = #{tag}
<if test="storeId != null">
and b1.store_id = #{storeId}
</if>
limit #{start}, #{size}
</select>
<select id="getBookByContent" resultMap="bookResultMap">
select b1.book_id, b1.store_id, b1.title, b1.author, b1.publisher, b1.original_title, b1.translator, b1.pub_year,
b1.pages, b1.price, b1.binding, b1.isbn, b1.stock_level, b2.book_intro, b2.author_intro, b2.content
from t_book_detail b2
join t_book b1 on b2.book_id = b1.book_id
where match(author_intro, book_intro, content) against(#{content})
<if test="storeId != null">
and b1.store_id = #{storeId}
</if>
limit #{start}, #{size}
</select>
</mapper>

Loading…
Cancel
Save