Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BookBindingModel:
- package library.bindingModels;
- public class BookBindingModel {
- private String title;
- private String author;
- private double price;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public Double getPrice() {
- return price;
- }
- public void setPrice(Double price) {
- this.price = price;
- }
- }
- //BookController:
- package library.controllers;
- import library.bindingModels.BookBindingModel;
- import library.entities.Book;
- import library.repositories.BookRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.servlet.ModelAndView;
- import java.util.List;
- @Controller
- public class BookController {
- private final BookRepository bookRepository;
- @Autowired
- public BookController(BookRepository bookRepository) {
- this.bookRepository = bookRepository;
- }
- @GetMapping("/")
- public ModelAndView index(ModelAndView modelAndView) {
- List<Book> books = this.bookRepository.findAll();
- modelAndView.setViewName("base-layout");
- modelAndView.addObject("view", "book/index");
- modelAndView.addObject("books", books);
- return modelAndView;
- }
- @GetMapping("/create")
- public ModelAndView create(ModelAndView modelAndView) {
- modelAndView.setViewName("base-layout");
- modelAndView.addObject("view", "book/create");
- return modelAndView;
- }
- @PostMapping("/create")
- public String create(Book book) {
- this.bookRepository.saveAndFlush(book);
- return "redirect:/";
- }
- @GetMapping("/edit/{id}")
- public ModelAndView edit(@PathVariable(value = "id") Integer id, ModelAndView modelAndView) {
- Book book = this.bookRepository.findById(id).get();
- modelAndView.setViewName("base-layout");
- modelAndView.addObject("view", "book/edit");
- modelAndView.addObject("book", book);
- return modelAndView;
- }
- @PostMapping("edit/{id}")
- public String edit(@PathVariable(value = "id") Integer id, BookBindingModel bookBindingModel){
- Book bookEdit = this.bookRepository.getOne(id);
- bookEdit.setTitle(bookBindingModel.getTitle());
- bookEdit.setAuthor(bookBindingModel.getAuthor());
- bookEdit.setPrice(bookBindingModel.getPrice());
- this.bookRepository.saveAndFlush(bookEdit);
- return "redirect:/";
- }
- @GetMapping("/delete/{id}")
- public ModelAndView remove(@PathVariable(value = "id")Integer id, ModelAndView modelAndView){
- Book book = this.bookRepository.findById(id).get();
- modelAndView.setViewName("base-layout");
- modelAndView.addObject("view", "book/remove");
- modelAndView.addObject("book", book);
- return modelAndView;
- }
- @PostMapping("/delete/{id}")
- public String remove(Book book){
- this.bookRepository.delete(book);
- return "redirect:/";
- }
- }
- //class Book:
- package library.entities;
- import javax.persistence.*;
- import javax.validation.constraints.Min;
- @Entity
- @Table(name = "books")
- public class Book {
- private Integer id;
- private String title;
- private String author;
- private Double price;
- public Book() {
- }
- public Book(String title, String author, Double price) {
- this.title = title;
- this.author = author;
- this.price = price;
- }
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- @Column(name = "title", nullable = false)
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- @Column(name = "author", nullable = false)
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- @Column(name = "price")
- @Min(value = 1)
- public Double getPrice() {
- return price;
- }
- public void setPrice(Double price) {
- this.price = price;
- }
- }
- //interface BookRepository:
- package library.repositories;
- import library.entities.Book;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- @Repository
- public interface BookRepository extends JpaRepository<Book,Integer> {
- }
- //application properties:
- #Database Properties
- spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
- spring.datasource.url = jdbc:mysql://localhost:3306/book_library?useSSL=false&createDatabaseIfNotExist=true
- spring.datasource.username = root
- spring.datasource.password =
- #JPA Properties
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- spring.jpa.properties.hibernate.format_sql=TRUE
- spring.jpa.hibernate.ddl-auto=update
- #Server Properties
- server.port=8080
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement