- Spring Boot Basics
- Hibernate ORM Basics
- Set Up
- Build a Spring boot applicaton with MySQL database together!
Spring | Spring boot | |
---|---|---|
Definition | huge Enterprise Java Framework | bootstrap a standalone, producation-grade Spring application that can be easily run |
Features | - POJOs - MVC Dependency Injection - Testing - Dependency Injection - Security ... |
- Convention over configuration. - Embedded Tomcat server ... |
-
Develop web application.
-
Send data to Front-end via RESTful API.
- RESTful API: An application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
-
Security solution to web application.
-
Communicate with database.
-
Integrate with external resources.
- POJO: Plain Old Java Object. For more information.
POJO | Java Bean |
---|---|
No extends No implements No prespecified annotation |
|
Not all POJOs are Java Beans | All Java Beans are POJOS |
POJO example:
@Component
public class ExampleListener {
@JmsListener(destination = "myDestination")
public void processOrder(String message) {
System.out.println(message);
}
}
Java Bean example:
public class ExampleListener implements MessageListener {
public void onMessage(Message message) {
...
}
}
-
Dependency Injection: Details will be talked with the hands-on coding part.
-
Tomcat: A Java-capable HTTP Server
-
HTTP Server: A piece of software that understands URLs and HTTP protocol.
Hibernate is a high-performance object/relational persistence and query service.
A way to leverage SQL in other programming languages.
Example:
SQL | Hibernate ORM |
---|---|
CREATE TABLE product | @Entity @Table(name = "product") public class Cart { … } |
1.2 What is Hibernate ORM? (http://hibernate.org/orm/what-is-an-orm/)
An object-relational mapping tool for the Java programming language.
Using Hibernate ORM will help your application to achieve Data Persistence.
- Java 8 SDK
- A Java IDE that you are comfortable to work with.
- Postman
Suggested Method 1: Use IntelliJ IDE
-
Instead of downloading the Community Edition from above, it is recommended that students get the IntelliJ IDEA Ultimate for free by signing up using their university email address here.
-
If you do not see Spring Initializr in IntelliJ, (you are probably using the Community Edition which doesn't support this and thus) you can download the Spring Assistant plug-in by going to File > Settings > Plugins > Marketplace > Search for "Spring Assistant". Click "Install", then "Restart". Now when you create a new project under File > New > Project, you should be able to see Spring Assistant (an alternative to Spring Initializr).
Add the following codes to your pom.xml
under the first hierarchy
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Method 3: Download from Spring Boot Website
-
For Windows
- Download MySQL Community Server. Please download the
zip
file.
-
Unzip the
mysql-X.X.X-winxXX.zip
to a directory. E.g.C:\Users\%USERNAME%\Downloads
-
Open command prompt. Type in the following:
$ cd C:\Users\%USERNAME%\Downloads\mysql-X.X.XX-winxXX\mysql-X.X.XX-winxXX\bin # Initialize the database. Create a root user with random password. Show the messages on console $ mysqld --initialize --console ... ...A temporary password is generated for root@localhost: xxxxxxxx // take note of this password, if forget, delete and unzipped folder, unzip it, and repeat the above steps
- To start the server:
$ mysqld --console
-
To shut down the server: Ctrl+C (but don't do it now, we need to keep the connection alive to connect to MySQL)
-
Open another Command Prompt. To start a 'Client':
$ mysql -u root -p Enter password: XXXXXXX // Enter temporary password which was generated just now Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 8.0.xx Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
- Download MySQL Community Server. Please download the
-
For MacOS
- Download MySQL Community Server. Please download the
dmg
file.
-
Follow this. Basically just open the dmg and follow the instruction. Take note of the password!!
-
It will be installed in
/usr/local/mysql
-
To start and shutdown the server:
- To start a 'Client':
$ cd /usr/local/mysql/bin ./mysql -u root -p Enter password: // Enter the root's password given during installation. You will NOT any * for maximum security Welcome to the MySQL monitor. Commands end with ; or \g. ...... mysql>
- Download MySQL Community Server. Please download the
-
For Ubuntu.
- Step 1: Open
File/Project Structure
in IntelliJ
- Step 2: Open
Database
on the right-hand side tool bar
We are good to go!
-
(GET) Retreive products according to catagory
-
(GET) Retreive products that a merchant has
-
(POST) Add new products
-
(PUT) Update product information
-
(DELETE) Delete products
- Step 0: Create Database in MySQL and configuration ini
application.properties
Windows:
cd \SpringBootIntroduction\mysql\bin
mysql -u root -p
...
mysql> CREATE DATABASE inventory;
MacOS:
cd /usr/local/mysql/bin
./mysql -u root -p
...
mysql> CREATE DATABASE inventory;
Insert some data:
insert into Product (productID, category,description,merchantID,name,price) VALUES (110,"book","extrodinary book that deserves your reading",200,"Black Swan Green", 37.9);
insert into Product (productID, category,description,merchantID,name,price) VALUES (111,"pen","hand made wood pen that is specially for you",200,"Burgendy Pen",26.8);
insert into Product (productID, category,description,merchantID,name,price) VALUES (112,"mouse","hold this mouse, you will feel you own the whole world",201,"Good Mouse", 67);
insert into Product (productID, category,description,merchantID,name,price) VALUES (113,"book","heartbreaking. The portrait of Afghan culture broadly painted",202,"The Kite Runner",37);
insert into Product (productID, category,description,merchantID,name,price) VALUES (114,"book","When a man is found murdered in an abandoned building, unflappable detective Sasagaki is assigned to the case.", 202,"Journey Under the Midnight Sun", 37);
Application.properties:
server:
port: 8080
spring:
jpa:
generate-ddl: true
show-sql: true
hibernate:
show-sql: true
ddl-auto: update
naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
properties:
hibernate:
default_schema: app
id.new_generator_mappings: true
dialect: org.hibernate.dialect.MySQL5Dialect
datasource:
url: jdbc:mysql://localhost:3306/inventory?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password:
- Step 1: Write Entities
@Entity
: name an entity
@Table(name="product")
: name a table in database
@Id
: primary key
@Column
: name a column in database
- Step 2: Create Repo
extends JpaRepository<T,D>
: JpaRepository = CrudRepository + PageAndSortingRepository
- Step 3: Write Service
ProductService
: ProductServiceImpl
, ProductServiceCacheImpl
,...
@Service
: sterotype annotation
@Autowire
: dependency injection
- Step 4: Write Controller
@RestController
: = @Controller
+ @RequestBody
@RequestMapping
: annotation