8 things to insert into DB using Spring Boot and JPA

8 things to insert into DB using Spring Boot and JPA

table of contents 0. Purpose of this time

  1. Create a Spring Boot project
  2. Prepare a DB in Mysql
  3. PropertiesFile settings
  4. Preparation of Entity class
  5. Preparation of Repository class
  6. Preparation of Service class
  7. Preparation of Controller class
  8. Confirm using curl
  9. End

0. Purpose of this time

-** When I was just starting Spring Boot, I couldn't just insert into the DB using JPA. I didn't have an article that even cats could understand, so I'll write for myself at that time. ** ** --There is no explanation about special JPA, it's just an article about inserting into DB.

1. Create a Spring Boot project

--First, create a Spring Boot project -Access SPRING INITIALIZR

2. Prepare a DB in Mysql

--Prepare a JPA test DB in MySql. ――This time, we prepared with such a configuration. --DB name is ** [jpa_test] **

CREATE TABLE `members` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NULL,
  PRIMARY KEY (`id`));

id name
1 hoge

3. PropertiesFile settings

--Open IDE --Open ** [application.properties] ** of ** [resources] ** directly under main in src ――Write the following three lines of magic and connect it to MySql. - spring.datasource.url=jdbc:mysql://localhost:3306/jpa_test --spring.datasource.username = MySql username --spring.datasource.password = password for MySql

4. Preparation of Entity class

--Create ** [domain] ** Package directly under main in src. -** [domain] ** Create ** [Members.java] ** in Package and write the following code.

package com.jpa.demojpa.domain;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Data
@Entity
public class Members {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
}

5. Preparation of Repository class

-** [domain] ** Create ** [MembersRepository.java] ** in Package and write the following code.

package com.jpa.demojpa.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MembersRepository extends JpaRepository<Members, Integer> {
}

6. Preparation of Service class

--Create a ** [service] ** Package directly under main in src. -** [service] ** Create ** [MembersService.java] ** in Package and write the following code.

package com.jpa.demojpa.service;

import com.jpa.demojpa.domain.Members;
import com.jpa.demojpa.domain.MembersRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MembersService {
    private final MembersRepository membersRepository;

    public String create() {
        Members members = new Members();
        members.setName("hoge");
        membersRepository.save(members);
        return "success!!";
    }
}

7. Preparation of Controller class

--Create ** [web] ** Package directly under main in src. -** [web] ** Create ** [MembersController.java] ** in Package and write the following code.

package com.jpa.demojpa.web;

import com.jpa.demojpa.service.MembersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/members")
@RequiredArgsConstructor
public class MembersController {
    private final MembersService membersService;

    @PostMapping
    public String create() {
        return membersService.create();
    }
}

8. Confirm using curl

--Open a terminal and check with the following curl.

curl http://localhost:8080/members

-** [success !!] ** should be returned, please check MySql when it is returned.

9. End

--This is the end of 8 things *** for inserting into DB using Spring Boot and JPA. ――After that, ** [findOne] **** [save] **** [delete] ** is prepared in Jpa Repository, so please use it.

membersRepository.findOne(1);
membersRepository.save(members);
membersRepository.delete(1);

Recommended Posts

8 things to insert into DB using Spring Boot and JPA
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
Connect to database with spring boot + spring jpa and CRUD operation
Insert data into DB using Yaml file
Add spring boot and gradle to eclipse
Apply Twitter Bootstrap 4 to Spring Boot 2 using Webjars
Plans to support JDK 11 for Eclipse and Spring Boot
Try using DI container with Laravel and Spring Boot
Spring boot Things that beginners tend to have (@ComponentScan)
[Introduction to Spring Boot] Submit a form using thymeleaf
Try Spring Boot from 0 to 100.
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
How to call and use API in Java (Spring Boot)
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Spring Boot DB related tips
Introduction to Spring Boot Part 1
Try using Spring Boot Security
Implement REST API with Spring Boot and JPA (domain layer)
Spring Boot DB connection pool
I tried to get started with Swagger using Spring Boot
How to control transactions in Spring Boot without using @Transactional
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
[Java] Get data from DB using singleton service in Spring (Boot)
Uploading and downloading files using Ajax in Spring Boot (without JQuery)
Things to note when using Spring AOP in Jersery resource classes
Things to consider when running a specified job using Spring Batch
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
Image Spring Boot app using jib-maven-plugin and start it with Docker
Spring Boot Tutorial Using Spring Security Authentication
[How to install Spring Data Jpa]
How to set Spring Boot + PostgreSQL
Spring profile function, and Spring Boot application.properties
How to use ModelMapper (Spring boot)
[Rails] Reflection to db using seeds.rb
Java beginner tried to make a simple web application using Spring Boot
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)
Steps to create a simple camel app using Apache Camel Spring Boot starters
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
Asynchronous processing with Spring Boot using @Async
How to split Spring Boot message file
How to insert icons using Font awesome
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Sample code for DB control by declarative transaction in Spring Boot + Spring Data JPA
Sample to batch process data on DB with Apache Camel Spring Boot starters