[Eclipse] STS와 MySQL연동하기.
먼저 프로젝트를 만들때 MySQL Driver를 선택해 다운로드 받는다. 만약 프로젝트를 생성할 때 만들지 않았을 경우 다운로드 받아서해야한다.
https://downloads.mysql.com/archives/c-j/
MySQL :: Download MySQL Connector/J (Archived Versions)
Please note that these are old versions. New releases will have recent bug fixes and features! To download the latest release of MySQL Connector/J, please visit MySQL Downloads. MySQL open source software is provided under the GPL License.
downloads.mysql.com
여기에서 드라이버를 다운로드 받는데, [ Product Version]은 최신것으로 선택하고, [Operating System]은 [Platform Independent]을 선택한다.
둘중에 아무거나 다운로드 받은다음에, 압축을 푼다. 그다음 프로젝트에서 폴더 [lib]을 생성하는데, [src]디렉터리와 비슷한 위치에 만든다.
다음에 압축을 푼 파일에서 [mysql-connector-java-<version>.jar]을 복사해서 [lib]폴더안에 넣는다.
그 다음에 [Java Build Path][Libraries]탭에서 [Classpath][Add JARs]누르고 [lib]안에 있는 [jar]파일을 선택하고 [Apply and Close]버튼을 클릭한다.
[Modulepath]는 모듈화된 프로젝트에 사용되기 때문에, [Classpath]를 선택 후 드라이버를 추가하는것이 좋다.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.mysql:mysql-connector-j' //mysql
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
compileOnly 'org.projectlombok:lombok' //롬복
annotationProcessor 'org.projectlombok:lombok' //롬복
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' //jpa추가안하면 @Entity어노테이션에서 오류가뜬다.
build.gradle을 다음과 같이 작성한후 새로고침을 해준다.
spring.datasource.url=jdbc:mysql://localhost:3306/본인db이름 //mysqlurl
spring.datasource.username=아이디 //아이디
spring.datasource.password=비밀번호 // 비밀번호
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mysql
# JPA
spring.jpa.hibernate.ddl-auto=update //만든 엔티티로 테이블을 생성할 때 설정하는 규칙이다.
[application.properties]에는 다음과 같이 만든다. db이름은 mysql에서 만들어주어야한다.
[SHOW DATABASES;]을 친다음 번개모양을 치면 DB이름들이 나타난다.
나는 [CREATE DATABASE MYDATA;] 실행시켜서 [mydata]라는 DB이름을 만들었다.
import java.time.LocalDateTime;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(length = 200)
private String subject;
@Column(columnDefinition = "TEXT")
private String content;
private LocalDateTime createDate;
}
그다음 엔티티 클래스를 만들어, 데이터베이스 테이블과 매핑시켜준다.
테이블을 확인하면 매핑된것을 확인할 수 있다.
'IDE > Eclipse' 카테고리의 다른 글
[Eclipse] STS 설치하기 및 설정하기 (0) | 2024.08.06 |
---|---|
[JAVA] Elicpse import 에러 날때 (0) | 2024.06.11 |
[JAVA] 이클립스 설치와 Tomcat연동 (0) | 2024.06.11 |
댓글