it is sub module of spring
[Link]
JPA (java persistance api) (internally use hibernate)
UserRepository User -> gettre,setter,toString
create read
update delete
delete all
custom query UserRepository (interface)
JPQL java persistance query language (line 17 , 18) and native QL
@Param autocreated class
API (Applicartion Programming Interface)
if we give @ResponseBody then
method will not retun view name but normal string on browser
Rest 1) Read -> GET (@GetMapping)
[Link] -> getter,setter,toString
BookController
• to return string to browser use @RequestBody
• if we use @RestController then @RequestBody is not required
• also @RequestMapping(value=”/books”, methos= [Link])
can be replaced by @GetMapping(“/books”)
same as above using Rest (return JSON)
edited BookController BookService
@ParamVariable
continue
2) CREATE -> POST (@PostMapping)
add to [Link] add to BookController
• use method post , select body TAb , select raw tab
• write one JSON
• and send post request
3) Delete -> @DeleteMapping
add to [Link] add to BookController
• use method delete , select body TAb , select raw tab
• on url pass id
• and send post request
4) Update Put -> @PutMapping
add to [Link] add to BookController
• use method put , select body TAb , select raw tab
• write one JSON
• on url pass id
• and send put request
ResponseEntity | Handling HttpStatus while creating Rest api
• to handle exception we can use ResponseEntity
• which is extension of HttpEntity
• that add HttpStatus code
BookController get (list of books)
• return type is ResponseEntity of List of Books -> which can return
• HttpStatus & data(list)
• if list dones not contain any books then status will be 404 not found
• and if list contains books then (200 ok)
BookController get book BookController post add book
BookController delete book BookController put update book
Connecting with Database with JPA | Rest
[Link] [Link]
• edit [Link] with adding all the necessary
dependencies
BookRepository (dao)
------------------- ------------------------- BookService ---------------------------------------------------
getBooks
create BookRepositoty object
getBookById update
deleteBook
addBook
BookController is notChanged just 1 change in getBooks() line 34
use postman for CRUD operation
• POST ,GET,,PUT,DELETE
• use -> localhost:8080/books -> post,get
• use -> localhost:8080/books/id -> delete,update,getbookByid
one to one in (Book)
Book Entity
• change author to Author type line 23 and 24
• cascade all for -> foren key
• and generate getter , setter , toString again
• We HAVE to create --->>> Author Entity
Author Entity Postman
one to one in (Author)
Book Entity Author Entity
• here in above we have given mapping in book and author so we can get author from book and book from author
• but when we getbook -> it will get author (but author has book) so author will getbook again book will getAuthor
• this will be infinite loop
• so to solve this problem @JsonManagedRefernce and @JsonBackReference is used
• @JsonManagedRefernce -> in parent (Book)
• @ JsonBackReference -> in child (Author) so it will manage that infinite loop
Thymleaf
controller
[Link] in templates folder
MyController [Link]
Conditional in thymleaf [Link]
controller
list contail more than one elemennt
beacuse of line 53 in controller
Including Thymeleaf Templates | Include | insert | replace
Replace -> will replace host tag with fragment tag
insert -> insert fragment tag as body in host tag
include -> will only copy content of fragment tag
controller
[Link] (fragment)
[Link]
line 11 -> [Link]=”htmlName::fragmentName”
[Link]=”htmlName::fragmentName”
[Link]=”htmlName::fragmentName”
thymeleaf passing dynamic value to template while including |
Controller [Link] (host) [Link] (fragment)
• to send dynamic content we have send title , subtitle from controller to [Link] ,
• [Link] is sending this dynamic content as parameter to the [Link]
• [Link] will be printing all this dynamic content and returned to [Link] and [Link] will print all the [Link] with all content
Inheriting Thymeleaf Templare | This thing used in project |
controller
[Link]
• line 2 -> other page (about,contact) content will
come here as parameter
-->> [Link]=”fragmentName(parameter)”
• line 13 -> content will be replaced here
-->> ${parameter} [Link]
• line 7 -> has link to css files
[Link]
• line 2 -> this page will be replaced by [Link]
• content (<section>) this will go to [Link] and
pasted between <h1>common header</h1> and
<h1>common footer </h1>
• to send content use
”htmlName::fragmentName(~{::tag})”
about browser