自己写的小东西,空指针异常,但是该加的restController,service,mapper注解都加完了,为什么
下面贴出完整的代码::
Controller层:
package com.example.lib.controller;
import com.example.lib.po.Book;
import com.example.lib.result.Result;
import com.example.lib.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping("/add")
//新增图书
public Result addBooks(@RequestBody Book book){
Boolean isAlReadyExit = bookService.select(book);
if (isAlReadyExit){
return Result.error("序号已存在");
}else {
bookService.add(book);
return Result.success("添加成功");
}
}
//查询图书
@GetMapping("/select")
public Result<List<Book>> selectBooks(){
return Result.success(bookService.select());
}
}
mapper接口层:
package com.example.lib.mapper;
import com.example.lib.po.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface BookMapper {
Book selectByBookId(String bookId);
@Select("select * from books")
List<Book> select();
Boolean add(Book book);
}
book实体类:
package com.example.lib.po;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Book {
private Long id;
private String bookName;
private String author;
private String publish;
private BigDecimal price;
private String bookId;
private Date inputDate;
}
BookService 的实现类:
package com.example.lib.service.serviceImpl;
import com.example.lib.mapper.BookMapper;
import com.example.lib.po.Book;
import com.example.lib.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
public void add(Book book) {
bookMapper.add(book);
}
public Boolean select(Book book) {
String bookId = book.getBookId();
Book book1 = bookMapper.selectByBookId(bookId);
if (book1!=null){
return false;
}
return null;
}
@Override
public List<Book> select() {
return bookMapper.select();
}
}
application.properties配置图: