2
2
.
.
6
6
.
.
2
2
M
M
y
y
S
S
Q
Q
L
L
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorial show how to use Repository to work with MySQL Database.
When you install MySQL DB it will be installed with default world database/schema which we will use in our example.
To use different Database/Schema you have to manually Create Schema/Database before connecting with Hibernate.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @RequestMapping (includes Tomcat Server)
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
MySQL Database
Enables Hibernate to work with MySQL DB
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_db_mysql (add Spring Boot Starters from the table)
Edit File: application.properties (enter DB connection parameters)
Create Package: entities (inside main package)
– Create Class: PersonEntity.java (inside package entities)
Create Package: repositories (inside main package)
– Create Interface: PersonRepository.java (inside package repositories)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
application.properties
# MYSQL DATABASE
spring.datasource.url = jdbc:mysql://localhost:3306/world?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = admin
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
# JPA / HIBERNATE
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true
MyController
http://localhost:8080/AddPerson
addPerson()
PersonEntity
MySQL
PersonEntity.java
package com.ivoronline.springboot_db_mysql.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class PersonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
public Integer age;
}
PersonRepository.java
package com.ivoronline.springboot_db_mysql.repositories;
import com.ivoronline.springboot.repository_mysql.entities.PersonEntity;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, Integer> { }
MyController.java
package com.ivoronline.springboot_db_mysql.controllers;
import com.ivoronline.springboot.repository_mysql.entities.PersonEntity;
import com.ivoronline.springboot.repository_mysql.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired
PersonRepository personRepository;
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson() {
//CREATE PERSON ENTITY
PersonEntity personEntity = new PersonEntity();
personEntity.name = "John";
personEntity.age = 20;
//STORE PERSON ENTITY
personRepository.save(personEntity);
//RETURN SOMETHING TO BROWSER
return "Person added to DB";
}
}