오라클

DISTINCT ( 열 중복 제거 ) 

 예 ) SELECT DISTINCT 

 

ORDER BY ( 정렬 ) 기본값 오름차순(ASC), 내림차순은 DESC

예 )

SELECT *

FROM EMP

ORDER BY SAL (ASC),(DESC) 선택

ORDER BY DEPTNO ASC(1순위), SAL DESC(2순위)

 

WHERE

예 )

SELECT *

FROM EMP

WHERE DEPTNO = 30;

 

AND ( 피연산자가 둘 다 true ) 제출 하고 

예 )

SELECT *

FROM EMP

WHERE DEPTNO = 30

 AND JOB = 'SALESMAN';

 

OR ( 피연산자가 둘 다 또는 둘 중 하나 true이면 true ) 제출 하거나 

SELECT *

FROM EMP

WHERE DEPTNO = 30

 OR JOB = 'SALESMAN';

 

곱셈 산술 연산자 ( 연봉 찾기 ) 

SELECT *

 FROM EMP

 WHERE SAL * 12 = 36000;

 

등가 비교 연산자 ( 급여가 3000이 아닌 )

SELECT *

 FROM EMP

 WHERE SAL != 3000;

 

NOT 연산자 ( NOT이 많이 사용 됨 )

SELECT *

 FROM EMP

 WHERE  NOT SAL = 3000;

 

IN 연산자 ( 출력하고 싶은 열의 조건이 여러 가지일 때 ) OR로 하나씩 해주는 것보다 간편

예)

SELECT *

 FROM EMP

WHERE JOB IN ('MANAGER', 'SALESMAN', 'CLERK')

WHERE JOB NOT IN ('MANAGER', 'SALESMAN', 'CLERK') NOT 가능 

 

BETWEEN 최소값 AND 최댓값 2000~3000

예)

SELECT *

 FROM EMP

WHERE SAL BETWEEN 2000 AND 3000;

WHERE SAL NOT BETWEEN 2000 AND 3000; NOT 가능 

 

LIKE 연산자 ( 일부 문자열 조회 ) ( _는 한 개의 문자 데이터를 의미 )

예 ) 1

SELECT *

 FROM EMP

WHERE ENAME LIKE 'S%' ( 대문자 S로 시작하는 ) 

 

예 ) 2

SELECT *

 FROM EMP

WHERE ENAME LIKE '_S%' ( 두번 째 문자 대문자 S로 시작하는 ) 

 

예 ) 3

SELECT *

 FROM EMP

WHERE ENAME LIKE '%AM%' ( AM이 포함되어 있는 ) 

WHERE ENAME NOT LIKE '%AM%' NOT 가능 ( AM이 포함되어 있지 않은 ) 

 

IS NULL ( 값이 NULL인 경우 출력 ) 

예) 1

SELECT *

 FROM EMP

WHERE COMM IS NULL;

 

예) 2

SELECT *

 FROM EMP

WHERE MGR IS NOT NULL; NOT 가능

( 직속상관이 있는 사원만 출력 ) 사장은 없기때문에 출력 안됨 )

 

 

집합 연산자 UNION ( 두 개 이상의 SELECT문의 결과 값을 연결할 때 사용 ) 결과 값 중복 제거

예)

select empno, ename, sal, deptno
 from emp
 where deptno = 10
 UNION
 select empno, ename, sal, deptno
 from emp
 where deptno = 20

 

집합 연산자 UNION ALL ( 중복된 결과값도 출력 ) 

예)

select empno, ename, sal, deptno
 from emp
 where deptno = 10
 UNION ALL
 select empno, ename, sal, deptno
 from emp
 where deptno = 20

 

 

차집합 연산자 MINUS ( 결과 값이 같은 데이터 제외 ) 

예)

select empno, ename, sal, deptno
 from emp
 MINUS
 select empno, ename, sal, deptno
 from emp
 where deptno = 10

 

교집합 연산자 INTERSECT ( 결과 값이 같은 데이터만 출력 )

예)

select empno, ename, sal, deptno
 from emp
INTERSECT
 select empno, ename, sal, deptno
 from emp
 where deptno = 10

 

문자 데이터를 가공하는 문자 함수

UPPER(문자열) : 괄호 안 문자 데이터를 모두 대문자로 변환

LOWER(문자열) : 괄호 안 문자 데이터를 모두 소문자로 변환

INITCAP(문자열) : 괄호 안 문자 데이터 중 첫 글자는 대문자, 나머지 문자를 소문자로 변환

예)

select ename, UPPER(ename), LOWER(ename), INITCAP(ename)
 from emp;

 

문자열 길이를 구하는 LENGTH 함수

예)

select ename, LENGTH(ename)
 from emp;

where LENGTH(ename) >= 5; ( 숫자 비교 가능 ) 5글자 이상

 

문자열 일부를 추출하는 SUBSTR 함수

SUBSTR(문자열 데이터, 시작위치, 추출길이 ) 추출길이 입력 안할 시 끝까지 출력

예)

select ename, SUBSTR(job, 1, 2), SUBSTR(job, 2, 3), SUBSTR(job, 1)
 from emp;

 

특정 문자를 다른 문자로 바꾸는 REPLACE 함수

REPLACE([문자열 데이터 또는 열 이름(필수)], [찾는 문자(필수)], [대체할 문자(선택)])

