Project UI -
Project Structure-
/Springboot-curd-thymeleaf/src/main/java/com/nima/springpractice/SpringbootCurdThymeleafApplication.java
package com.nima.springpractice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootCurdThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCurdThymeleafApplication.class, args);
}
}
/Springboot-curd-thymeleaf/src/main/java/com/nima/springpractice/BookController.java
package com.nima.springpractice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import com.nima.springpractice.entity.Book;
import com.nima.springpractice.service.BookService;
@Controller
public class BookController {
@Autowired
BookService service;
@GetMapping("/")
public String findAll(Model model) {
model.addAttribute("books", service.findAllbooks());
return "all-books";
}
@GetMapping("/add")
public String lunchAddBookPage(Model model) {
model.addAttribute("book", new Book());
return "add-book";
}
@PostMapping("/addbook")
public String createbook(Book book){
service.Addbook(book);
return "redirect:/";
}
@GetMapping("/edit/{id}")
public String luncheditbooks(Model model,@PathVariable("id") int id) {
model.addAttribute("books", service.findBookbyId(id));
return "edit-book";
}
@PostMapping("/updatebook")
public String updatebook(Book book) {
service.Updatebook(book);
return "redirect:/";
}
@GetMapping("/delete/{id}")
public String deletebook(@PathVariable("id") int id) {
service.deleteById(id);
return "redirect:/";
}
@GetMapping("/test")
public String Test() {
Book st=new Book();
st.setAuthor("nima");
st.setName("nimai pradhan");
st.setNoOfpages(5);
st.setId(12);
service.Addbook(st);
return "index";
}
}
/Springboot-curd-thymeleaf/src/main/java/com/nima/springpractice/entity/Book.java
package com.nima.springpractice.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column
String name;
@Column
String author;
@Column
String publication;
@Column
int noOfpages;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublication() {
return publication;
}
public void setPublication(String publication) {
this.publication = publication;
}
public int getNoOfpages() {
return noOfpages;
}
public void setNoOfpages(int noOfpages) {
this.noOfpages = noOfpages;
}
}
/Springboot-curd-thymeleaf/src/main/java/com/nima/springpractice/repository/BookRepository.java
package com.nima.springpractice.repository;
import javax.persistence.Entity;
import org.springframework.data.jpa.repository.JpaRepository;
import com.nima.springpractice.entity.Book;
public interface BookRepository extends JpaRepository<Book, Integer> {
}
/Springboot-curd-thymeleaf/src/main/java/com/nima/springpractice/service/BookService.java
package com.nima.springpractice.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nima.springpractice.entity.Book;
import com.nima.springpractice.repository.BookRepository;
@Service
public class BookService {
@Autowired
BookRepository repository;
public List<Book> findAllbooks(){
return (List<Book>) repository.findAll();
}
public Book findBookbyId(int id) {
Optional<Book> result=repository.findById(id);
if(result.isPresent()) {
return result.get();
}
return new Book();
}
public Book Addbook(Book book) {
return repository.save(book);
}
public Book Updatebook(Book book) {
Optional<Book> result=repository.findById(book.getId());
Book existing=result.get();
existing.setAuthor(book.getAuthor());
existing.setName(book.getName());
existing.setNoOfpages(book.getNoOfpages());
existing.setPublication(book.getPublication());
repository.save(existing);
return existing;
}
public void deleteById(int id) {
repository.deleteById(id);
}
}