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";
}
}