提交 dc168aaf authored 作者: xuyang's avatar xuyang

完善程序4.22.1

上级 4c52d621
# Introduction
# 介绍
简单的图书馆系统
功能:
上传书籍、删除书籍、根据ID获取书籍信息、获取书籍列表
借书、还书、获取借书还书列表
新增用户、根据ID查找用户、获取用户列表
A library demo for test 1111111
......
......@@ -38,6 +38,17 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- faster json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......@@ -49,6 +60,21 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.4</version>
</dependency>
</dependencies>
<build>
......
......@@ -2,6 +2,7 @@ package com.springboot.demo1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Demo1Application {
......
......@@ -4,6 +4,9 @@ import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Books;
import com.springboot.demo1.entity.Response;
import com.springboot.demo1.service.BooksService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
......@@ -19,6 +22,7 @@ public class BooksController {
/**
* 获取图书列表
*/
@ApiOperation(value = "获取图书列表")
@GetMapping
public Response getBooksList(){
List<Books> booksList = booksService.getBooksList();
......@@ -30,8 +34,13 @@ public class BooksController {
* @param bookId
* @return
*/
@ApiOperation(value = "根据书本ID查找书籍")
@ApiImplicitParams({
@ApiImplicitParam(name = "bookId", value = "书籍ID", paramType = "path", required = true,
dataType = "Long")
})
@GetMapping(value = "/{bookId}")
public Response getBookById(@PathVariable Integer bookId) throws BaseException{
public Response getBookById(@PathVariable Long bookId) throws BaseException{
Books books = booksService.getBookById(bookId);
return new Response().normalResponse(books);
}
......@@ -42,6 +51,11 @@ public class BooksController {
* @return
* @throws BaseException
*/
@ApiOperation(value = "添加书籍")
@ApiImplicitParams({
@ApiImplicitParam(name = "bookName", value = "书名", paramType = "path", required = true,
dataType = "String")
})
@PostMapping(value = "/{bookName}")
public Response addBook(@PathVariable String bookName) throws BaseException{
Response response = new Response();
......@@ -54,8 +68,13 @@ public class BooksController {
* @param bookId
* @return
*/
@ApiOperation(value = "删除书籍")
@ApiImplicitParams({
@ApiImplicitParam(name = "bookId", value = "书籍ID", paramType = "path", required = true,
dataType = "Long")
})
@DeleteMapping(value = "/{bookId}")
public Response deleteBook(@PathVariable Integer bookId) throws BaseException{
public Response deleteBook(@PathVariable Long bookId) throws BaseException{
Response response = new Response();
booksService.deleteBook(bookId);
return response.normalResponse();
......
......@@ -3,10 +3,12 @@ package com.springboot.demo1.controller;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Response;
import com.springboot.demo1.service.BorrowInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping(value = "/borrowInfos")
......@@ -18,7 +20,7 @@ public class BorrowInfoController {
/**
* 获取借阅信息列表
*/
@PostMapping(value = "/get")
@PostMapping
public Response getBorrowInfoList() {
return new Response().normalResponse(borrowInfoService.getBorrowInfoList());
}
......@@ -26,8 +28,17 @@ public class BorrowInfoController {
/**
* 借书/还书
*/
@PostMapping(value = "/update/{userId}/{bookId}/{type}")
public Response updateBorrowInfo(@PathVariable Integer userId,@PathVariable Integer bookId,@PathVariable Integer type) throws BaseException {
@ApiOperation(value = "借书/还书")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", required = true,
dataType = "Long"),
@ApiImplicitParam(name = "bookId", value = "书籍ID", paramType = "path", required = true,
dataType = "Long"),
@ApiImplicitParam(name = "type", value = "操作方式0/1", paramType = "path", required = true,
dataType = "Long")
})
@PostMapping(value = "/{userId}/{bookId}/{type}")
public Response updateBorrowInfo(@PathVariable Long userId,@PathVariable Long bookId,@PathVariable Long type) throws BaseException {
borrowInfoService.updateBorrowInfoList(userId, bookId, type);
return new Response().normalResponse();
}
......
......@@ -3,6 +3,9 @@ package com.springboot.demo1.controller;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Response;
import com.springboot.demo1.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
......@@ -17,7 +20,8 @@ public class UserController {
/**
* 获取用户列表
*/
@PostMapping(value = "/get")
@ApiOperation(value = "获取用户列表")
@GetMapping
public Response getUserList(){
return new Response().normalResponse(userService.getUserList());
}
......@@ -25,14 +29,30 @@ public class UserController {
/**
* 根据用户ID获取用户
*/
@ApiOperation(value = "根据用户ID获取用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", required = true,
dataType = "Long")
})
@PostMapping(value = "/{userId}")
public Response getUserById(@PathVariable Integer userId){
public Response getUserById(@PathVariable Long userId){
return new Response().normalResponse(userService.getUserByUserId(userId));
}
/**
* 添加用户
*/
@ApiOperation(value = "添加用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "userName", value = "用户ID", paramType = "path", required = true,
dataType = "String"),
@ApiImplicitParam(name = "passWord", value = "密码", paramType = "path", required = true,
dataType = "String"),
@ApiImplicitParam(name = "email", value = "邮箱", paramType = "path", required = true,
dataType = "String"),
@ApiImplicitParam(name = "nickName", value = "昵称", paramType = "path", required = true,
dataType = "String")
})
@PostMapping(value = "/{userName}/{passWord}/{email}/{nickName}")
public Response addUser(@PathVariable String userName,@PathVariable String passWord,@PathVariable String email,
@PathVariable String nickName) throws BaseException{
......
......@@ -3,10 +3,10 @@ package com.springboot.demo1.dao;
import com.springboot.demo1.entity.Books;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BooksRepository extends JpaRepository<Books, Integer> {
public interface BooksRepository extends JpaRepository<Books, Long> {
Books findBooksByBookId(Integer bookId);
Books findBooksByBookId(Long bookId);
Integer countBooksByBookName(String bookName);
Long countBooksByBookName(String bookName);
}
......@@ -3,8 +3,8 @@ package com.springboot.demo1.dao;
import com.springboot.demo1.entity.BorrowInfo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BorrowInfoRepository extends JpaRepository<BorrowInfo, Integer> {
public interface BorrowInfoRepository extends JpaRepository<BorrowInfo, Long> {
BorrowInfo getBorrowInfoByUserIdAndBookId(Integer userId, Integer bookId);
BorrowInfo getBorrowInfoByUserIdAndBookId(Long userId, Long bookId);
}
......@@ -7,12 +7,12 @@ import org.springframework.data.repository.query.Param;
import java.util.List;
public interface UserRepository extends JpaRepository<User,Integer> {
public interface UserRepository extends JpaRepository<User,Long> {
User getUserByUserId(Integer userId);
User getUserByUserId(Long userId);
Integer countUserByEmail(String email);
Long countUserByEmail(String email);
Integer countUserByUserName(String userName);
Long countUserByUserName(String userName);
}
package com.springboot.demo1.entity;
import lombok.Data;
import lombok.NonNull;
import lombok.experimental.Accessors;
import javax.persistence.*;
......@@ -14,7 +15,7 @@ public class Books {
*/
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer bookId;
private Long bookId;
/**
* 书名
......@@ -26,6 +27,6 @@ public class Books {
* 书籍状态 ==0表示书籍未被借阅 ==1表示书籍已被借阅
*/
@Column(nullable = false)
private Integer bookState;
private Long bookState;
}
package com.springboot.demo1.entity;
import lombok.Data;
import lombok.NonNull;
import lombok.experimental.Accessors;
import org.hibernate.annotations.DynamicUpdate;
......@@ -17,25 +18,25 @@ public class BorrowInfo {
*/
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer borrowId;
private Long borrowId;
/**
* 用户ID
*/
@Column(nullable = false)
private Integer userId;
private Long userId;
/**
* 书籍ID
*/
@Column(nullable = false)
private Integer bookId;
private Long bookId;
/**
* 借阅状态 ==1表示借书 ==0表示还书
*/
@Column(nullable = false)
private Integer type;
private Long type;
@Column
private Date borrowTime;
......
package com.springboot.demo1.entity;
import lombok.Data;
import lombok.NonNull;
import lombok.experimental.Accessors;
import javax.persistence.*;
......@@ -14,7 +15,7 @@ public class User {
*/
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer userId;
private Long userId;
/**
* 用户名
......
package com.springboot.demo1.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Response;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.ConnectException;
@ControllerAdvice
public class ExceptionHandle {
//处理异常
@ExceptionHandler({Exception.class})
@ResponseBody
public static Object handle(HttpServletRequest req, HttpServletResponse resp, Exception e){
Response response = new Response();
resp.setContentType( "text/json;charset=UTF-8" );
if(e instanceof NullPointerException){
response.setMsg("参数异常");
response.setResult("901");
}else if(e instanceof BaseException){
BaseException baseException = ((BaseException) e);
response.setMsg(baseException.getMessage());
response.setResult(baseException.getCode() + "");
}else if(e instanceof ConnectException){
response.setMsg("数据库连接失败");
response.setResult("888");
}else if(e instanceof MissingServletRequestParameterException){
//不处理
response.setMsg("缺少请求参数!");
response.setResult("899");
} else{
response.setMsg("网络异常,请稍后再试!");
response.setResult("898");
}
return response;
}
}
package com.springboot.demo1.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@ComponentScan(basePackages = {"com.springboot.demo1.controller"})
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.apiInfo(apiInfo())
//请求头路径
.pathMapping("/")
.select()
//swagger注解扫描api路径
.apis(RequestHandlerSelectors.basePackage("com.springboot.demo1.controller"))
.paths(PathSelectors.any())
.build().groupName("library_api");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Api document")
.termsOfServiceUrl("http://localhost:8080/")
.description("springboot swagger2")
//.contact(new Contact("lin", "", "490456661@qq.com"))
.version("1.0")
.build();
}
}
......@@ -4,6 +4,7 @@ import com.springboot.demo1.dao.BooksRepository;
import com.springboot.demo1.entity.BaseException;
import com.springboot.demo1.entity.Books;
import com.springboot.demo1.service.impl.BooksServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -19,30 +20,29 @@ public class BooksService implements BooksServiceImpl {
return booksRepository.findAll();
}
public Books getBookById(Integer bookId) throws BaseException{
public Books getBookById(Long bookId) throws BaseException{
if(!booksRepository.findById(bookId).isPresent()) {
throw new BaseException("书籍不存在", 506);
}
return booksRepository.findBooksByBookId(bookId);
}
public void addBook(String bookName) throws BaseException {
if (bookName == null || bookName.length() < 1) {
if (StringUtils.isBlank(bookName)) {
throw new BaseException("书名信息有误", 506);
}
Integer numByBookName = booksRepository.countBooksByBookName(bookName);
Long numByBookName = booksRepository.countBooksByBookName(bookName);
if (numByBookName > 0) {
throw new BaseException("该书籍已经存在", 506);
} else {
Books books = new Books();
books.setBookName(bookName);
books.setBookState(0); //bookState为0表示书籍未被借出
books.setBookState(0L); //bookState为0表示书籍未被借出
booksRepository.save(books);
}
}
public void deleteBook(Integer bookId) throws BaseException {
public void deleteBook(Long bookId) throws BaseException {
if(!booksRepository.findById(bookId).isPresent()) {
throw new BaseException("书籍不存在", 506);
}
......
......@@ -25,7 +25,7 @@ public class BorrowInfoService implements BorrowInfoServiceImpl {
return borrowInfoRepository.findAll();
}
public void updateBorrowInfoList(Integer userId, Integer bookId, Integer type) throws BaseException {
public void updateBorrowInfoList(Long userId, Long bookId, Long type) throws BaseException {
if (userId == null || bookId == null || type == null) {
throw new BaseException("参数为空", 506);
}
......@@ -45,12 +45,12 @@ public class BorrowInfoService implements BorrowInfoServiceImpl {
if (type == 1 && books.getBookState() == 0) { //借书
borrowInfo.setBorrowId(borrowInfo1.getBorrowId()); //将对应借书记录进行修改
borrowInfo.setBorrowTime(new Date());
books.setBookState(1);
books.setBookState(1L);
} else if (type == 0 && books.getBookState() == 1) { //还书
borrowInfo.setBorrowId(borrowInfo1.getBorrowId());
borrowInfo.setBorrowTime(borrowInfo1.getBorrowTime());
borrowInfo.setReturnTime(new Date());
books.setBookState(0);
books.setBookState(0L);
} else if (type == 1 && books.getBookState() == 1) {
throw new BaseException("该书籍已被借出", 506);
} else {
......@@ -64,9 +64,9 @@ public class BorrowInfoService implements BorrowInfoServiceImpl {
} else {
borrowInfo.setBorrowTime(new Date());
if(books.getBookState() == 0) {
books.setBookState(1);
books.setBookState(1L);
} else {
books.setBookState(0);
books.setBookState(0L);
}
}
}
......
......@@ -19,7 +19,7 @@ public class UserService implements UserServiceImpl {
return userRepository.findAll();
}
public User getUserByUserId(Integer userId) {
public User getUserByUserId(Long userId) {
return userRepository.getUserByUserId(userId);
}
......@@ -30,8 +30,8 @@ public class UserService implements UserServiceImpl {
if(email == null || email.length() < 6) {
throw new BaseException("邮箱格式有误", 506) ;
}
Integer numByEmail = userRepository.countUserByEmail(email);
Integer numByUserName = userRepository.countUserByUserName(userName);
Long numByEmail = userRepository.countUserByEmail(email);
Long numByUserName = userRepository.countUserByUserName(userName);
if(numByEmail > 0) {
throw new BaseException("该邮箱已被使用", 506);
}
......
......@@ -9,10 +9,10 @@ public interface BooksServiceImpl {
public List<Books> getBooksList();
public Books getBookById(Integer bookId) throws BaseException;
public Books getBookById(Long bookId) throws BaseException;
public void addBook(String bookName) throws BaseException;
public void deleteBook(Integer bookId) throws BaseException;
public void deleteBook(Long bookId) throws BaseException;
}
......@@ -9,6 +9,6 @@ public interface BorrowInfoServiceImpl {
public List<BorrowInfo> getBorrowInfoList();
public void updateBorrowInfoList(Integer userId, Integer bookId, Integer type) throws BaseException;
public void updateBorrowInfoList(Long userId, Long bookId, Long type) throws BaseException;
}
......@@ -9,7 +9,7 @@ public interface UserServiceImpl {
public List<User> getUserList();
public User getUserByUserId(Integer userId);
public User getUserByUserId(Long userId);
public void addUser(String userName, String passWord, String email, String nickName) throws BaseException;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论