오늘의 코딩순서
(폴더: oBootMybatis01)
5. JPA Member(CRUD)
=> model 폴더가 아닌 domain폴더/ dao 폴더가 아닌 repository 폴더에 만듬
=> 같은 테이블을 지향하더라도, 어노테이션은 따로 만들기
1. 새 멤버 입력
- application.yml
- Member.class + MemberJpaRepository.interface + MemberJpaRepositoryImpl.class + MemberJpaService.class + MemberJpaController + createMemberForm.jsp
2. 멤버 조회
- memberList.jsp
+ MemberJpaController + MemberJpaService.class + MemberJpaRepository.interface + MemberJpaRepositoryImpl.class
3. 멤버 정보 Null 확인
- interCeptor.jsp
+ MemberJpaController + MemberJpaService.class + MemberJpaRepository.interface
+ MemberJpaRepositoryImpl.class
4. 멤버 정보 수정
- memberModify.jsp
+ MemberJpaController + MemberJpaService.class + MemberJpaRepository.interface
+ MemberJpaRepositoryImpl.class
6. interCeptor(가로채기)
1. 시작화면 + 2. interCeptor Number2
- interCeptorForm.jsp + Member1.class + SampleInterceptor.class + WebMvcConfiguration.class + configuration.xml
+ EmpController.class + EmpService.interface + EmpServiceImpl.class
+ Member1Dao.Interface + Member1DaoImpl.class + member1.xml
3. SampleInterceptor 내용을 받아 처리 + 4. interCeptor 진행 Test
- doMemberWrite.jsp + doMemberList.jsp
+ EmpController.class + EmpService.interface + EmpServiceImpl.class
+ Member1Dao.Interface + Member1DaoImpl.class + member1.xml
오늘의 코딩 포인트
5. JPA Member(CRUD)
=> model 폴더가 아닌, domain폴더에 만듬
=> 같은 테이블을 지향하더라도, 어노테이션은 따로 만들기
1. 새 멤버 입력
- application.yml
server:
port: 8387
# Oracle Connect
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@localhost:1521/xe
username: scott
password: tiger
#JPA Setting
jpa:
show-sql: true
hibernate:
ddl-auto: create
# View Resolver
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
# gmail Transfer
mail:
host: smtp.gmail.com
port: 587
username: rollout147@gmail.com
password: bbwt lmdi vhew burk
properties:
mail:
smtp:
auth: true
starttls.enable: true
# Mybatis
mybatis:
config-location: classpath:configuration.xml
mapper-locations: classpath:mappers/*.xml
- Member.class
package com.oracle.oBootMybatis01.domain;
import java.util.Date;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Entity
@Table(name = "member3")
@Getter
@Setter
@ToString
public class Member {
@Id
private Long id;
private String name;
private String password;
@Column(nullable = false, columnDefinition = "date default sysdate")
private Date reg_date = new Date();
}
- MemberJpaController.class
package com.oracle.oBootMybatis01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.service.MemberJpaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class MemberJpaController {
private final MemberJpaService memberJpaService;
@GetMapping(value = "/memberJpa/new")
public String createForm() {
System.out.println("MemberController /members/new start..");
return "memberJpa/createMemberForm";
}
@PostMapping(value = "/memberJpa/save")
public String create(Member member) {
System.out.println("MemberController memberJpa/save start...");
System.out.println("MemberController memberJpa/save member->"+member);
memberJpaService.join(member);
return "memberJpa/createMemberForm";
}
}
- MemberJpaService.class
package com.oracle.oBootMybatis01.service;
import org.springframework.stereotype.Service;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.repository.MemberJpaRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
@Transactional
public class MemberJpaService {
private final MemberJpaRepository memberJpaRepository;
public void join(Member member) {
System.out.println("MemberJpaService Join Start...");
memberJpaRepository.save(member);
}
}
- MemberJpaRepository.interface
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import com.oracle.oBootMybatis01.domain.Member;
public interface MemberJpaRepository {
Member save(Member member);
List<Member> findAll();
Optional<Member> fingById(Long memberId);
void updateByMember(Member member);
}
- MemberJpaRepositoryImpl.class
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Repository;
import com.oracle.oBootMybatis01.domain.Member;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class MemberJpaRepositoryImpl implements MemberJpaRepository {
private final EntityManager em;
@Override
public Member save(Member member) {
System.out.println("MemberJpaRepositoryImpl save Start...");
em.persist(member);
return member;
}
}
- createMemberForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Mybatis Jpa 회원 등록</h1>
<div class="container">
<form action="${pageContext.request.contextPath}/memberJpa/save" method="post">
ID : <input type="text" id="id" name="id" required="required"><p>
이름 : <input type="text" id="name" name="name" placeholder="이름을 입력하세요"><p>
PW : <input type="text" id="pw" name="password" placeholder="비밀번호를 입력하세요">
<button type="submit">등록</button>
</form>
<a href="/members">JPA Member List 조회</a><p>
</div> <!-- /container -->
</body>
</html>
2. 멤버 조회
- memberList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>JPA 직원(member)목록</h2>
<a href="memberJpa/new">입력</a>
<c:set var="num" value="1"></c:set>
<c:if test="${message != null }">
${message}
</c:if>
<table>
<tr>
<th>번호</th> <th>사번</th> <th>이름</th> <th>비번</th>
</tr>
<c:forEach var="member" items="${members }">
<tr>
<td>${num }</td>
<td>${member.id }</td>
<td><a href="memberJpa/memberUpdateForm?id=${member.id }">${member.name }</a></td>
<td>${member.password }</td>
<c:set var="num" value="${num + 1 }"></c:set>
</tr>
</c:forEach>
</table>
</body>
</html>
- MemberJpaController
package com.oracle.oBootMybatis01.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.service.MemberJpaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class MemberJpaController {
private final MemberJpaService memberJpaService;
//2. 멤버 조회
@GetMapping(value = "/members")
public String listMember(Model model) {
System.out.println("MemberController listMember Start...");
List<Member> memberList = memberJpaService.getListAllMember();
model.addAttribute("members", memberList);
return "memberJpa/memberList";
}
}
- MemberJpaService.class
package com.oracle.oBootMybatis01.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.repository.MemberJpaRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
@Transactional
public class MemberJpaService {
private final MemberJpaRepository memberJpaRepository;
//2. 멤버 조회
public List<Member> getListAllMember() {
System.out.println("MemberJpaService getListAllMember Start...");
List<Member> listMember = memberJpaRepository.findAll();
System.out.println("MemberJpaService getListAllMember listMember.size()->"+listMember.size());
return listMember;
}
}
- MemberJpaRepository.interface
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import com.oracle.oBootMybatis01.domain.Member;
public interface MemberJpaRepository {
Member save(Member member);
List<Member> findAll();
Optional<Member> fingById(Long memberId);
void updateByMember(Member member);
}
- MemberJpaRepositoryImpl.class
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Repository;
import com.oracle.oBootMybatis01.domain.Member;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class MemberJpaRepositoryImpl implements MemberJpaRepository {
private final EntityManager em;
//2. 멤버 조회
@Override
public List<Member> findAll() {
System.out.println("MemberJpaRepositoryImpl findAll Start...");
List<Member> memberList = em.createQuery("select m from Member m", Member.class)
.getResultList();
return memberList;
}
3. 멤버 정보 NULL 확인
- MemberJpaController
package com.oracle.oBootMybatis01.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.service.MemberJpaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class MemberJpaController {
private final MemberJpaService memberJpaService;
// 3. 멤버 정보 Null확인
@GetMapping(value = "/memberJpa/memberUpdateForm")
public String memberUpdateForm(Member member1, Model model) {
Member member = null;
String rtnJsp = "";
System.out.println("MemberController memberUpdateForm id->"+member1.getId());
Optional<Member> maybeMember = memberJpaService.findById(member1.getId());
if (maybeMember.isPresent()) {
System.out.println("MemberController memberUpdateForm maybeMember Is Not Null");
member = maybeMember.get();
model.addAttribute("member",member);
// model.addAttribute("message", "member가 존재합니다. 수정 후 입력해주세요");
rtnJsp = "memberJpa/memberModify";
} else {
System.out.println("MemberController memberUpdateForm maybeMember Is Null");
// 검증용
model.addAttribute("message", "member가 입력되지 않았습니다. 입력해주세요.");
rtnJsp = "forward:/members";
}
return rtnJsp;
}
- MemberJpaService.class
package com.oracle.oBootMybatis01.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.repository.MemberJpaRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
@Transactional
public class MemberJpaService {
private final MemberJpaRepository memberJpaRepository;
// 3. 멤버 정보 Null확인
public Optional<Member> findById(Long memberId) {
System.out.println("MemberJpaService findById Start...");
Optional<Member> member = memberJpaRepository.fingById(memberId);
return member;
}
- MemberJpaRepository.interface
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import com.oracle.oBootMybatis01.domain.Member;
public interface MemberJpaRepository {
Member save(Member member);
List<Member> findAll();
Optional<Member> fingById(Long memberId);
void updateByMember(Member member);
}
- MemberJpaRepositoryImpl.class
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Repository;
import com.oracle.oBootMybatis01.domain.Member;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class MemberJpaRepositoryImpl implements MemberJpaRepository {
private final EntityManager em;
// 3. 멤버 정보 Null확인
@Override
public Optional<Member> fingById(Long memberId) {
System.out.println("MemberJpaRepositoryImpl fingById Start...");
Member member = em.find(Member.class, memberId);
return Optional.ofNullable(member);
}
}
4. 멤버 정보 수정
- memberModify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>회원정보(JPA) 수정</h2>
<form action="<%=request.getContextPath()%>/memberJpa/memberUpdate" method="get">
<input type="hidden" name="id" value="${member.id }">
<table>
<tr><th>회원번호</th><td>${member.id }</td></tr>
<tr>
<th>이름</th>
<td><input type="text" name="name" required="required" value="${member.name }"></td>
</tr>
<%-- <tr>
<th>비밀번호</th>
<td><input type="text" name="password" value="${member.password }"></td>
</tr> --%>
<tr>
<td colspan="2"> <input type="submit" value="확인"></td>
</tr>
</table>
</form>
</body>
</html>
- MemberJpaController
package com.oracle.oBootMybatis01.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.service.MemberJpaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class MemberJpaController {
private final MemberJpaService memberJpaService;
// 4. 멤버 정보 수정
@GetMapping(value = "/memberJpa/memberUpdate")
public String memberUpdate(Member member, Model model) {
System.out.println("MemberController member->"+member);
memberJpaService.memberUpdate(member);
System.out.println("MemberController memberUpdate after...");
return "redirect:/members";
}
}
- MemberJpaService.class
Tip)- 모든 쿼리는 Transaction 단위로 돌아가기 때문에 무결성을 보장할 수 있음
package com.oracle.oBootMybatis01.service;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.oracle.oBootMybatis01.domain.Member;
import com.oracle.oBootMybatis01.repository.MemberJpaRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
@Transactional
public class MemberJpaService {
private final MemberJpaRepository memberJpaRepository;
//4. 멤버 정보 수정
public void memberUpdate(Member member) {
System.out.println("MemberJpaService memberUpdate member->"+member);
memberJpaRepository.updateByMember(member);
System.out.println("MemberJpaService memberUpdate after...");
return;
}
}
- MemberJpaRepository.interface
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import com.oracle.oBootMybatis01.domain.Member;
public interface MemberJpaRepository {
Member save(Member member);
List<Member> findAll();
Optional<Member> fingById(Long memberId);
void updateByMember(Member member);
}
- MemberJpaRepositoryImpl.class
package com.oracle.oBootMybatis01.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Repository;
import com.oracle.oBootMybatis01.domain.Member;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class MemberJpaRepositoryImpl implements MemberJpaRepository {
private final EntityManager em;
// 4. 멤버 정보 수정
@Override
public void updateByMember(Member member) {
System.out.println("MemberJpaRepositoryImpl updateByMember Start...");
Member member3 = em.find(Member.class, member.getId());
// Member member3 = new Member();
member3.setId(member.getId());
member3.setName(member.getName());
System.out.println("MemberJpaRepositoryImpl updateByMember after...");
}
}
6. interCeptor(가로채기) => 맞는 정보의 회원만 조회가능하도록 하는 기능의 로직
1. 시작화면 + 2. interCeptor Number2
- interCeptorForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>직원정보 조회</h2>
<c:if test="${msg!=null}">${msg}</c:if>
<form action="interCeptor" name="frm">
<table>
<tr>
<th>사번</th>
<td>><input type="text" name="id" required="required"></td>
</tr>
<tr>
<td><input type="submit" value="확인"></td>
</tr>
</table>
</form>
</body>
</html>
- Member1.class
package com.oracle.oBootMybatis01.model;
import java.util.Date;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Member1 {
private String id;
private String name;
private String password;
private Date reg_date;
}
- SampleInterceptor.class
Tip) postHandle, preHandle를 자동완성해서 @override 필드 만들기
package com.oracle.oBootMybatis01.service;
import java.lang.reflect.Method;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class SampleInterceptor implements HandlerInterceptor {
public SampleInterceptor() {
}
// 3번
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
System.out.println("post handle............................");
String ID = (String) modelAndView.getModel().get("id");
int memCnt = (Integer) modelAndView.getModel().get("memCnt");
System.out.println("SampleInterceptor post handle memCnt: "+ memCnt);
if (memCnt < 1) {
System.out.println("memCnt Not exists");
request.getSession().setAttribute("ID", ID);
// User가 존재하지 않으면 User inteCeptor Page(회원등록)로 이동
response.sendRedirect("doMemberWrite");
} else { // 정상 Login User
System.out.println("memCnt exists");
request.getSession().setAttribute("ID", ID);
// User가 존재하면 User InterCeptor Page(회원 List)로 이동
response.sendRedirect("doMemberList");
}
}
// 1번
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler)
throws Exception {
System.out.println("pre handle.....................");
HandlerMethod method = (HandlerMethod) handler;
Method methodObj = method.getMethod();
System.out.println("Bean: " + method.getBean());
System.out.println("Method: " + methodObj);
return true;
}
}
- WebMvcConfiguration.class
Tip) addInterceptors 자동완성해서 @override 필드 만들기
package com.oracle.oBootMybatis01.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.oracle.oBootMybatis01.service.SampleInterceptor;
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 누군가가 URL을 InterCeptor하면 SampleInterceptor()처리 해줌
registry.addInterceptor(new SampleInterceptor()).addPathPatterns("/interCeptor");
}
}
- configuration.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Emp" type="com.oracle.oBootMybatis01.model.Emp"/>
<typeAlias alias="Dept" type="com.oracle.oBootMybatis01.model.Dept"/>
<typeAlias alias="EmpDept" type="com.oracle.oBootMybatis01.model.EmpDept"/>
<typeAlias alias="DeptVO" type="com.oracle.oBootMybatis01.model.DeptVO"/>
<typeAlias alias="Member1" type="com.oracle.oBootMybatis01.model.Member1"/>
</typeAliases>
</configuration>
- EmpController.class
package com.oracle.oBootMybatis01.controller;
import java.util.HashMap;
import java.util.List;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.oracle.oBootMybatis01.model.Dept;
import com.oracle.oBootMybatis01.model.DeptVO;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.model.EmpDept;
import com.oracle.oBootMybatis01.model.Member1;
import com.oracle.oBootMybatis01.service.EmpService;
import com.oracle.oBootMybatis01.service.Paging;
import jakarta.mail.internet.MimeMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class EmpController {
private final EmpService es;
private final JavaMailSender mailSender;
// ----------------------------------
// 1. ` 시작 화면
@RequestMapping(value = "interCeptorForm")
public String interCeptorForm() {
System.out.println("interCeptorForm Start...");
return "interCeptorForm";
}
// 2. interCeptor Number2
@RequestMapping(value = "interCeptor")
public String interCeptor(Member1 member1, Model model) {
System.out.println("EmpController interCeptor Test Start...");
System.out.println("EmpController interCeptor id->"+member1.getId());
// 존재하면 1, 존재하지 않으면 0
int memCnt = es.memCount(member1.getId());
System.out.println("EmpController interCeptor memCnt->"+memCnt);
model.addAttribute("id", member1.getId());
model.addAttribute("memCnt", memCnt);
System.out.println("interCeptor Test End");
return "interCeptor";
// User 존재하면 User 이용 조회 Page
}
}
- EmpService.interface
package com.oracle.oBootMybatis01.service;
import java.util.HashMap;
import java.util.List;
import com.oracle.oBootMybatis01.model.Dept;
import com.oracle.oBootMybatis01.model.DeptVO;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.model.EmpDept;
public interface EmpService {
int totalEmp();
List<Emp> listEmp(Emp emp);
// (0809)현장HW 2-2
Emp detailEmp(int empno);
// (0809)현장HW 3-2
int updateEmp(Emp emp);
// (0812)현장 1-2
List<Emp> listManager();
List<Dept> deptSelect();
// (0812)현장 3-1
int insertEmp(Emp emp);
// (0812)현장 4-1
int deleteEmp(int empno);
// Validate 방법
int condEmpTotal(Emp emp);
// 현장 HW
List<Emp> listSearchEmp(Emp emp);
// (0813) 현장 HW 1-2. Emp TBL과 Dept TBL을 Join
List<EmpDept> listEmpDept();
// PL/SQL 연동
void insertDept(DeptVO deptVO);
void selListDept(HashMap<String, Object> map);
// 2. interCeptor Number2
int memCount(String id);
}
- EmpServiceImpl.class
package com.oracle.oBootMybatis01.service;
import java.util.HashMap;
import java.util.List;
import org.springframework.stereotype.Service;
import com.oracle.oBootMybatis01.dao.DeptDao;
import com.oracle.oBootMybatis01.dao.EmpDao;
import com.oracle.oBootMybatis01.dao.Member1Dao;
import com.oracle.oBootMybatis01.model.Dept;
import com.oracle.oBootMybatis01.model.DeptVO;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.model.EmpDept;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class EmpServiceImpl implements EmpService {
private final EmpDao ed;
private final DeptDao dd;
private final Member1Dao md;
// 서로 다른 Interface(DAO)를 받은 것이기 때문에 두 개 이상 가능
// -------------------------------------
// 2. interCeptor Number2
@Override
public int memCount(String id) {
System.out.println("EmpServiceImpl memCount id->"+id);
return md.memCount(id);
}
}
- Member1Dao.Interface
package com.oracle.oBootMybatis01.dao;
public interface Member1Dao {
int memCount(String id); // Member1의 Count
}
- Member1DaoImpl.class
package com.oracle.oBootMybatis01.dao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class Member1DaoImpl implements Member1Dao {
//Mybatis 연동
private final SqlSession session;
@Override
public int memCount(String id) {
System.out.println("Member1DaoImpl memCount Start...");
// Mapper ==> Member1.xml
// result = session.selectOne("memCount", id);
int result = 0;
System.out.println("Member1DaoImpl id->"+id);
try {
result = session.selectOne("memCount", id);
} catch (Exception e) {
System.out.println("Member1DaoImpl memCount Exception->"+e.getMessage());
}
return result;
}
}
- Member1.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oracle.oBootMybatis01.Member1Mapper">
<select id="memCount" parameterType="java.lang.String" resultType="int">
SELECT COUNT(*)
FROM member1
WHERE id=#{id}
</select>
</mapper>
3. SampleInterceptor 내용을 받아 처리 + 4. interCeptor 진행 Test
- doMemberWrite.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>doMemberWrite</title>
</head>
<body>
<h2>직원정보 입력</h2>
<form action="" method="post" name="frm">
<table>
<tr><th>사번</th><td><input type="text" name="id"
required="required" maxlength="4" value="${id}">
<input type="button" value="중복확인"
onclick="chk()"> </td></tr>
<tr><th>비밀번호</th><td><input type="text" name="password"
required="required"> </td></tr>
<tr><th>이름</th><td><input type="text" name="name"
required="required"></td></tr>
<tr><td colspan="2"><input type="submit" value="확인"></td></tr>
</table>
</form>
</body>
</html>
- doMemberList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>회원정보</h2>
조회자 ID : ${ID}
<table>
<tr>
<th>ID</th> <th>이름</th> <th>비밀번호</th> <th>등록일</th>
</tr>
<c:forEach var="listMem" items="${listMem}">
<tr>
<td>${listMem.id }</td>
<td>${listMem.name }</td>
<td>${listMem.password }</td>
<td>${listMem.reg_date }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
- EmpController.class
package com.oracle.oBootMybatis01.controller;
import java.util.HashMap;
import java.util.List;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.oracle.oBootMybatis01.model.Dept;
import com.oracle.oBootMybatis01.model.DeptVO;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.model.EmpDept;
import com.oracle.oBootMybatis01.model.Member1;
import com.oracle.oBootMybatis01.service.EmpService;
import com.oracle.oBootMybatis01.service.Paging;
import jakarta.mail.internet.MimeMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequiredArgsConstructor
@Slf4j
public class EmpController {
private final EmpService es;
private final JavaMailSender mailSender;
// 3. SampleInterceptor 내용을 받아 처리
@RequestMapping(value = "doMemberWrite")
public String doMemberWrite(Model model, HttpServletRequest request) {
String ID = (String) request.getSession().getAttribute("ID");
System.out.println("doMemberWrite부터 입력하세요.");
model.addAttribute("id",ID);
return "doMemberWrite";
}
// (0814) HW 2-1
// Member1 List Get Service
// Service, DAO --> listMem
// Mapper --> listMember1
// Member1 모든 Row Get
// 4. interCeptor 진행 Test
@RequestMapping(value = "doMemberList")
public String doMemberList(Model model, HttpServletRequest request) {
String ID = (String) request.getSession().getAttribute("ID");
System.out.println("doMemberList Test Start ID->"+ID);
Member1 member1 = null;
// Member1 List Get Service
List<Member1> listMem = es.listMem(member1);
model.addAttribute("ID",ID);
model.addAttribute("listMem",listMem);
return "doMemberList"; // User 존재하면 User 이용 조회 Page
}
}
- EmpService.interface
package com.oracle.oBootMybatis01.service;
import java.util.HashMap;
import java.util.List;
import com.oracle.oBootMybatis01.model.Dept;
import com.oracle.oBootMybatis01.model.DeptVO;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.model.EmpDept;
import com.oracle.oBootMybatis01.model.Member1;
public interface EmpService {
int totalEmp();
List<Emp> listEmp(Emp emp);
// (0809)현장HW 2-2
Emp detailEmp(int empno);
// (0809)현장HW 3-2
int updateEmp(Emp emp);
// (0812)현장 1-2
List<Emp> listManager();
List<Dept> deptSelect();
// (0812)현장 3-1
int insertEmp(Emp emp);
// (0812)현장 4-1
int deleteEmp(int empno);
// Validate 방법
int condEmpTotal(Emp emp);
// 현장 HW
List<Emp> listSearchEmp(Emp emp);
// (0813) 현장 HW 1-2. Emp TBL과 Dept TBL을 Join
List<EmpDept> listEmpDept();
// PL/SQL 연동
void insertDept(DeptVO deptVO);
void selListDept(HashMap<String, Object> map);
// 2. interCeptor Number2
int memCount(String id);
// (0814) HW 4. interCeptor 진행 Test
List<Member1> listMem(Member1 member1);
}
- EmpServiceImpl.class
package com.oracle.oBootMybatis01.service;
import java.util.HashMap;
import java.util.List;
import org.springframework.stereotype.Service;
import com.oracle.oBootMybatis01.dao.DeptDao;
import com.oracle.oBootMybatis01.dao.EmpDao;
import com.oracle.oBootMybatis01.dao.Member1Dao;
import com.oracle.oBootMybatis01.model.Dept;
import com.oracle.oBootMybatis01.model.DeptVO;
import com.oracle.oBootMybatis01.model.Emp;
import com.oracle.oBootMybatis01.model.EmpDept;
import com.oracle.oBootMybatis01.model.Member1;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class EmpServiceImpl implements EmpService {
private final EmpDao ed;
private final DeptDao dd;
private final Member1Dao md;
// 서로 다른 Interface(DAO)를 받은 것이기 때문에 두 개 이상 가능
// (0814) HW
// 4. interCeptor 진행 Test
@Override
public List<Member1> listMem(Member1 member1) {
List<Member1> listMember1 = null;
System.out.println("EmpServiceImpl listMem Start...");
listMember1 = md.listMem(member1);
System.out.println("EmpServiceImpl listMem listMember1->"+listMember1);
return listMember1;
}
}
- Member1Dao.Interface
package com.oracle.oBootMybatis01.dao;
import java.util.List;
import com.oracle.oBootMybatis01.model.Member1;
public interface Member1Dao {
int memCount(String id); // Member1의 Count
// (0814) HW
// 4. interCeptor 진행 Test
List<Member1> listMem(Member1 member1);
}
- Member1DaoImpl.class
package com.oracle.oBootMybatis01.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.oracle.oBootMybatis01.model.Member1;
import lombok.RequiredArgsConstructor;
@Repository
@RequiredArgsConstructor
public class Member1DaoImpl implements Member1Dao {
//Mybatis 연동
private final SqlSession session;
// (0814) HW
// 4. interCeptor 진행 Test
@Override
public List<Member1> listMem(Member1 member1) {
List<Member1> listMember1 = null;
System.out.println("Member1DaoImpl listMem Start...");
try {
listMember1 = session.selectList("listMember1", member1);
System.out.println("Member1DaoImpl listMem listMember1->"+listMember1);
} catch (Exception e) {
System.out.println("Member1DaoImpl listMember1 Exception->"+e.getMessage());
}
return listMember1;
}
}
- member1.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oracle.oBootMybatis01.Member1Mapper">
<select id="memCount" parameterType="java.lang.String" resultType="int">
SELECT COUNT(*)
FROM member1
WHERE id=#{id}
</select>
<select id="listMember1" parameterType="Member1" resultType="Member1">
SELECT *
FROM member1
</select>
</mapper>
질문목록
수업교재
오늘의 숙제
'Spring' 카테고리의 다른 글
2024_08_20_화~08_21_수 (0) | 2024.08.20 |
---|---|
2024_08_19_월~08_20_화 (0) | 2024.08.19 |
2024_08_13_화 (0) | 2024.08.13 |
2024_08_12_월 (0) | 2024.08.12 |
2024_08_08_목~08_12_월 (0) | 2024.08.08 |