User 테이블 만들기
@NoArgsConstructor @Data @Table(name = "user_tb") @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(unique = true) private String username; private String password; private String email; @CreationTimestamp // pc -> db (날짜주입) private Timestamp createdAt; public User(String username, String password, String email) { this.username = username; this.password = password; this.email = email; } }
비밀번호에 length를 주면 망한다!!
DB에 암호화해서 (HASH해서) 넣으니까 length = 12로 걸면 망한다.
hash로 넣으면 와아아아악 하고 늘어나니까!
Board 테이블
@NoArgsConstructor @Data @Table(name = "board_tb") @Entity public class Board { // Entity 무조건 기본 생성자가 있어야 오류 나지 않음 @NoArgsConstructor @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String title; private String content; // @JoinColumn(name = "user_id") // 카멜 표기법 써서 DB에 직접 하고 싶으면 userId @ManyToOne // user_id 유저명의 id(유저의 pk) 필드로 만들어 줄께 private User user; // 유저 객체를 넣음 // private String username; @CreationTimestamp // pc -> db (날짜주입) private Timestamp createdAt; }
username이 변경되면 User와 Board가 연동되어서 바뀌어야 한다 → 외래키
더미 데이터
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()); insert into user_tb(username, password, email, created_at) values ('love', '1234', 'love@nate.com', now()); insert into board_tb(title, content, user_id, created_at) values('제목1','내용1', 1, now()); insert into board_tb(title, content, user_id, created_at) values('제목2','내용2', 1, now()); insert into board_tb(title, content, user_id, created_at) values('제목3','내용3', 2, now()); insert into board_tb(title, content, user_id, created_at) values('제목4','내용4', 3, now());
Share article