问题描述
请考虑以下技术堆栈:
还要考虑2个表: 书籍和作者
Books:
book_id : Integer & PrimaryKey
author_id: Integer & ForeignKey to author_id column of Authors table
book_title: Varchar
date: Date
Authors:
author_id : Integer & PrimaryKey
author_name: Varchar
date: Date
======== 问题陈述: 我需要设计2个休息服务端点:
问题:直接添加作者记录。
代码部分
图书实体:
@Entity
@Table(name = "books")
public class Books {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_id",nullable = false)
private Integer book_id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "author_id",referencedColumnName = "author_id")
private Authors author_id;
@Column(name = "book_title",nullable = false)
private String book_title;
@Column(name = "created_date",nullable = false)
private Date created_date;
public Integer getBook_id() {
return book_id;
}
public void setBook_id(Integer book_id) {
this.book_id = book_id;
}
public Authors getAuthor_id() {
return author_id;
}
public void setAuthor_id(Authors author_id) {
this.author_id = author_id;
}
public String getBook_title() {
return book_title;
}
public void setBook_title(String book_title) {
this.book_title = book_title;
}
public Date getCreated_date() {
return created_date;
}
public void setCreated_date(Date created_date) {
this.created_date = created_date;
}
}
作者身份:
@Entity
@Table(name = "authors")
public class Authors {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "author_id",nullable = false)
private Integer author_id;
@Column(name = "author_name",nullable = false)
private String author_name;
@Column(name = "created_date",nullable = false)
private Date created_date;
public Integer getAuthor_id() {
return author_id;
}
public void setAuthor_id(Integer author_id) {
this.author_id = author_id;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public Date getCreated_date() {
return created_date;
}
public void setCreated_date(Date created_date) {
this.created_date = created_date;
}
}
说明:
@Service
public class LibraryService {
@Autowired
private AuthorDao authorDao;
@Autowired
private BooksDao booksDao;
public void addAuthor(Authors authors){
authors.setCreated_date(new Date());
authors.setLast_modified_date(new Date());
authorDao.save(authors);
}
public void addBooks(Books books){
books.setCreated_date(new Date());
books.setLast_modified_date(new Date());
booksDao.save(books);
}
}
控制器类:
@RestController
public class LibraryController {
@Autowired
private LibraryService libraryService;
@PostMapping(value = "/author",produces = MediaType.APPLICATION_JSON_VALUE)
public void addProduct(@RequestBody Authors authors) throws GlobalException {
try{
libraryService.addAuthor(authors);
}catch(RuntimeException exception){
throw new GlobalException(exception.getLocalizedMessage());
}
}
@PostMapping(value = "/book",produces = MediaType.APPLICATION_JSON_VALUE)
public void addBook(@RequestBody Books books) throws GlobalException {
try{
libraryService.addBooks(books);
}catch(RuntimeException exception){
throw new GlobalException(exception.getLocalizedMessage());
}
}
}
如果在添加Book时以以下格式给出JSON(这意味着触发addBooks方法),则新记录将成功插入到Authors ad Books表中。但这是一个好习惯吗?如果作者表中已经存在作者姓名,该怎么办。如何仅在请求正文json中发送author_id,以便成功触发?
{
"author_id": {
"author_name": "Chetan Bhagat","created_date": "2020-11-03",},"book_title": "Two states",}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)