[스프링부트] 익명 블로그 V5 - 인증 블로그 V1 - 프로젝트 초기 세팅, 테이블, 더미데이터, View

익명 블로그 V5(인증 블로그 V1) - 1
이나겸's avatar
Nov 29, 2024
[스프링부트] 익명 블로그 V5 - 인증 블로그 V1 - 프로젝트 초기 세팅, 테이블, 더미데이터, View

프로젝트 세팅 - 라이브러리 종속성 추가

notion image
 

application.properties 파일

# 0. UTF-8 설정 server.servlet.encoding.charset=utf-8 server.servlet.encoding.force=true # 1. DB 연결 spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:test spring.datasource.username=sa # 2. hibernate 세팅 spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true # 3. 더미데이터 세팅 spring.sql.init.data-locations=classpath:db/data.sql spring.jpa.defer-datasource-initialization=true
 

User 클래스 - user_tb 테이블 생성

@AllArgsConstructor // 풀 생성자 @NoArgsConstructor // 디폴트(빈) 생성자 @Table(name = "user_tb") // 테이블명 @Getter @Entity // 엔티티, 테이블 생성 가능 public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // Autoincrement private Integer id; // Null일것도 감안해서 Integer @Column(unique = true, nullable = false) // 컬럼제약조건 unique(index 알아서 만들어질것), not null private String username; // 유저 아이디는 username으로 약속되어있음, id 아님 @Column(nullable = false) private String password; @Column(nullable = false) private String email; @CreationTimestamp private Timestamp createdAt; }
 

data.sql 파일 - 더미데이터 세팅

insert into user_tb(username, password, email, created_at) values('ssar', '1234', 'ssar@nate.com', now()); insert into user_tb(username, password, email, created_at) values('cos', '1234', 'cos@nate.com', now());
 

View 생성

  • index.mustache 파일
<!-- 글 목록 보이는 페이지 --> {{> layout/header}} <section> <!-- 표 --> <table border="1"> <!-- border로 테두리 설정 --> <tr> <!-- 표의 헤더 --> <th>번호</th> <th>제목</th> <th></th> </tr> <tr> <!-- 표의 바디 --> <td>제목1</td> <td>내용1</td> <td><a href="/board/1">상세보기</a></td> </tr> </table> </section> </body> </html>
  • header.mustache 파일
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>blog</title> </head> <body> <nav> <ul> <li> <a href="/">홈</a> </li> <li> <a href="/login-form">로그인</a> </li> <li> <a href="/join-form">회원가입</a> </li> <li> <a href="/board/save-form">글쓰기</a> </li> <li> <a href="/logout">로그아웃</a> </li> </ul> </nav> <hr>
  • save-form.mustache 파일
{{> layout/header}} <section> <form action="/board/save" method="post" enctype="application/x-www-form-urlencoded"> <input type="text" name="title" placeholder="제목"><br> <input type="text" name="content" placeholder="내용"><br> <button type="submit">글쓰기</button> </form> </section> </body> </html>
  • login-form.mustache 파일
{{> layout/header}} <section> <form action="/login" method="post"> <input type="text" name="username" placeholder="아이디"><br> <input type="text" name="password" placeholder="패스워드"><br> <button type="submit">로그인</button> </form> </section> </body> </html>
 
Share article

Nakyeom's Study