我正在使用Spring Boot和Spring制作Book Store应用程序 数据。我需要支持才能知道我所做的事情是否正确。
我正在使用Spring Boot和Spring Data创建一个Book Store REST应用程序。我刚开始使用它,却陷入了困境。尽管我已经实现了(自定义查询-加入)我想要的,但是我真的很想知道我所做的是正确的方法还是有更好的方法。
这是三个实体类:
图书:
@Entity
@Table(name="Book_Details")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CUSTOM_SEQUENCE")
@SequenceGenerator(sequenceName = "customer_seq", allocationSize = 1, name = "CUSTOM_SEQUENCE")
@Column(name="BookId")
int bookId;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
@Column(name="BookName")
String bookName;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="authorId")
Author author;
@Column(name="Language")
String language;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="genreId")
Genre genre;
@Column(name="GoodReadReviews")
String goodReadReviews;
//To display values
@Override
public String toString() {
return "BookId=" + this.bookId + "::BookName=" + this.bookName +
"::AuthorId=" + this.author.authorName +
"::Langauge = " + this.language + "::GenreId = " +
this.genre.genreName + "::GoodReadReviews=" +
this.goodReadReviews;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getGoodReadReviews() {
return goodReadReviews;
}
public void setGoodReadReviews(String goodReadReviews) {
this.goodReadReviews = goodReadReviews;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
}
作者:
@Entity
@Table(name="Author_Details")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="AuthorSeqGenerator")
@SequenceGenerator(sequenceName = "author_seq", allocationSize = 1, name
= "AuthorSeqGenerator")
@Column(name="AuthorId")
int authorId;
@Column(name="AuthorName")
String authorName;
@Column(name="Country")
String country;
@Column(name="Gender")
String gender;
@OneToOne(mappedBy="author")
@JoinColumn(name="authorId")
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
类型:
@Entity
@Table(name="Genre_details")
public class Genre {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="GenreSeqGenerator")
@SequenceGenerator(sequenceName = "genre_seq", allocationSize = 1, name =
"GenreSeqGenerator")
int genreId;
@Column(name="GenreName")
String genreName;
@OneToOne(mappedBy="genre")
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getGenreId() {
return genreId;
}
public void setGenreId(int genreId) {
this.genreId = genreId;
}
public String getGenreName() {
return genreName;
}
public void setGenreName(String genreName) {
this.genreName = genreName;
}
}
以下是主要的Spring-boot应用程序类:
@SpringBootApplication
public class BookStoreManagerApplication implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(BookStoreManagerApplication.class, args);
}
@Autowired
DataSource dataSource;
@Autowired
BookRepository bookRepository;
//@Transactional(readOnly=true)
@Override
public void run(String... args) throws Exception {
System.out.println("DATASOURCE = " + dataSource);
System.out.println("1.) Find By Book Id = " );
for(Book book:bookRepository.findByBookId(1)) {
System.out.println(book.toString());
}
System.out.println("2.) Find By Book Name = ");
for(Book book:bookRepository.findByBookName("Pinjar")) {
System.out.println(book.toString());
}
System.out.println("3.) Find All = " );
for(Book book:bookRepository.findAll()) {
System.out.println(book.toString());
}
System.out.println("4.) Custom Query using @Query annotation = " );
for(Object[] joinResult: bookRepository.findByAuthorIdAndGenreId(1, 1)){
System.out.println(joinResult[0] + "::" + joinResult[1] + "::" +
"::" + joinResult[2] + "::" + joinResult[3] + "::" + joinResult[4] +
"::" + joinResult[5] + "::" + joinResult[6] + "::" + joinResult[7]);
}
System.out.println("Check if tables are created!");
}
}
这是图书库:
public interface BookRepository extends CrudRepository<Book, String>{
List<Book> findByBookId(int id);
//List<Book> findByAuthorId(int authorId);
List<Book> findByLanguage(String lang);
//List<Book> findByGenreId(int genreId);
List<Book> findByGoodReadReviews(String reviews);
List<Book> findByBookName(String bookName);
List<Book> findAll();
//Stream<Book> findAllByStream();
@Query("select b.bookId, b.bookName, a.authorId, a.authorName, b.language, "
+ "g.genreId, g.genreName,b.goodReadReviews from Book b, Author a, "
+ "Genre g where b.author=a.authorId and b.genre=g.genreId and "
+ "a.authorId=?1 and g.genreId=?2")
List<Object[]> findByAuthorIdAndGenreId(int authorId, int genreId);
}
这整个过程工作正常,但对于上述使用 @Query注释我想知道这是正确的方法还是我 可以使用一些更好的方法。我为这个幼稚的事感到抱歉 问题。
结果如下:
1.) Find By Book Id =
2019-01-22 17:02:40 DEBUG org.hibernate.SQL - select book0_.book_id as book_id1_1_, book0_.author_id as author_id5_1_, book0_.book_name as book_name2_1_, book0_.genre_id as genre_id6_1_, book0_.good_read_reviews as good_read_reviews3_1_, book0_.language as language4_1_ from book_details book0_ where book0_.book_id=?
BookId=1::BookName=Tuesdays With Morrie::AuthorId=null::Langauge = English::GenreId = null::GoodReadReviews=4.5
2.) Find By Book Name =
2019-01-22 17:02:40 DEBUG org.hibernate.SQL - select book0_.book_id as book_id1_1_, book0_.author_id as author_id5_1_, book0_.book_name as book_name2_1_, book0_.genre_id as genre_id6_1_, book0_.good_read_reviews as good_read_reviews3_1_, book0_.language as language4_1_ from book_details book0_ where book0_.book_name=?
BookId=2::BookName=Pinjar::AuthorId=null::Langauge = Punjabi::GenreId = null::GoodReadReviews=4.8
3.) Find All =
2019-01-22 17:02:40 DEBUG org.hibernate.SQL - select book0_.book_id as book_id1_1_, book0_.author_id as author_id5_1_, book0_.book_name as book_name2_1_, book0_.genre_id as genre_id6_1_, book0_.good_read_reviews as good_read_reviews3_1_, book0_.language as language4_1_ from book_details book0_
BookId=1::BookName=Tuesdays With Morrie::AuthorId=null::Langauge = English::GenreId = null::GoodReadReviews=4.5
BookId=2::BookName=Pinjar::AuthorId=null::Langauge = Punjabi::GenreId = null::GoodReadReviews=4.8
BookId=3::BookName=Raavi Paar::AuthorId=null::Langauge = Hindi::GenreId = null::GoodReadReviews=4.6
4.) Custom Query using @Query annotation =
2019-01-22 17:02:40 DEBUG org.hibernate.SQL - select book0_.book_id as col_0_0_, book0_.book_name as col_1_0_, author1_.author_id as col_2_0_, author1_.author_name as col_3_0_, book0_.language as col_4_0_, genre2_.genre_id as col_5_0_, genre2_.genre_name as col_6_0_, book0_.good_read_reviews as col_7_0_ from book_details book0_ cross join author_details author1_ cross join genre_details genre2_ where book0_.author_id=author1_.author_id and book0_.genre_id=genre2_.genre_id and author1_.author_id=? and genre2_.genre_id=?
1::Tuesdays With Morrie::::1::Mitch Albom::English::1::Fiction::4.5