table of contents 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.
--First, create a Spring Boot project -Access SPRING INITIALIZR
--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 |
--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
--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;
}
-** [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> {
}
--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!!";
}
}
--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();
}
}
--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.
--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