DB 확인
MySQL RDS DB 인스턴스의 데이터베이스에 연결하기
PROMPT> mysql -h <endpoint> -P <port> -u <mymasteruser> -p
database 선택
테이블 보기
테이블 정보
테이블 내용 조회
SpringBoot
참고 :
2022.08.31 - [java/spring] - [Spring] spring 프로젝트 MySQL 연결하기
build.gradle
dependencies
resources/application.properties
spring.output.ansi.enabled=always
# MySQL setting
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JDBC URL of the database.
spring.datasource.url=jdbc:mysql://{endpoint}:{port}/{schema}
spring.datasource.username={username}
spring.datasource.password={password}
# JPA
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.properties.hibernate.dialect.storage_engine=innodb
spring.sesion.store-type=jdbc
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=mysql
spring.jpa.show-sql=true
Member class
back-sto-sale-purchase/src/main/java/com/sto/salepurchase/backstosalepurchase/Member.java
package com.sto.salepurchase.backstosalepurchase;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Member {
@Id
private String id;
private String name;
private String password;
}
Repository
back-sto-sale-purchase/src/main/java/com/sto/salepurchase/backstosalepurchase/MemberRepository.java
package com.sto.salepurchase.backstosalepurchase;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MemberRepository extends JpaRepository<Member, String> {
}
Controller
back-sto-sale-purchase/src/main/java/com/sto/salepurchase/backstosalepurchase/MemberAPIController.java
package com.sto.salepurchase.backstosalepurchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RequestMapping("/member")
@RestController
public class MemberAPIController {
@Autowired
private MemberRepository memberRepository;
@RequestMapping(value = "/select", method = {RequestMethod.GET, RequestMethod.POST})
public List<Member> selectAll() {
return memberRepository.findAll();
}
@RequestMapping(value = "/insert", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json; charset=utf-8")
public Member insert(@RequestBody Map<String, String> map) {
return memberRepository.save(new Member(map.get("id"), map.get("name"), map.get("password")));
}
}
웹브라우저에서 localhost:8080/member/select url 요청 후 확인(GET방식)
참고 :
https://frogand.tistory.com/29
댓글