상세 컨텐츠

본문 제목

인텔리J mariaDB연동

Java TIL

by 개발일지작성 2023. 11. 14. 10:35

본문

728x90

build.gradle -> dependencies

implementation 'org.mariadb.jdbc:mariadb-java-client:3.0.3'

 추가

 

html 에서 name속성 값으로 서버로 전송할 변수 이름을 정하기

 

entity로 DB실행 시 실행 할 코드 작성

package com.study.member.entity;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Setter
@Getter
@Table(name = "member_table")
public class MemberEntity {
    @Id // pk 지정
    @GeneratedValue(strategy = GenerationType.IDENTITY) // auto_increment
    private Long id;

    @Column(unique = true) // unique 제약조건 추가
    private String memberEmail;

    @Column
    private String memberPassword;

    @Column
    private String memberName;
}

컨트롤러에서 html에 있는 input name 을 가져오기 위한 코드 작성

@PostMapping("/member/save")
public String save(@ModelAttribute MemberDTO memberDTO) {
    System.out.println("MemberController.save");
    System.out.println("memberDTO = " + memberDTO);
    memberService.save(memberDTO);
    return "index";
}

repository 에 코드 작성

package com.study.member.repository;

import com.study.member.entity.MemberEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<MemberEntity, Long> {
}

service에 작성

package com.study.member.service;

import com.study.member.dto.MemberDTO;
import com.study.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MemberService {
    private final MemberRepository memberRepository;
    public void save(MemberDTO memberDTO) {
    }
}

관련글 더보기