eclipse
Thread
채팅 프로그램 socket
entity 테이블을 생성
repositoory 데이터를 넣어준다
JPA <-> sql stmt
Entity 모델 클래스
반드시 사용해야 하는 어노테이션 2개 @Entity @Data @id
클래스의 내용이 테이블로 적용될때의 규칙
1모든 대문자가 소문자로 변경
2camel case는 snake case로 personAge -> person_age
3. 연관관계 (N:1, 1:N, 1:1, N:N)를 사용하면 외래키가
상대방 테이블_기본키 컬럼명으로 생성
ex) team 테이블의 team_id 컬럼 → team_team_id
컬럼을 원하는 이름으로 지정하려면 @Column 사용
ex) @Column(name="xxx")
외래키 컬럼을 원하는 이름으로 지정하려면 @JoinColumn 사용
ex) @JoinColumn(name="xxx")
// @ToString(exclude = {"players"})
@Entity
@Data
@ToString(exclude = {"players"})
public class Team {
@Id
int teamId;
String teamName;
@JsonIgnore
@OneToMany(mappedBy = "team")
List<Player> players = new ArrayList<>();
@ToString(exclude = {"players"}) 콘솔에 출력을 하려고 할때 연결고리를 끊어줘야 한다
test/java/com/example/domo/BasicApplicationTests.java.
@Test
void contextLoads2() {
List<Dept> list = deptRepository.findAll();
System.out.println(list);
컨트롤러를 일일히 만들려면 번거로우니 간단히 테스트 할 수 있다.
이때 프린트를 하려고 할때 양방향이라서 오류가 나기 때문에 Tostring exclude를 해줘야한다.
@OneToMany
오류 laintial...
1. 빠른 조회 eager 미리 조회\ fetch
2.1번작업 후 접속해제하지않음 @Transactional
lazy <=> Eager
@OneToMany(mappedBy = "dept", fetch = FetchType.EAGER)
조회할때 원투매니 일경우 fetchType이 lazy라서 다 받아오지 않아서 오류가 난다.
@Test @Transactional
void contextLoads2() {
List<Dept> list = deptRepository.findAll();
System.out.println(list);
2. 작업이 끝날때까지 한다.
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@GetMapping("/pagination")
@ResponseBody
public List<Emp> pagination(){
Pageable page = PageRequest.of(1,5);
Page<Emp> list = empRepository.findAll(page);
return list.getContent();
}
data.domain
personRepository.findByAgeGreaterThanEqualAndHeightGreaterThanEqual(20, 170);
@GetMapping("/foodpagination")
@ResponseBody
public List<Food> foodpagination(){
Sort sort = Sort.by(Sort.Direction.DESC,"id");
Pageable page = PageRequest.of(1, 10 , sort );
Page<Food> list = foodRepository.findAll(page);
System.out.println(list.getTotalPages());
return list.getContent();
}
페이지 정렬하기 , 오름차순, 내림차순 sort.direction.desc.
boardRepository.deleteById(no); [출처] 5. JPA 데이터 삭제(delete)하기 (자바 교실) | 작성자 박성환
자바 교실 : 네이버 카페
java, 자바, android, spring, mybatis, 개발자, jsp,html, javascript, jquery,..
cafe.naver.com
View Template으로 Jsp 또는 Thymeleaf
타임리프
1.출력 [[${변수명}]]
2.반복 th:each="변수 : ${ 변수명 }"
3.출력 th:if = ' 조건식 '
@GetMapping("user")
public String user(Model model) {
Map<String, Object> user = null;
user = new HashMap<>();
user.put("userId", "z");
user.put("userName", "zoo");
user.put("userAge", 25);
model.addAttribute("user", user);
return "user";
}
<body>
아이디:<span>[[${user.userId}]]</span><br>
이름:<span>[[${user.userName}]]</span><br>
나이:<span>[[${user.userAge}]]</span><br>
<hr>
아이디:<span th:text="${user.userId}"></span><br>
이름:<span th:text="${user.userName}"></span><br>
나이:<span th:text="${user.userAge}"></span><br>
<hr>
아이디:<span data-th-text="${user.userId}"></span><br>
이름:<span data-th-text="${user.userName}"></span><br>
나이:<span data-th-text="${user.userAge}"></span><br>
</body>
자바가 가지고 있는 데이터를 html로 전달했다!
@GetMapping("empList2")
public String empList2(Model model) {
List<Emp> empList = empRepository.findAll();
model.addAttribute("empList", empList);
return "empList2";
}
<tr th:each="emp: ${empList}">
<td th:text="${emp.empno}">
</td>
<td th:text="${emp.ename}">
</td>
<td th:text="${emp.job}">
</td>
<td th:text="${emp.mgr}">
</td>
<td th:text="${emp.hiredate}">
</td>
<td th:text="${emp.sal}">
</td>
<td th:text="${emp.comm}">
</td>
<td th:text="${emp.dept.deptno}">
</td>
<td th:text="${emp.dept.dname}">
</td>
<td th:text="${emp.dept.loc}">
</td>
</tr>