r/learnjava • u/rank_guru • Nov 09 '20
Spring Boot REST API Using JPA, Hibernate, MySQL Tutorial
Here I've explained how to develop Spring Boot REST API with MySQL database.
https://javatodev.com/spring-boot-mysql/
Technologies Stack Used,
Java 1.8
Spring Boot: 2.3.4.RELEASE
JPA
MySQL
Lombok
Gradle
Additionally REST API covered full CRUD including exposing multiple API endpoints.
Hope this article could help someone who is reading about Spring Boot.
3
u/tangara888 Nov 09 '20
Thanks for the tutorial. Is it possible for you add in how to handle the file upload with images and how it is stored in the database in the correct way? Tks
3
u/rank_guru Nov 09 '20
Im writing one with storing in database and AWS S3, will share soon,
2
u/tangara888 Nov 10 '20 edited Nov 10 '20
Btw, I tried out your tutorial but is gives me Duplicate field Lend.status line 17 and line 21 but if I keep the Enum and delete the other one is ok. Is there something you have overlooked?
And your LibraryService even though it is using Lombok eclipse still asking me to add getter and setter. Why is that so?
1
u/rank_guru Nov 10 '20
I didnt’t got you correctly, can you add any stack trace or image of issue here ?
2
u/tangara888 Nov 10 '20
So there is actually no need to use String status since you already have Enum and it can be converted to String
2
u/rank_guru Nov 10 '20
Yeah no need of adding string status, normally we dont keep statuses like constants in Strings , enum is the best option to keep those, additionally i explained how to use both ordinal and string enum types to store. Keep eye on that,
2
u/tangara888 Nov 10 '20
There are still many red squiggly lines appeared and the ide asked for getStatus(), setMember() etc etc.
How do I post the picture here?
Member member = memberForId.get(); if (member.getStatus() != MemberStatus.ACTIVE) { throw new RuntimeException("User is not active to proceed a lending."); } Optional<Lend> burrowedBook = lendRepository.findByBookAndStatus(bookForId.get(), LendStatus.BURROWED); if (!burrowedBook.isPresent()) { booksApprovedToBurrow.add(bookForId.get().getName()); Lend lend = new Lend(); lend.setMember(memberForId.get()); lend.setBook(bookForId.get()); lend.setStatus(LendStatus.BURROWED); lend.setStartOn(Instant.now()); lend.setDueOn(Instant.now().plus(30, ChronoUnit.DAYS)); lendRepository.save(lend); }1
u/rank_guru Nov 11 '20
see you've correctly configured lombok with your IDE. or else try without lombok and generate Getter and Setter manually in your classes.
2
1
u/rank_guru Nov 10 '20
We dont need getter and setter for service, have you added @ReuiredArgsConstructor ? and @Service annotations. That should be enough
2
u/tangara888 Nov 10 '20 edited Nov 10 '20
It is still showing ...as in the ide still requires the getter and setter...yes I have added the annotations etc
And also at the LibraryController the final Library service the ide has asking to initialized.
One question - why @autowired is no being used through out?
1
2
u/Jhonrak Nov 09 '20
How can I make the STS show me more versions of Springboot? I want to create a project with Spring 2.3.2 but the closest one it shows available is 2.3.5
2
u/rank_guru Nov 10 '20
I don't think It will allow you to use older versions. since all the places use https://start.spring.io/ - Spring initialize internally, and for the moment they allow only for the latest version.
And I don't recommend using old versions too.
But if you need to use an older one, just change the boot version after generating. But in that case, you could face issues.
2
2
u/Nix-X Nov 10 '20
Are there prerequisites to be able to understand this? I already know some basics of building REST apis with Spring Boot.
1
2
u/tleipzig Nov 10 '20
Hello, nice article! Maybe you could save some time using https://bootify.io, setting up the database model? All the best, Thomas
2
u/rank_guru Nov 10 '20
Just checked that app. it's an awesome tool. will write about this tool in the future after working with it. Thanks for sharing.
2
2
Nov 10 '20
Thank you for this! Can you add Mochito and Junit tests?
2
1
u/rank_guru Nov 09 '20 edited Nov 09 '20
Both are same, start with maven, you could understand same when you’re working since the concept is same but there few pros and cons in both. Good luck u/curiousaboutweird
2
u/curiousaboutweird Nov 09 '20
Wow thanks. This is exactly what I was studying nowadays except using Gradle instead of Maven. It'll be a good practice for me.
2
2
2
2
5
u/JudoboyWalex Nov 09 '20
Thank you for this! I notice you just started blogging about Spring. Keep up the good work bro!