대체할 문자를 입력하지 않으면 찾는 문자 삭제

예)
select '010-1234-5678'
, REPLACE('010-1234-5678', '-', ' ') as RE1
, REPLACE('010-1234-5678', '-') as RE2
from dual;

 

특정 문자를 지우는 TRIM, LTRIM, RTRIM ( 공백제거 많이 사용 ) 

삭제 할 문자를 지정하지않으면 공백제거

예 ) 

SELECT 
TRIM('     oracleStudy     ') AS result 
, LTRIM('     oracleStudy     ') AS result1 ( 왼쪽 공백 제거 )
, RTRIM('     oracleStudy     ') AS result1 ( 오른쪽 공백 제거 )
FROM DUAL;

 

특정 위치에서 반올림하는 ROUND 함수

반올림 위치 지정하지 않으면 소수점 첫 번째 자리에서 반올림 

예)

select ROUND(1234.5678) as ROUND, 
          ROUND(1234.5678, 1) as ROUND,
          ROUND(1234.5678, 2) as ROUND
    from dual;

 

특정 위치에서 버리는 TRUNC 함수

예)

select TRUNC(1234.5678) as TRUNC, 
       TRUNC(1234.5678, 1) as TRUNC,
       TRUNC(1234.5678, 2) as TRUNC
    from dual;

 

나머지 값을 구하는 MOD 함수

예)

select MOD(15, 6),
       MOD(10, 2),
       MOD(11, 2)
    from dual;

 

 

날짜 데이터를 다루는 날짜 함수 SYSDATE

select SYSDATE AS NOW,
       SYSDATE-1 AS YESTERDAY,
       SYSDATE+1 AS TOMORROW
    from dual;

 

몇 개월 이후 날짜를 구하는 ADD_MONTHS함수

예1) 3개월 후 ( 10년이면 120 ) 

select SYSDATE,
    ADD_MONTHS(SYSDATE, 3)
 from dual

 

예2) 입사 40년 미만인 사원 데이터 출력하기

SELECT EMPNO, ENAME, HIREDATE, SYSDATE
 FROM EMP
 WHERE ADD_MONTHS(HIREDATE, 480) > SYSDATE;

 

 

자료형을 변환하는 형 변환 함수

 

SYSDATE 날짜 형식 지정하여 출력

예)

SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS') AS 현재날짜시간
 FROM DUAL;

 

TO_DATE 함수로 문자 데이터를 날짜 데이터 변환하기

예)

SELECT TO_DATE('2018-07-14', 'YYYY-MM-DD') AS TODATE1,
       TO_DATE('20180714', 'YYYY-MM-DD') AS TODATE2
    FROM DUAL;

예2) 1981년 6월 1일 이후에 입사한 사원 정보 출력

SELECT *
 FROM EMP
WHERE HIREDATE > TO_DATE('1981-06-01', 'YYYY-MM-DD');

 

NVL 함수 ( NULL인 값을 0으로 대체해서 연산하기 )

SELECT ENAME, SAL, COMM, SAL+COMM, NVL(COMM, 0), SAL+NVL(COMM, 0)
FROM EMP;

 

NVL2 함수 ( 추가 수당 (COMM)열이 NULL이 아니라면 O를, NULL이라면 X를 표기 ) 추가 수당 존재 여부 확인

예)

SELECT EMPNO, ENAME, COMM, NVL2(COMM, '0', 'X'), NVL2(COMM, SAL*12+COMM, SAL*12) AS ANNSAL
FROM EMP

 

 

스프링

 

스프링을 이용한 객체 조립과 사용

 

생성자 방식

 

스프링 설정 클래스 ( 등록, 패스워드 변경 ) 

@Configuration // 스프링 설정 클래스 
public class StudentCtx {
	
	@Bean // ( DAO 빈 객체 생성
	public StudentDao studentDao() {
		return new StudentDao();
	}
	
	@Bean // DAO가 생성한 객체를  StudentRegisterService 생성자를 통해 주입 
	public StudentRegisterService studentRegisterService() {
		return new StudentRegisterService(studentDao());
	}
	
	@Bean // setStudentDao()메서드를 이용해서 의존 객체 주입 
	public StudentChangePasswordService studentChangePWdSvc() {
		StudentChangePasswordService pwdSvc = new StudentChangePasswordService();
		pwdSvc.setStudentDao(studentDao());
		return pwdSvc;
	}

 

Service 

package student;

import java.time.LocalDateTime;

import spring.DuplicateMemberException;
import spring.RegisterRequest;

public class StudentRegisterService {
    private StudentDao studentDao;

    // 생성자를 통해 StudentDao 객체를 주입받음
    // 의존 객체를 생성자를 통해 주입한다.
    public StudentRegisterService(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    // 학생 등록을 수행하는 메서드
    public Long regist(StudentRequest req) {
        // 입력한 전화번호로 이미 등록된 학생이 있는지 조회
        Student student = studentDao.selectByPhone(req.getPhone());

        // 같은 전화번호를 가진 학생이 이미 존재하면 예외 발생
        if (student != null) {
            throw new DuplicateMemberException("dup phone " + req.getPhone());
            // DuplicateMemberException 예외를 발생시키고 메시지를 설정하여 사용자에게 중복된 전화번호임을 알림
        }

        // 같은 전화번호를 가진 학생이 존재하지 않으면 DB에 삽입
        Student newStudent = new Student(
                req.getPhone(), req.getPassword(), req.getName(),
                LocalDateTime.now());
        studentDao.insert(newStudent); // 학생 정보를 DB에 삽입

        // 새로 등록한 학생의 ID를 반환
        return newStudent.getSid();
    }

	public void regist(RegisterRequest req) {
		// TODO Auto-generated method stub
		
	}
}

 

 

 

스프링 설정 클래스 ( 목록(list) 출력 ) 

 

	@Bean
	public StudentPrinter studentPrinter() {
		return new StudentPrinter();
	}
	
	@Bean
	public StudentListPrinter slistPrinter() {
		return new StudentListPrinter(studentDao(), studentPrinter());
	}

 

 

MemberPrinter 클래스

package student;

public class StudentPrinter {

	public void print(Student student) {
		System.out.printf(
				"회원 정보: 아이디=%d, 이메일=%s, 이름=%s, 등록일=%tF\n", 
				student.getSid(), student.getPhone(),
				student.getName(), student.getRegisterDateTime());
	}

}

 

MemberListPrinter 생성자로 두 개의 파라미터를 전달받는 클래스 ( DAO, Printer )

package student;

import java.util.Collection;

public class StudentListPrinter {

	private StudentDao studentDao;
	private StudentPrinter printer;

	public StudentListPrinter(StudentDao studentDao, StudentPrinter printer) {
		this.studentDao = studentDao;
		this.printer = printer;
	}

	public void printAll() {
		Collection<Student> students = studentDao.selectAll();
		students.forEach(m -> printer.print(m));
	}

}

 

세터 메서드 방식 ( 자바빈 규칙 Set, Get )

 

 

빈 객체 추가

	@Bean
	public StudentInfoPrinter studentInfoPrinter() {
		StudentInfoPrinter studentInfoPrinter = new StudentInfoPrinter();
		
		studentInfoPrinter.setStudentDao(studentDao());
		studentInfoPrinter.setStudentPrinter(studentPrinter());
		return studentInfoPrinter;
	}

 

 

지정한 이메일을 갖는 Student를 찾아서 정보를 콘솔에 출력

 

package student;

public class StudentInfoPrinter {

	private StudentDao studentDao;
	private StudentPrinter studentPrinter;

	public void printStudentInfo(String phone) {
		Student student = studentDao.selectByPhone(phone);
		if (student == null) {
			System.out.println("데이터 없음\n");
			return;
		}
		studentPrinter.print(student);
		System.out.println();
	}
	
	// DAO 세터 메서드 정의 
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
    // Printer 세터 메서드 정의
	public void setStudentPrinter(StudentPrinter studentPrinter) {
		this.studentPrinter = studentPrinter;
	}

}

 

 

 

빈 객체 추가

	@Bean
	public StudentInfoPrinter studentInfoPrinter() {
		StudentInfoPrinter studentInfoPrinter = new StudentInfoPrinter();
		
		studentInfoPrinter.setStudentDao(studentDao());
		studentInfoPrinter.setStudentPrinter(studentPrinter());
		return studentInfoPrinter;
	}

 

지정한 버전 출력 ( 두 개의 int 타입 값을 세터 메서드로 전달 받음 ) 

package student;

public class StudentVersionPrinter {

	private int majorVersion;
	private int minorVersion;

	public void print() {
		System.out.printf("이 프로그램의 버전은 %d.%d입니다.\n\n",
				majorVersion, minorVersion);
	}

	public void setMajorVersion(int majorVersion) {
		this.majorVersion = majorVersion;
	}

	public void setMinorVersion(int minorVersion) {
		this.minorVersion = minorVersion;
    }

}

 

 

실행

package student;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import spring.DuplicateMemberException;
import spring.MemberNotFoundException;
import spring.WrongIdPasswordException;

public class StudentForSpring {

	private static ApplicationContext ctx = null;
	
