Gradle + SpringBoot+ GraphQL
์นดํ ๊ณ ๋ฆฌ | ๊ธฐ์ |
---|---|
ํ๊ทธ | |
์์ ์ผ | Aug 3, 2024 |
์์ฑ์ผ | Mar 19, 2024 |
๐ก
์ ์ฒด ์์ค ์ฝ๋๋ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์.
ํ๋ก์ ํธ ํ๊ฒฝ
- VSCode 1.86.0
- Gradle 8.6
- SpringBoot 3.2.3
- java 17
- mariaDB (feat. dbeaver)
์ค์น
Database
- DBeaver ์ค์น
- โachroโ ์ด๋ฆ์ Database ์์ฑ (๋ณธ ์์ ๋ MariaDB ๊ธฐ๋ณธ ์ค์ ์ด์ฉ)
JDK 17 ์ค์น
https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html
VSCode Extensions
- Extension Pack for Java
vscjava.vscode-java-pack
- Gradle for Java
vscjava.vscode-gradle
- Spring Boot Tools
vmware.vscode-spring-boot
- Spring Initializr Java Support
vscjava.vscode-spring-initializr
VSCode settings
// ~/.vscode/settings.json
{
"java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "automatic"
}
ํ๋ก์ ํธ ์์ฑ
Ctrl + Shift + P
orCommand + Shift + P
Java: Create Java Projectโฆ
์ ํ- Select the project type โ Spring Boot
- Select prject type. โ Gradle project
- Specify Spring Boot version โ 3.2.3
- Specify project language. โ Java
- Input Group Id for your project. โ com.croot
- Input Artifact Id for your project. โ achro
- Specify packaging type. โ Jar
- Specify Java version โ 17
- Search dependencies. โ none
Gradle ์ค์
~/build.gradle
// build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.3'
id 'io.spring.dependency-management' version '1.1.4'
}
java {
group = 'com.achro'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
// Spring Boot
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:15.0.0'
testImplementation 'com.graphql-java-kickstart:graphql-spring-boot-starter-test:15.0.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// DB
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client:3.3.3'
}
Gradle ์์กด์ฑ ๊ด๋ฆฌ ์์ฑ
- `complieClasspath` : ํ๋ก์ ํธ ์์ค ์ฝ๋ ์ปดํ์ผ ์ ํ์ํ ๋ชจ๋ ํด๋์ค, ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- `runtimeClasspath` : ํ๋ก์ ํธ ์คํ ์ ํ์ํ ๋ชจ๋ ํด๋์ค, ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- `runtimeOnly` : ๋ฐํ์ ์์ ์๋ง ํ์ํ ๊ฒฝ์ฐ
- `compileOnly` : ์ปดํ์ผ ์์ ์๋ง ํ์ํ ๊ฒฝ์ฐ
- `implementation` : ์ปดํ์ผ ๋ฐ ๋ฐํ์ ๋ชจ๋ ํ์
ํ๋ก์ ํธ ์ค์
~/src/main/resources/application.properties
# application.properties
server.port=8000
spring.application.name=achro
graphql.servlet.enabled=true
graphql.altair.enabled=true
graphql.playground.enabled=true
# DB Properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/achro
spring.datasource.username=root
spring.datasource.password=
~/src/main/resources/graphql/schema.graphqls
// schema.graphqls
schema{
query: Query
}
type Member{
id: ID!
name: String!
age: Int!
}
type Query{
getMember(id: ID!): Member
getAllMembers: [Member]
}
๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ ๋ฐ ์์ค์ฝ๋
~/src/main/java/com/croot/achro
|- model/
|- repository/
|- service/
`- ApiApplication.java
model/
`Member.java`
```java // ~/src/main/java/com/croot/achro/model/Member.java package com.croot.achro.model; import lombok.Getter; import lombok.Setter; import jakarta.persistence.*; @Entity @Table(name="MEMBER") @Getter @Setter public class Member { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "MEMBER_ID") private Integer id; @Column(name = "MEMBER_NAME") private String name; @Column(name = "MEMBER_AGE") private Integer age; } ```repository/
`MemberRepository.java`
```java // ~/src/main/java/com/croot/achro/repository/MemberRepository.java package com.croot.achro.repository; import com.croot.achro.model.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface MemberRepository extends JpaRepository<Member, Integer> { } ```service/
`MemberService.java`
```java // ~/src/main/java/com/croot/achro/service/MemberService.java package com.croot.achro.service; import graphql.kickstart.tools.GraphQLQueryResolver; import java.util.List; import org.springframework.stereotype.Service; import com.croot.achro.model.Member; import com.croot.achro.repository.MemberRepository; @Service public class MemberService implements GraphQLQueryResolver { private final MemberRepository memberRepository; public MemberService(MemberRepository memberRepository) { this.memberRepository = memberRepository; } public List`LoadDataService.java` : ์ด๊ธฐ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ธฐ ์ํ ์๋น์ค
```java // ~/src/main/java/com/croot/achro/service/LoadDataService.java package com.croot.achro.service; import com.croot.achro.model.Member; import com.croot.achro.repository.MemberRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jakarta.annotation.PostConstruct; @Service public class DataLoaderService { @Autowired private MemberRepository memberRepository; @PostConstruct public void load() { // ๋ฐ์ดํฐ ์ถ๊ฐ for (int i = 1; i <= 10; i++) { Member member = new Member(); member.setName("Member " + i); // Member ์ด๋ฆ ์ค์ member.setAge(i * 10); // Member ๋์ด ์ค์ memberRepository.save(member); } } } ```
Previous Feb 29, 2024
« ์ ๋ก ํธ๋ฌ์คํธ ๊ฐ์ด๋๋ผ์ธ
« ์ ๋ก ํธ๋ฌ์คํธ ๊ฐ์ด๋๋ผ์ธ
Mar 19, 2024 Next
Proxy์ Reflect »
Proxy์ Reflect »