1. 프로젝트 생성
2. build.gradle 확인
3. DB 접속정보 확인
4. DB접속정보 application.yml에 입력(application.properties에서 확장자 변경)
5. DB 연동 테스트용 테이블 작성
CREATE TABLE temp_user(
id INT NOT NULL,
email VARCHAR(100) NULL,
nickname VARCHAR(30) NOT NULL,
primary KEY(id)
);
6.DB 연동 테스트용 파일들 작성
package company.dbproject.login.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter @Setter @ToString
public class User {
int id;
String email;
String nickname;
}
package company.dbproject.login;
import org.springframework.stereotype.Controller;
@Controller
public class LoginController {
private final LoginService loginService;
public LoginController(LoginService loginService) {
this.loginService = loginService;
}
}
package company.dbproject.login;
import org.springframework.stereotype.Component;
@Component
public class LoginService {
private final LoginMapper loginMapper;
public LoginService(LoginMapper loginMapper) {
this.loginMapper = loginMapper;
}
}
package company.dbproject.login;
import company.dbproject.login.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface LoginMapper {
}
package company.dbproject;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ComponentScan
@Configuration
public class LoginConfig implements WebMvcConfigurer {
}
#resource/template/login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:each="user:${users}">
<h1 th:text="${user.id}"></h1>
</div>
<h2 th:text="${oneUser}"></h2>
</body>
</html>
7. db 연동 테스트
LoginMapper,LoginService, LoginController의 내용을 다음과 같이 변경합니다.
package company.dbproject.login;
import company.dbproject.login.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface LoginMapper {
//insert문 실행(회원입력)
@Insert("INSERT INTO temp_user(id,email,nickname) VALUES(123,'aa@aa.a','jyanoos')")
int saveUser();
//select문 실행(회원조회)
@Select("SELECT * FROM temp_user")
List<User> listUser();
//동적쿼리 ${}로 감싸면 ''포함 안되고, #{}로 감싸면 ''이 자동으로 포함됩니다
@Select("SELECT * FROM temp_user where id=${id} AND email=#{email}")
User showUser(@Param("id")int id, @Param("email")String email);
}
ackage company.dbproject.login;
import company.dbproject.login.domain.User;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class LoginService {
private final LoginMapper loginMapper;
public LoginService(LoginMapper loginMapper) {
this.loginMapper = loginMapper;
}
//회원입력
public void joinUser(){
loginMapper.saveUser();
}
//회원리스트조회
public List<User> showUser(){
return loginMapper.listUser();
}
//회원조회
public User showOneUser(int id, String email){
return loginMapper.showUser(id,email);
}
}
package company.dbproject.login;
import company.dbproject.login.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class LoginController {
private final LoginService loginService;
public LoginController(LoginService loginService) {
this.loginService = loginService;
}
@RequestMapping("/test")
public String join(Model model){
loginService.joinUser(); //회원 입력
List<User> users = loginService.showUser(); //회원조회
model.addAttribute("users",users);
int id = 123;
String email = "aa@aa.a";
User oneUser = loginService.showOneUser(id,email);
model.addAttribute("oneUser",oneUser);
return "login";
}
}
'web > Spring' 카테고리의 다른 글
카카오 로그인 구현(4) - 카카오 토큰 받기 (0) | 2022.01.23 |
---|---|
카카오 로그인 구현(3) - 인증코드받기 (0) | 2022.01.23 |
카카오 로그인 구현(2) - 애플리케이션 설정 (0) | 2022.01.23 |
카카오 로그인 구현(1) - 애플리케이션 등록 (0) | 2022.01.23 |
개발환경설정 - spring 시작하기 (0) | 2021.07.01 |