	public static void main(String[] args) throws IOException {
		ctx = new AnnotationConfigApplicationContext(StudentCtx.class);
		
		BufferedReader reader = 
				new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			System.out.println("명령어를 입력하세요:");
			String command = reader.readLine();
			if (command.equalsIgnoreCase("exit")) {
				System.out.println("종료합니다.");
				break;
			}
			if (command.startsWith("new ")) {
				processNewCommand(command.split(" "));
				continue;
			} else if (command.startsWith("change ")) {
				processChangeCommand(command.split(" "));
				continue;
			} else if (command.equals("list")) {
				processListCommand();
				continue;
			} else if (command.startsWith("info ")) {
				processInfoCommand(command.split(" "));
				continue;
			} else if (command.equals("version")) {
				processVersionCommand();
				continue;
			}
			printHelp();
		}
	}
	// new 생성
	private static void processNewCommand(String[] arg) {
		if (arg.length != 5) {
			printHelp();
			return;
		}
		StudentRegisterService regSvc = 
				ctx.getBean("studentRegisterService", StudentRegisterService.class);
		StudentRequest req = new StudentRequest();
		req.setPhone(arg[1]);
		req.setName(arg[2]);
		req.setPassword(arg[3]);
		req.setConfirmPassword(arg[4]);
		
		if (!req.isPasswordEqualToConfirmPassword()) {
			System.out.println("암호와 확인이 일치하지 않습니다.\n");
			return;
		}
		try {
			regSvc.regist(req);
			System.out.println("등록했습니다.\n");
		} catch (DuplicateMemberException e) {
			System.out.println("이미 존재하는 이메일입니다.\n");
		}
	}
	// change 비밀번호 변경
	private static void processChangeCommand(String[] arg) {
		if (arg.length != 4) {
			printHelp();
			return;
		}
		StudentChangePasswordService studentPwdSvc = 
				ctx.getBean("studentChangePWdSvc", StudentChangePasswordService.class);
		try {
			studentPwdSvc.changePassword(arg[1], arg[2], arg[3]);
			System.out.println("암호를 변경했습니다.\n");
		} catch (MemberNotFoundException e) {
			System.out.println("존재하지 않는 이메일입니다.\n");
		} catch (WrongIdPasswordException e) {
			System.out.println("이메일과 암호가 일치하지 않습니다.\n");
		}
	}
	// 오류 처리 
	private static void printHelp() {
		System.out.println();
		System.out.println("잘못된 명령입니다. 아래 명령어 사용법을 확인하세요.");
		System.out.println("명령어 사용법:");
		System.out.println("new 이메일 이름 암호 암호확인");
		System.out.println("change 이메일 현재비번 변경비번");
		System.out.println();
	}
	// 목록 ( list ) 출력
	private static void processListCommand() {
		StudentListPrinter slistPrinter = 
				ctx.getBean("slistPrinter", StudentListPrinter.class);
		slistPrinter.printAll();
	}
	// 지정한 이메일을 갖는 정보를 찾아서 출력
	private static void processInfoCommand(String[] arg) {
		if (arg.length != 2) {
			printHelp();
			return;
		}
		StudentInfoPrinter studentInfoPrinter = 
				ctx.getBean("studentInfoPrinter", StudentInfoPrinter.class);
		studentInfoPrinter.printStudentInfo(arg[1]);
	}
	// 버전 정보 출력
	private static void processVersionCommand() {
		StudentVersionPrinter studentVersionPrinter = 
				ctx.getBean("studentVersionPrinter", StudentVersionPrinter.class);
		studentVersionPrinter.print();
	}

}

 

'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정' 카테고리의 다른 글

2023-07-26 49일차  (0) 2023.07.26
2023-07-25 48일차  (0) 2023.07.25
2023-07-21 46일차  (0) 2023.07.21
2023-07-20 45일차  (0) 2023.07.20
2023-07-19 44일차  (0) 2023.07.19

 

스프링 이론

스프링의 목적 이해하기: 스프링이 왜 만들어졌고, 어떤 문제를 해결하기 위해 등장했는지를 이해하는 것이 중요합니다. 스프링은 자바 기반의 애플리케이션을 보다 효율적으로 개발하고 관리하기 위해 만들어진 프레임워크입니다.

경량 컨테이너 개념: 스프링은 "경량 컨테이너"라고도 불리는데, 이는 스프링이 애플리케이션의 객체를 생성하고 관리하는 역할을 한다는 의미입니다. 이 컨테이너가 의존성 주입(Dependency Injection)을 통해 객체 간의 결합도를 낮추고, 객체를 더 효율적으로 관리하는 기능을 제공합니다.

IoC와 DI 개념: IoC(Inversion of Control)와 DI(Dependency Injection)는 스프링의 핵심 개념입니다. IoC는 제어의 역전을 의미하며, 스프링이 개발자가 아닌 스프링 컨테이너가 애플리케이션의 흐름과 제어를 맡는다는 개념입니다. DI는 의존성 주입으로, 객체들 간의 의존 관계를 스프링 컨테이너가 자동으로 관리한다는 개념입니다.

스프링의 핵심 모듈 이해: 스프링은 여러 개의 모듈로 구성되어 있습니다. 가장 기본적인 핵심 모듈로는 스프링 컨테이너를 제공하는 "spring-core"와 의존성 주입을 지원하는 "spring-beans" 등이 있습니다. 또한 웹 개발에 필요한 "spring-web" 모듈, 데이터베이스 연동을 위한 "spring-jdbc" 모듈 등이 있습니다.

스프링의 주요 기능: 스프링은 다양한 기능을 제공합니다. 예를 들어, AOP(Aspect-Oriented Programming)를 통해 애플리케이션의 여러 부분에서 공통 로직을 적용할 수 있고, 트랜잭션 관리, 보안, 캐싱 등의 기능을 제공합니다.

예제와 실습: 스프링의 개념을 이해하는 가장 좋은 방법은 예제와 실습을 통해 직접 코드를 작성하고 실행해보는 것입니다. 스프링 공식 문서나 온라인 자습서를 참고하여 간단한 예제를 따라해보면서 스프링의 개념을 익힐 수 있습니다.

커뮤니티와 자료 활용: 스프링은 매우 큰 개발자 커뮤니티를 가지고 있으며, 다양한 자료와 토론을 통해 지식을 공유하고 학습할 수 있습니다. 스프링 공식 홈페이지와 포럼, 온라인 개발자 커뮤니티, 블로그, 동영상 강의 등을 활용하여 스프링에 대한 정보를 얻을 수 있습니다.

이러한 접근 방법을 따라가면서 스프링의 개념을 체계적으로 이해할 수 있으며, 간단한 예제와 실습을 통해 실제로 스프링을 활용하는 방법을 익힐 수 있을 것입니다. 스프링은 기능이 많고 다양한 영역에서 사용되기 때문에 처음에는 어려움을 느낄 수 있지만, 단계적으로 학습하면서 점차 스프링을 마스터할 수 있을 것입니다.

 

 

조립기

스프링(Spring)의 조립기는 스프링 프레임워크에서 IoC(Inversion of Control)와 DI(Dependency Injection)를 구현하는 핵심 컴포넌트입니다. 스프링의 조립기는 XML 또는 Java Config와 같은 설정을 사용하여 애플리케이션의 객체들을 생성하고 관리하는 역할을 담당

조립기는 애플리케이션에서 사용할 객체들의 생성과 의존성 주입을 관리하여 객체들 간의 결합도를 낮추고 유연한 애플리케이션을 구축할 수 있도록 도와준다.

 

 

객체 생성하고 관리하는 방법

1. XML 설정 파일을 사용한 조립기: 스프링에서는 applicationContext.xml과 같은 XML 설정 파일을 사용하여 빈(Bean)들의 설정과 의존성 주입 정보를 정의합니다. 이러한 설정 파일을 스프링 컨테이너가 읽어들여서 빈들을 생성하고 관리합니다.

2. Java Config를 사용한 조립기: XML 대신에 자바 코드를 사용하여 빈들을 설정하는 방법도 있습니다. @Configuration 어노테이션을 사용하여 Java Config 클래스를 만들고, @Bean 어노테이션을 사용하여 빈을 등록합니다.

3. 주입 방식에 따른 조립기: 스프링 조립기는 생성자 주입, Setter 주입 등 다양한 주입 방식을 지원합니다. 이를 통해 빈들 간의 의존 관계를 자동으로 해결할 수 있습니다.

SqlSession

MyBatis는 데이터베이스와의 상호작용을 위해 SqlSession 객체를 사용

데이터베이스 트랜잭션을 시작하고, SQL 매퍼 구문을 실행하며, 결과를 가져오는 등

데이터베이스와의 모든 상호작용을 처리하는 주요 객체

 

SqlSessionFactory

MyBatis의 핵심 인터페이스로, 데이터베이스 연결을 설정하고 SqlSession을 생성하는 역할을 합니다.

openSession() 메서드를 호출하면 SqlSession 객체가 반환

 

SqlSession sqlSession = sqlSessionFactory.openSession();

SqlSessionFactory를 사용하여 데이터베이스와의 세션을 열고 SqlSession 객체를 생성하는 부분입니다.

이후에는 sqlSession 객체를 사용하여 데이터베이스와의 상호작용을 수행할 수 있습니다.

 


 

서블릿 마이바티스 사용할 때 밑에 코드 맨 윗 줄에 위치 

