본문 바로가기

스프링 부트/웹MVC

셋업

반응형

 

프로젝트 생성

 

 

 

mysql db 생성

 

 

 

application.properties 설정

#
#
# lombok 설치와 mysql utf-8 설정 확인

# MySQL 설정
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# DB Source URL
spring.datasource.url=jdbc:mysql://localhost:3306/web01

spring.datasource.username=root
spring.datasource.password=1234

# jpa 설정
spring.jpa.hibernate.ddl-auto=create

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

# jpa의 넘버링 전략을 따라가지 않음
spring.jpa.hibernate.use-new-id-generator-mappings=false

# 클래스에 있는 필드 이름들 그대로 테이블을 만듬
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

 

 

 

RestController 하나 만들어서 로컬 서버 접속 확인

package com.cos.web01.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebRestController {

	@GetMapping({ "", "/" })
	public String index() {
		return "hello";
	}
}

 

 

 

 

 

 

 

유저 엔티티 생성

package com.cos.web01.service.domain.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED) // 기본 생성자 생성 방지
@Data
@Entity
public class User {

	@Id // primary key
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 값 생성이 데이터베이스 설정을 따라감, mysql을 쓰고 있으므로 auto_increment로 됨
	private Long id;

	@Column(length = 20, unique = true, nullable = false)
	private String userId;

	@Column(length = 50, unique = true, nullable = false)
	private String password;

	private String userName;

	@Builder
	public User(String userId, String password, String userName) {
		this.userId = userId;
		this.password = password;
		this.userName = userName;
	}
}

 

 

application.properties에서 ddl 설정해준대로 서버를 실행시키면

 

 

 

UserRepositroy 생성

package com.cos.web01.service.domain.user;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

}

 

 

테스트 코드

package com.cos.web01;

import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.cos.web01.service.domain.user.User;
import com.cos.web01.service.domain.user.UserRepository;

@SpringBootTest
public class UserTest {

	@Autowired
	UserRepository userRepository;

	@Test
	public void test100_유저추가() {
		userRepository.save(User.builder().userId("테스트아이디").password("test").userName("테스트네임").build());

		List<User> userList = userRepository.findAll();

		System.out.println(userList.toString());

//		User user = userList.get(0);

		for (User user : userList) {
			System.out.println(user.toString());
		}

	}

}

 

assertThat이 되지않아 임시방편으로 System.out.println으로 하였다

 

 

 

 

반응형