์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ์ฐ์ ์์ํ
- ๋ฆฌ์กํธ๋ค์ดํฐ๋ธ
- ๊ทธ๋ฆฌ๋์๊ณ ๋ฆฌ์ฆ
- ๊ทธ๋ฆฌ๋
- ํ๋ก ํธ์ค๋
- ๋ฐฑ์ค1436
- ์ปด๊ณต์
- ํ์ด์ฌ
- ์๊ณ ๋ฆฌ์ฆ
- SSE
- ์ปดํจํฐ๊ณตํ
- boj11653
- ์คํ๋ง
- ๊ฐ๋ฐ์
- ์๋ฃ๊ตฌ์กฐ
- ๋ชจ๋ฐ์ผ์ฑํ๋ก๊ทธ๋๋ฐ
- ๋จ์ํ ์คํธ
- spring
- ChatGPT
- ์ปด๊ณต
- ์น๊ฐ๋ฐ๊ธฐ๋ก
- ๋ฐฑ์คํ์ด
- ํ๋ก๊ทธ๋๋ฐ
- ์ดํญ๊ณ์
- ์น๊ฐ๋ฐ
- ์ฝ๋ฉ
- ๋ฐฑ์ค
- ๋ฆฌ์กํธ
- ์ฝ๋ฉํ ์คํธ
- ๋ฐฑ์๋
- Today
- Total
๐ป๐ญ๐ง๐
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 1. DB ์ค๊ณ ๋ฐ domain ์ฝ๋ ์์ฑ ๋ณธ๋ฌธ
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 1. DB ์ค๊ณ ๋ฐ domain ์ฝ๋ ์์ฑ
adorableco 2024. 1. 3. 00:09ํ๋ก์ ํธ ๊ฐ๋ต ์๊ฐ : ํจ๊ป ์ด๋ ๊ฒฝ๊ธฐ๋ฅผ ํ ์ธ์์ ๋ชจ์งํ๋ ์๋น์ค
๋จผ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ์์ ๋ฐฐ์ ๋ ๋ด์ฉ๋ค์ ๋ค์ ๋ณต๊ธฐ์์ผ ERD ๋ฅผ ์์ฑํ๋ค.
์ํ๊ตฌ์กฐ๊ฐ ๋๋๊ฑด ์ข์ง ์๋ค๊ณ ํ๋๋ฐ.. ์๋ฌด๋ฆฌ ์๊ฐํด๋ ๋ค๋ฅธ ๋ฐฉ์์ด ๋ ์ค๋ฅด์ง ์์๋ค.
ERD ๋ง ๋ดค์ ๋ USER๊ฐ POST์ MATCH ๋ ์ํฐํฐ ๋ชจ๋์ ๊ด๊ณ๊ฐ ์๋ ๊ฒ์ด ์ด์ํด๋ณด์ด๋๋ฐ POST ๋ฅผ ๋ฑ๋กํ USER ์ ๊ฒฝ์ฐ์๋ POST -> MATCH ์ ๊ฐ์ด MATCH ์ํฐํฐ๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ์ง๋ง, ๋จ์ํ ๋ฑ๋ก๋ ๋ชจ์ง๊ธ์ ํตํด MATCH ์ ์ฐธ๊ฐ ์ ์ฒญ์ ํ ๊ฒฝ์ฐ์๋ PARTICIPATION -> MATCH ์ ํตํด ์ ๊ทผํ๋ ๋ฐฉ๋ฒ ๋ฟ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
๊ทธ๋ผ ์์ MATCH๋ฅผ ์์ ..? ๊ธฐ์๋ MATCH ์ํฐํฐ ์์ฒด๊ฐ ๊ฐ๋ ํ๋๋ค์ด ๋ง์๊ธฐ์ ๊ทธ๋ฅ ๋ฐ๋ก ๋บ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ค์ ์ฐจ๋ก๋ก domain ์ฝ๋๋ฅผ ์์ฑํ์๋ค.
1. User.java
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="User_id",updatable = false)
private Long userId;
@Column(name="Name", nullable = false)
private String name;
@Column(name="Email")
private String email;
@Column(name="Sex")
private char gender;
@Column(name = "Age")
private String age;
@Column(name="Sex_visible")
@ColumnDefault("1")
private boolean genderVisible;
@Column(name="Age_visible")
@ColumnDefault("1")
private boolean ageVisible;
@Column(name = "winningRate")
private float winningRate;
@Column(name = "Late_rate")
private float lateRate;
@Builder
public User(Long userId, String name, String email,
char gender, String age, boolean genderVisible,
boolean ageVisible, float winningRate, float lateRate) {
this.userId = userId;
this.name = name;
this.email = email;
this.gender = gender;
this.age = age;
this.genderVisible = genderVisible;
this.ageVisible = ageVisible;
this.winningRate = winningRate;
this.lateRate = lateRate;
}
}
2. Post.java
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "posts")
public class Post extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Post_id", updatable = false)
private Long postId;
@ManyToOne
@JoinColumn(name = "User_id")
@CreatedBy
private User user;
@OneToOne
@JoinColumn(name = "Tag_id")
private Tag tag;
@OneToOne
@JoinColumn(name = "Match_id")
private Match match;
@Column(name = "Title", nullable = false)
private String title;
@Column(name = "Content", nullable = false)
private String content;
@Builder
public Post(Long postId, User user, Tag tag, Match match, String title, String content) {
this.postId = postId;
this.user = user;
this.tag = tag;
this.match = match;
this.title = title;
this.content = content;
}
}
3. Tag.java
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "tags")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Tag_id",updatable = false)
private Long tagId;
@ManyToOne
@JoinColumn(name="Post_id")
private Post post;
@Column(name = "Sex")
private String genderType;
@Column(name = "Level")
private String levelType;
@Column(name = "Age")
private String ageType;
@Builder
public Tag(Long tagId, Post post, String genderType, String levelType, String ageType) {
this.tagId = tagId;
this.post = post;
this.genderType = genderType;
this.levelType = levelType;
this.ageType = ageType;
}
}
4. Match.java
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "matches")
public class Match {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Match_id",updatable = false)
private Long matchId;
@ManyToOne
@JoinColumn(name = "Post_id")
private Post post;
@ManyToOne
@JoinColumn(name = "User_id")
private User user;
@Column(name = "Category",nullable = false)
private String category;
@Column(name = "Current_head_cnt",nullable = false)
private int currentHeadCnt;
@Column(name = "Head_cnt",nullable = false)
private int headCnt;
@Column(name = "Place", nullable = false)
private String place;
@Column(name = "Match_date",nullable = false)
private Timestamp matchDate;
@Column(name = "Attendance_count",nullable = false)
private int attendanceCnt;
@Builder
public Match(Long matchId, Post post, User user,
String category, int currentHeadCnt, int headCnt,
String place, Timestamp matchDate, int attendanceCnt) {
this.matchId = matchId;
this.post = post;
this.user = user;
this.category = category;
this.currentHeadCnt = currentHeadCnt;
this.headCnt = headCnt;
this.place = place;
this.matchDate = matchDate;
this.attendanceCnt = attendanceCnt;
}
}
5.Participation.java
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "participations")
public class Participation{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Participation_id",updatable = false)
private Long participationId;
@ManyToOne
@JoinColumn(name = "User_id")
private User user;
@ManyToOne
@JoinColumn(name = "Match_id")
private Match match;
@Column(name = "Status", nullable = false)
@ColumnDefault("wait")
private String status;
@Column(name = "Attendance", nullable = false)
@ColumnDefault("0")
private boolean attendance;
@Column(name = "Iswin", nullable = false)
@ColumnDefault("0")
private boolean isWin;
@Builder
public Participation(Long participationId, User user, Match match,
String status, boolean attendance, boolean isWin){
this.participationId = participationId;
this.user = user;
this.match = match;
this.status = status;
this.attendance = attendance;
this.isWin = isWin;
}
}
6.BaseEntity
โก๏ธ ์๋ ์์ฑ/์์ ๋ ์ง๋ฅผ ์์๋ฐ๊ธฐ ์ํด ๋ง๋ ํด๋์ค
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
@Column(name = "Created_date", nullable = false)
@CreatedDate
@DateTimeFormat(pattern = "yyyy-MM-dd/HH:mm:ss")
private Timestamp createdDate;
@Column(name = "Modified_date", nullable = false)
@LastModifiedDate
private Timestamp modifiedDate;
}
๊ทผ๋ฐ ์ฌ์ค @ManytoOne
, @ManytoMany
๋ฑ์ด ์ ํํ ์ด๋ป๊ฒ ์ด์ฉ๋๋์ง ์์ง ๋ชจ๋ฅด๊ณ ์๋ ๋๋น ๊ด๊ณ์์ ์๊ฐํ ์ ์๋๋๋ก ๋น์ทํ๊ฒ ์ง.. ํ๊ณ ์ ์ฉํด๋จ๋ค. ์๋ง ๋ค์ ๊ณต๋ถํด์ ์ฌ์ ์ฉ์ ํด์ผํ ๊ฒ ๊ฐ๋ค.
์ด์ ์๋น์ค ์ญํ ์ธ PostService.java ๋ฅผ ์์ฑํ๊ณ โ ๋ชจ์ง๊ธ ์์ธ ์กฐํ โก ๋ชจ์ง๊ธ ์ ์ฒด ์กฐํ๋ฅผ ๊ตฌํํด ๋ณธ๋ค.
@GetMapping("/api/post/{id}")
public ResponseEntity<PostResponse> findPostById(@PathVariable Long id) {
Optional<Post> post = postService.findById(id);
return ResponseEntity.ok()
.body(new PostResponse(post,post.getTag(),post.getMatch(),post.getUser()));
}
@GetMapping("/api/post/{id}")
public ResponseEntity<PostResponse> findPostById(@PathVariable Long id) {
Optional<Post> byId = postService.findById(id);
Post post = byId.orElse(null);
return ResponseEntity.ok()
.body(new PostResponse(post,post.getTag(),post.getMatch(),post.getUser()));
}
๋ชจ๋ ์ ๋๋ก ์๋ํจ์ ํ์ธํ๋ค!
๊ทธ๋ฐ๋ฐ ์ผ๋จ mockdata๋ก ์์ฑ,์์ ๋ ์ง๋ฅผ ์์๋ก ์ฃผ์ ํ์ผ๋ฏ๋ก ์ถํ์ ์๋์์ฑ์ ํ์ธํด์ผํ๊ณ ,
์ง๊ธ ์ ์ผ ๊ถ๊ธํ๊ฑด post dto ์์ฑ ์์ ๋ฑ๋กํ user์ ์ ๋ณด๋ id๋ง ๋ฃ์ด๋์ผ๋ฉด ๋๋๊ฑด์ง, ์๋๋ฉด user ๊ฐ์ฒด ์์ฒด๋ฅผ ๋ฃ์ด์ผํ๋๊ฑด์ง..
๊ทธ๋ฆฌ๊ณ ์ ์๋๋ก id๋ง ๋ฃ์ด๋์ ๊ฒฝ์ฐ ๋์ค์ ํด๋น post์ ์ฐ๊ด์ด ์๋ ๊ทธ์ธ์ user ์ ๋ณด๋ ์ด๋ป๊ฒ ๋ถ๋ฌ์ค๋์ง์ด๋ค.