	private static SqlSessionFactory sqlSessionFactory;

	static {
		try {
			// 마이바티스 환경 설정 XML 파일 경로
			String resource = "resources/mybatis/config-mybatis.xml";
			Reader reader = Resources.getResourceAsReader(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 


마이바티스 EmpMapper.xml에서 데이터 내역 가져오기 ( Select )

 

Mapper.xml

    <select id="selectGoods" resultType="com.db.Goods">
    	select num, thing, recdate
      	  from goods 
    </select>
    
    resultType = "DTO 경로"

 

Servlet.java

		SqlSession sqlSession = sqlSessionFactory.openSession();

		try {
			// 조회 매핑 구문 실행
			List<Object> list = sqlSession.selectList("org.mybatis.persistence.EmpMapper.selectGoods");
			
			request.setAttribute("list", list);
			request.getRequestDispatcher("index-result.jsp")
			.forward(request, response);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 세션 및 트랜잭션 종료
			sqlSession.close();
		}

 

request.setAttribute("list", list);는 JSP와 Servlet 간에 데이터를 전달하는 방법 중 하나인 "request 스코프"를 이용하는 코드

JSP 페이지에서 ${키}를 사용하여 해당 데이터에 접근할 수 있다

list라는 데이터를 "list"라는 이름으로 JSP 페이지에 전달할 수 있게 된다.

 

jsp에서 itemList = ${list}; 이렇게 하고 html 부분에 <div id="itemList">하게 되면 해당 부분 출력 가능 

 

 request.getRequestDispatcher("index-result.jsp").forward(request, response);
 
 index-result.jsp라는 JSP 파일로 서블릿의 처리 결과를 넘겨주고, 
 해당 JSP 페이지에서 클라이언트에게 결과를 표시하도록 하는 역할

 

 

 

 


마이바티스 EmpMapper.xml에서 데이터 추가 ( Insert )

서블릿 마이바티스 세션 연결 코드는 동일

 

Mapper.xml

    <insert id="insertGoods" parameterType="com.db.Goods">
        <selectKey keyProperty="num" resultType="java.lang.Integer"
                                                    order="BEFORE">
        select SEQ_GOODS.NEXTVAL from dual
    	</selectKey>
    	insert into goods(num, thing, recdate)
    	values(#{num}, #{thing}, sysdate)
    </insert>

selectKey로 시퀀스 num값 자동으로 받아오기 가능 

 

 

Servlet.java

		String thing = request.getParameter("thing");
		// 세션 및 트랜잭션 시작
				SqlSession sqlSession = sqlSessionFactory.openSession();

				try {
					// 새로 고침 안해도 번호 받아오기
					Goods goods = new Goods(0, thing, null);
					// 조회 매핑 구문 실행
					int res = sqlSession.insert("org.mybatis.persistence.EmpMapper.insertGoods"
							, goods);
					response.getWriter().print(goods.getNum());
					sqlSession.commit();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					// 세션 및 트랜잭션 종료
					sqlSession.close();
				}

 

"thing" 파라미터의 값을 가져오는 역할

"thing" 파라미터를 전달하지 않았을 경우, null이 반환

서버에서 "thing"  값을 보내주면 서블릿에서 받아서 Mapper.insertGoods로 전달해서 마이바티스가 DB에 저장

 

Goods goods = new Goods(0, thing, null);
					
int res = sqlSession.insert("org.mybatis.persistence.EmpMapper.insertGoods"
			, goods);
이렇게 작성 안하고


					
int res = sqlSession.insert("org.mybatis.persistence.EmpMapper.insertGoods"
			, new Goods(0, thing, null));
이렇게 작성시 insert는 되나 동적으로 num값을 가져올 수 없고 새로고침 해야됨

 

마이바티스 EmpMapper.xml에서 데이터 제거 ( Delete )

 

Mapper.xml

    <delete id="deleteGoods" parameterType="int">
    
    	delete from goods where num = #{num} 
    </delete>

 

Servlet.java

        int num = Integer.parseInt(request.getParameter("num")); 

        // 세션 및 트랜잭션 시작
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            // 조회 매핑 구문 실행
            int res = sqlSession.delete("org.mybatis.persistence.EmpMapper.deleteGoods",
                    num);
			response.getWriter().print(res);
			sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 세션 및 트랜잭션 종료
            sqlSession.close();
        }

 

int num = Integer.parseInt(request.getParameter("num"));

클라이언트에서 서버로 전달된 "num" 파라미터 값을 가져와서 String 타입에서 int 타입으로 변환

전달된 값은 String 타입이기때문에 int타입으로 변환 필요

 

나머지는 insert와 동일

 


 

// 클라이언트 측에서 관리되는 배열로, 데이터 베이스 내용을 담음
let itemList = [];

const addBtn = document.querySelector("#add"); // 버튼의 id

//클릭 이벤트가 발생하면 addList() 함수가 실행되도록 함
addBtn.addEventListener("click", addList);

// insert 함수 
function addList() {
	// 사용자가 입력한 값(HTML)을 나타내는 input 요소 
    let str = document.querySelector("#item"); 
    
    // 비동기적인 서버와의 통신을 수행할 준비를 함
    const xhr = new XMLHttpRequest();
    
    if (str.value != null && str.value != '') {
	
		let num;
		console.log(str.value);
		
		// 서버로 보낼 요청을 설정
		// GET 방식으로 'ExecAjax' 서블릿에 'thing'이라는 파라미터와 함께 사용자가 입력한 값을 전달
		xhr.open('GET', 'ExecAjax?thing=' + str.value, true);
		xhr.send();// 서버로 요청을 전송
		
		// 서버에서 응답이 도착했을 때 실행될 함수를 지정
		xhr.onload= ()=>{
			if(xhr.status == 200){
				console.log("성공")
				num = xhr.responseText;// 서버에서 반환된 응답 데이터를 num 변수에 저장
				
				//서버 응답으로 받은 num 값과 사용자가 입력한 str.value 값을 객체 형태로 itemList 배열에 추가
				itemList.push({idx:num, thing:str.value});
				    str.value = '';
        			str.focus();//  입력한 아이템 값을 비워주고, 다시 입력할 수 있도록 입력 필드에 포커스
        			showList();// 리스트를 갱신
				
			}else{
				console.log("실패");
			}
		}
		

    }
    
}
// itemList 배열에 있는 데이터를 가지고 HTML에 리스트 형태로 표시하는 역할
function showList() {
    let list = "<ul>";
    for(let i=0; i < itemList.length; i++) {
        list += `<li>${itemList[i].thing}<span class='close' id='${itemList[i].idx}'>X</span></li>`;
    }
    
    list += "</ul>";
    document.querySelector("#itemList").innerHTML = list; // div의 id

    let remove = document.querySelectorAll(".close");
    //console.log(remove);
    for(let i = 0; i < remove.length; i++) {
        remove[i].addEventListener("click", removeList);
    }
}

function removeList() {
	const xhr = new XMLHttpRequest();
    let id = this.getAttribute("id");
    	xhr.open('GET', 'DelServ?num=' + id, true);
		xhr.send();
		
    		xhr.onload= ()=>{
			if(xhr.status == 200){
				console.log("성공")
				let num = xhr.responseText;
			}else{
				console.log("실패");
			}
		}
    
    
    //console.log(id);
    //itemList.splice(id, 1);
    itemList = itemList.filter(t => t.idx != id);
    showList();
}

스프링

 

Toy클래스 Battery 클래스에 의존 => 주입으로 변경

위에 주석처리 의존상태를 주입하기 

package toy;

public class Toy {
	
	// 의존 상태
//	private Battery battery = new Battery(10);
	
	private Battery battery;
	// DI 방식 : 세터 
	public void setBattery(Battery battery) {
		this.battery = battery;
	}

	public void move() {
		System.out.println("장난감 움직인다.");
		battery.changePower();
	}
	
	public boolean isBattery() {
		return battery.getPower() > 0;
	}
	
}

 

 

배터리 클래스는 변경내용 없음

package toy;

public class Battery {
	private int power;
	// DI방식 : 생성자
	public Battery(int power) {
		this.power = power;
	}
	
	public int getPower() {
		return power;
	}
	
	public void changePower() {
		this.power--;
	}
}

 

@Bean 추가

	@Bean
	public Battery battery() {
		
		return new Battery(10);
	}
	
	@Bean
	public Toy toy() {
		Toy t = new Toy();
		t.setBattery(battery());
		return t;
	}

 

 

실행

 

package toy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import config.AppCtx;

public class ToyEx {

	public static void main(String[] args) {
		ApplicationContext ctx = new AnnotationConfigApplicationContext(AppCtx.class);
		Toy toy = ctx.getBean("toy", Toy.class);
		while (toy.isBattery()) {
			toy.move();
		}
		
		
		System.out.println("배터리 부족으로 멈춤.");

	}

}
<결과>
7월 21, 2023 6:55:38 오후 org.springframework.context.support.AbstractApplicationContext prepareRefresh
정보: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@27efef64: startup date [Fri Jul 21 18:55:38 KST 2023]; root of context hierarchy
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
장난감 움직인다.
배터리 부족으로 멈춤.

10번 출력 후 스톱

 

 

 

Swing, 스프링 회원 가입, 비밀번호 변경,  list 출력

Win1.java 스윙 전체 코드 

package swing;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import config.AppCtx;
import spring.ChangePasswordService;
import spring.DuplicateMemberException;
import spring.MemberListPrinter;
import spring.MemberNotFoundException;
import spring.MemberRegisterService;
import spring.RegisterRequest;
import spring.WrongIdPasswordException;

public class Win1 extends JFrame {
	
	private ApplicationContext ctx = 
			new AnnotationConfigApplicationContext(AppCtx.class);
	
	private JFrame frame = this;
	
	Win1() {
		
		
		JButton bt1 = new JButton("new");
		JButton bt2 = new JButton("change");
		JButton bt3 = new JButton("list");
		JButton bt4 = new JButton("info");
		
		JTextArea ta = new JTextArea();

		JTextField tf1 = new JTextField();
		JTextField tf2 = new JTextField();
		JTextField tf3 = new JTextField();
		JTextField tf4 = new JTextField();
		
		JLabel lb1 = new JLabel("email");
		JLabel lb2 = new JLabel("name");
		JLabel lb3 = new JLabel("password");
		JLabel lb4 = new JLabel("confirm");
		
		Container c = this.getContentPane();
		this.setLayout(null);
		this.setTitle("person");
		this.setSize(430, 300);
		this.setLocation(500, 500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		bt1.setSize(70,  30);              // 폭과 높이 조절
		bt1.setLocation(20, 230);          // 위치 조정
		c.add(bt1);
		bt2.setSize(70,  30);              // 폭과 높이 조절
		bt2.setLocation(120, 230);          // 위치 조정
		c.add(bt2);
		bt3.setSize(70,  30);              // 폭과 높이 조절
		bt3.setLocation(220, 230);          // 위치 조정
		c.add(bt3);
		bt4.setSize(70,  30);              // 폭과 높이 조절
		bt4.setLocation(320, 230);          // 위치 조정
		c.add(bt4);
		
		tf1.setSize(70,  30);              // 폭과 높이 조절
		tf1.setLocation(20, 30);          // 위치 조정
		c.add(tf1);
		tf2.setSize(70,  30);              // 폭과 높이 조절
		tf2.setLocation(120, 30);          // 위치 조정
		c.add(tf2);
		tf3.setSize(70,  30);              // 폭과 높이 조절
		tf3.setLocation(220, 30);          // 위치 조정
		c.add(tf3);
		tf4.setSize(70,  30);              // 폭과 높이 조절
		tf4.setLocation(320, 30);          // 위치 조정
		c.add(tf4);
		
		lb1.setSize(70,  30);              // 폭과 높이 조절
		lb1.setLocation(20, 5);          // 위치 조정
		c.add(lb1);
		lb2.setSize(70,  30);              // 폭과 높이 조절
		lb2.setLocation(120, 5);          // 위치 조정
		c.add(lb2);
		lb3.setSize(70,  30);              // 폭과 높이 조절
		lb3.setLocation(220, 5);          // 위치 조정
		c.add(lb3);
		lb4.setSize(70,  30);              // 폭과 높이 조절
		lb4.setLocation(320, 5);          // 위치 조정
		c.add(lb4);
		
		JScrollPane scrollPane = new JScrollPane(ta);
		ta.setCaretPosition(ta.getDocument().getLength());
		scrollPane.setSize(350,  80);
		scrollPane.setLocation(30,  100);
		c.add(scrollPane);
		
		this.setVisible(true);
		
		bt1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//System.out.println("클릭!");
				//ta.append("클릭!\n");
				// JOptionPane.showMessageDialog(frame, "Welcome to Swing"); // alret 창
				
				// Help 입력 빈 칸인지 확인
				if("".equals(tf1.getText())||
						"".equals(tf2.getText())||
						"".equals(tf3.getText())||
						"".equals(tf4.getText())) {
					JOptionPane.showMessageDialog
					(frame, "명령어 사용법을 확인하세요.\n " + "이메일 이름 암호 암호확인 모두 입력하세요.");
					return;
				}
				
				MemberRegisterService regSvc = 
						ctx.getBean("memberRegSvc", MemberRegisterService.class);
				RegisterRequest req = new RegisterRequest();
				req.setEmail(tf1.getText());
				req.setName(tf2.getText());
				req.setPassword(tf3.getText());
				req.setConfirmPassword(tf4.getText());
				
				if (!req.isPasswordEqualToConfirmPassword()) {
					JOptionPane.showMessageDialog(frame, "암호와 확인이 일치하지 않습니다.\n");
					return;
				}
				try {
					regSvc.regist(req);
					JOptionPane.showMessageDialog(frame, "등록했습니다.\n");
				} catch (DuplicateMemberException ed) {
					JOptionPane.showMessageDialog(frame, "이미 존재하는 이메일입니다.\n");
				}
			}
		});
		
		bt2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if("".equals(tf1.getText())||
						"".equals(tf2.getText())||
						"".equals(tf3.getText())||
						"".equals(tf4.getText())) {
					JOptionPane.showMessageDialog
					(frame, "명령어 사용법을 확인하세요.\n " + "이메일 암호 암호확인 모두 입력하세요.");
					return;
				}
				
				ChangePasswordService changePwdSvc = 
						ctx.getBean("changePwdSvc", ChangePasswordService.class);
				try {
					changePwdSvc.changePassword(tf1.getText(), tf3.getText(), tf4.getText());
					JOptionPane.showMessageDialog(frame, "암호를 변경했습니다.\n");
				} catch (MemberNotFoundException de) {
					JOptionPane.showMessageDialog(frame, "존재하지 않는 이메일입니다.\n");
				} catch (WrongIdPasswordException de) {
					JOptionPane.showMessageDialog(frame, "이메일과 암호가 일치하지 않습니다.\n");
				}
				
			}
		});
		
		
		bt3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberListPrinter listPrinter = 
						ctx.getBean("listPrinter", MemberListPrinter.class);
				//listPrinter.printAll();
				
