20240314 전자정부(JPA 정렬)

2024. 3. 14. 11:502023.11.21-2024.05.31

프로젝트때 사용하는 전자정부

 

 

 

정렬

jpa가 스펠링 인식해서 자동으로 정렬

 

내림차순

BoardRepository >쿼리문

public interface BoardRepository extends JpaRepository<JPABoard, Integer> {
	   @Query(value = "SELECT * FROM jpaboard jWHERE j.jbno=?1", nativeQuery = true)

 

 

 

 

JPABoard >테이블 정리

 

package com.mask.web.entity;

import java.time.LocalDateTime;


import org.hibernate.annotations.ColumnDefault;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class JPABoard {
	//데이터베이스 생성도 하는 곳
	//조심할 것 _(언더바)를 못쓴다.  board_no를 못쓴다. 

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int jbno;
	
//	@Column(columnDefinition = "TEXT")
	@Column(name="jbtitle", columnDefinition = "VARCHAR(50)")
	private String jbtitle;
	
	@Column(columnDefinition = "LONGTEXT")
	private String jbcontent;
	
	@ColumnDefault("CURRENT_TIMESTAMP")//컬럼 기본값
	private LocalDateTime jbdate = LocalDateTime.now();
	
	@ColumnDefault("0")//좋아요
	private int jblike=1;

	@ColumnDefault("1")//조회수
	private int jbread=1;
	
	
}

 

 

 

글쓴이 추가

 

entity 패키지 > JPAMember 클래스 생성

 

package com.mask.web.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity

public class JPAMember {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int jmno;
	
	@Column(unique=true, columnDefinition="VARCHAR(10)", nullable= false)
	private String jmid;
	
	@Column(columnDefinition="VARCHAR(10)", nullable= false)
	private String jmpw;
	
	@Column(name="jmname", columnDefinition="VARCHAR(10)", nullable= false)
	private String jmname;
	//칼럼 네임이 컬럼용 //String네임이 JPA용
}

 

 

 

컬럼 추가 > 한번 만들어진 컬럼이 수정이 되진 않고 추가가 된다.

	@Column(columnDefinition = "VARCHAR(15)")
	private String tel;

 

 

	@Column(columnDefinition = "VARCHAR(15)")
	private String jmtel;

 

 

 

 

 

이런 많은 과정을 거쳐 최종

 

package com.mask.web.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity

public class JPAMember {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int jmno;
	
	@Column(unique=true, columnDefinition="VARCHAR(10)", nullable= false)
	private String jmid;
	
	@Column(columnDefinition="VARCHAR(10)", nullable= false)
	private String jmpw;
	
	@Column(name="jmname", columnDefinition="VARCHAR(10)", nullable= false)
	private String jmname;
	//칼럼 네임이 컬럼용 //String네임이 JPA용
	
	@Column(columnDefinition = "VARCHAR(15)")
	private String jmtel;
}

 

 

JPABoard > 추가

	@ManyToOne
	@JoinColumn
	private JPAMember jpaMember;

 

 

재기동 >데이터베이스에서 확인

 

jpa_member_imno 추가

 

 

글쓰기

 

	@PostMapping("write")
	public String wriet(@RequestParam("title") String title, @RequestParam("content") String content) {
		JPABoard post = new JPABoard();
		post.setJbtitle(title);
		post.setJbcontent(content);
		JPAMember jpaMember = new JPAMember();
		//우리가 알고 있는 것은 세션에 올라간 mid...
		jpaMember = boardService.findById("mask");
		post.setJpaMember(jpaMember);
		
		boardService.write(post);
		
		return "redirect:/board";
	}

 

 

 

	public JPAMember findById(String string) {
		return boardRepository.findByJmid(string);
	}

 

 

 

 

MemberRepository(인터페이스) 생성해주고, BoardService 에서 받아오기

package com.mask.web.repository;

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

import com.mask.web.entity.JPAMember;

public interface MemberRepository extends JpaRepository<JPAMember,Integer>{

	JPAMember findByJmid(String string);
	


}

 

 

BoardService

 

 

MemberRepository 어노테이션으로 연결

	@Autowired
	private MemberRepository memberRepository;

 

 

 

MemberRepository으로 보내기

	public JPAMember findById(String string) {
		return memberRepository.fineByJmid(string);
	}

 

 

 

board.html 에 글쓴이 추가

 

 

<table border="1">
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>글쓴이</th>
			<th>날짜</th>
			<th>조회수</th>
			<th>좋아요</th>
		</tr>
		<tr th:each="row : ${list}">
		<td th:text="${row.jbno}"></td>
		<td th:onclick="|location.href='@{detail(no=${row.jbno })}'|" >[[${row.jbtitle}]]</td>
		<td th:text="${row.JPAMember.jmname }"></td>
		<td th:text="${row.jbdate}"></td>
		<td th:text="${row.jbread}"></td>
		<td th:text="${row.jblike}"></td>
		</tr>	
	</table>