spring boot와 mySQL 연결을 시도 했지만
데이터베이스에 관한 지식이 별로 없어서 성공하지는 못했습니다ㅠㅠ
* myBatis 등 mySQL과 웹을 연결하는 프레임워크를 공부할 것
더 공부한 뒤 참조하기 위해 글을 남깁니다.
* 언어 : spring boot
* 프로젝트 : gradle
* 데이터베이스 : mySQL
.domain / Member.java
package DSC4.cafein.domain;
import javax.persistence.*;
@Entity
@Table(name="member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String email;
private String username;
private String password;
public Member() {
}
public Member(String username,String password){
this.email = username;
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
.repository / MemberRepository.java
package DSC4.cafein.repository;
import DSC4.cafein.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MemberRepository extends JpaRepository<Member,String> {
Member findByUsernameAndPassword(String username,String password);
}
//public class MemberRepository {
//}
.service / MemberService.java
package DSC4.cafein.service;
import DSC4.cafein.domain.Member;
import DSC4.cafein.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MemberService {
@Autowired
private MemberRepository repo;
public Member login(String username, String password){
Member member = repo.findByUsernameAndPassword(username,password);
return member;
}
}
build.gradle
plugins {
id 'org.springframework.boot' version '2.3.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'DSC4'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//implementation 'android.arch.persistence.room:runtime:1.1.1'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'com.google.firebase:firebase-admin:6.8.1'
//annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('mysql:mysql-connector-java')
}
test {
useJUnitPlatform()
}
-> 사실 여기는 firebase와 mysql코드가 섞여있다.
(아래의 코드는 파이어베이스 코드이니 무시 가능)
implementation 'com.google.firebase:firebase-admin:6.8.1'