				//String str = String.format("%s, %s, %s, %s\n", tf1.getText(),tf2.getText(),tf3.getText(),tf4.getText());
				//ta.append(str);
				
				ta.setText(listPrinter.printAllS());
			}
		});
		
	}

	public static void main(String[] args) {
		Win1 win = new Win1();

	}

}

 

 

코드 분석

 

@Bean 설정정보 가져오기 Appctx클래스를 생성자 파라미터로 전달

	private ApplicationContext ctx = 
			new AnnotationConfigApplicationContext(AppCtx.class);

 

 

빈칸 입력 시 명령어 사용법 도움 처리 

				if("".equals(tf1.getText())||
						"".equals(tf2.getText())||
						"".equals(tf3.getText())||
						"".equals(tf4.getText())) {
					JOptionPane.showMessageDialog
					(frame, "명령어 사용법을 확인하세요.\n " + "이메일 이름 암호 암호확인 모두 입력하세요.");
					return;
				}

 

 

알럿 띄우기 

내부 익명 클래스에서 외부 클래스의 멤버 변수에 접근을 할라면

외부에 private JFrame frame = this; 선언

메시지 대화상자를 현재 프레임에 띄울 수 있게 하기 위해  외부에 선언 후 밑에 처럼 내부 익명클래스에서 사용 가

