Uploaded by arun kumar

BookManagementSpring boot for beginners

advertisement
BookManagement.java
import jakarta.persistence.*;
@Entity
@Table(name="book_mgmt")
public class BookManagement {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="book_mgmt_id")
private Long id;
@Column(name="book_name")
private String bookName;
private int bookId;
private String authName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getAuthName() {
return authName;
}
public void setAuthName(String authName) {
this.authName = authName;
}
public BookManagement(String bookName, int bookId, String authName)
{
super();
this.bookName = bookName;
this.bookId = bookId;
this.authName = authName;
}
public BookManagement() {
}
}
BookMgmtRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import
com.arun.batch4119.sb.bms.SpringBootB4119BMSEx1.model.BookManagement
;
@Repository
public interface BookMgmtRepository extends
JpaRepository<BookManagement,Long>{
}
BookMgmtController.java
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import
com.arun.batch4119.sb.bms.SpringBootB4119BMSEx1.model.BookManagement
;
import
com.arun.batch4119.sb.bms.SpringBootB4119BMSEx1.repository.BookMgmtR
epository;
@RestController
@RequestMapping("/book")
public class BookMgmtController {
@Autowired
BookMgmtRepository bmr;
@PostMapping("/save")
public void saveBook(@RequestBody BookManagement bm) {
bmr.save(bm);
}
@RequestMapping("/details")
public List<BookManagement> getBooks() {
return bmr.findAll();
}
@RequestMapping("/update/{id}/{bookName}")
public void updateBook(@PathVariable("id")Long
id,@PathVariable("bookName")String bname) {
BookManagement bmt = bmr.findById(id).get();
bmt.setBookName(bname);
bmr.save(bmt);
}
@RequestMapping("/details/{id}")
public BookManagement getById(@PathVariable("id")Long id) {
return bmr.findById(id).get();
}
@RequestMapping("/delete/{id}")
public void deleteBook(@PathVariable("id")Long id) {
bmr.deleteById(id);
}
}
-application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/batch4119bms
spring.datasource.username = root
spring.datasource.password =
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen
database
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL8Dialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
Download