JOptionPane.showMessageDialog(frame, "띄울 메세지");

 

입력한 내용 전체 list 출력 하기 

package spring;

public class MemberPrinter {

	public void print(Member member) {
		System.out.printf(
				"회원 정보: 아이디=%d, 이메일=%s, 이름=%s, 등록일=%tF\n", 
				member.getId(), member.getEmail(),
				member.getName(), member.getRegisterDateTime());
	}
	
	public String printS(Member member) {
		return 	String.format("회원 정보: 아이디=%d, 이메일=%s, 이름=%s, 등록일=%tF\n", 
				member.getId(), member.getEmail(),
				member.getName(), member.getRegisterDateTime());
	}
}

 

위에 print 메서드는 다른 실행문에서 사용하기 때문에 밑에 printS 추가로 메서드 선언

 

package spring;

import java.util.Collection;

public class MemberListPrinter {

	private MemberDao memberDao;
	private MemberPrinter printer;

	public MemberListPrinter(MemberDao memberDao, MemberPrinter printer) {
		this.memberDao = memberDao;
		this.printer = printer;
	}

	public void printAll() {
		Collection<Member> members = memberDao.selectAll();
		members.forEach(m -> printer.print(m));
		
//		for(Member member : members) {
//			printer.print(member);
//		}
	}
	
	public String printAllS() {
		Collection<Member> members = memberDao.selectAll();
		
		String str = "";
		
		for(Member member : members) {
			str += printer.printS(member);
			
		}
		return str;
		

	}

}

 

결과

 

 

 

'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정' 카테고리의 다른 글

2023-07-25 48일차  (0) 2023.07.25
2023-07-24 47일차  (0) 2023.07.24
2023-07-20 45일차  (0) 2023.07.20
2023-07-19 44일차  (0) 2023.07.19
2023-07-18 43일차  (0) 2023.07.18

+ Recent posts