일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
- 오라클
- 자바스크립트
- javascript
- 평일코딩
- 정보처리기사요약
- spring
- 국비IT
- 자스코테
- 자바스크립트 코딩테스트
- 자바의정석
- php
- 이안의평일코딩
- ReactNative
- 리액트네이티브
- VUE
- 코딩테스트
- Java의정석
- 국비코딩
- 정보처리기사실기
- 타입스크립트
- 스프링
- react
- 정보처리기사정리
- 정보처리기사
- typescript
- Oracle
- 정보처리기사실기정리
- CSS
- 리액트
- 정보처리기사실기요약
- Today
- Total
이안의 평일코딩
Spring 15일차 - AOP 본문
2020.11.17(화)
mvnrepository.com/artifact/javax.validation/validation-api/2.0.1.Final
mvnrepository.com/artifact/org.hibernate/hibernate-validator/6.1.6.Final
web.xml 복붙
pom.xml에 추가
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 유효성 검사 (12장) -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.6.Final</version>
</dependency>
|
cs |
1. 프로젝트 생성
프로젝트명 => 패키지 설정(3개이상)
2. 자바 버전 변경
2-2. pom.xml(라이브러리 추가)
=>라이브러리가 없는 경우 : mvnrepository.com 검색후 추가
3. web.xml 변경
4. application-context.xml 설정 / 자바로 설정
5. 자바 (Model, DAO, VO) => html
com.sist.web
EmpController
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
|
package com.sist.web;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.sist.dao.*;
@Controller
public class EmpController {
@Autowired
private EmpDAO dao;
@GetMapping("emp/list.do")
public String emp_list(Model model){
List<EmpVO> list=dao.empListData();
model.addAttribute("list", list);
return "emp/list";
}
@GetMapping("emp/detail.do")
public String emp_detail(int empno, Model model){
EmpVO vo=dao.empDetailData(empno);
model.addAttribute("vo", vo);
return "emp/detail";
}
}
|
cs |
com.sist.dao
EmpVO
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package com.sist.dao;
import java.util.*;
public class EmpVO {
private int empno;
private String ename;
private String job;
private Date hiredate;
private int sal;
private String dname;
private String loc;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
|
cs |
MyDataSource
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
31
32
33
34
|
package com.sist.dao;
// BasicDataSource
public class MyDataSource {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
|
cs |
EmpDAO
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package com.sist.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Repository //메모리할당
public class EmpDAO {
@Autowired //application-context.xml에서 값을 불러옴
private MyDataSource ds; //bean id="ds"
private Connection conn;
private PreparedStatement ps;
public EmpDAO(){
try{
Class.forName(ds.getDriverClassName());
}catch(Exception ex){}
}
//반복 구간
public void getConnection(){
try{
conn=DriverManager.getConnection(ds.getUrl(),ds.getUsername(),ds.getPassword());
}catch(Exception ex){}
}
public void disConnection(){
try{
if(ps!=null) ps.close();
if(conn!=null) conn.close();
}catch(Exception ex){}
}
//AOP => 반복 제거 프로그램
//@Transactional // 밑의 @어노테이션 기능 총 집합
/*
* 스프링
* = 공통 모듈 => 공통관심사 ==> 스프링에서 담당(AOP) Callback함수 (시스템에 의해 자동 호출)
* = 핵심 모듈 => 핵심관심사 ==> 프로그래머가 코딩
* = 반복을 제거하는 프로그램 AOP(트랜잭션, 로그파일, 보안)
*/
public List<EmpVO> empListData(){
List<EmpVO> list=new ArrayList<EmpVO>();
// @Before
try{
// @Around => 실행 전 => SetAutoCommit(false)
// 핵심 코딩(DML)
// 실행 후 => commit
getConnection();
String sql="SELECT empno,ename,job,hiredate,sal,"
+"(SELECT dname FROM dept d WHERE d.deptno=e.deptno) as dname,"
+"(SELECT loc FROM dept d WHERE d.deptno=e.deptno) as loc "
+"FROM emp e";
ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
EmpVO vo=new EmpVO();
vo.setEmpno(rs.getInt(1));
vo.setEname(rs.getString(2));
vo.setJob(rs.getString(3));
vo.setHiredate(rs.getDate(4));
vo.setSal(rs.getInt(5));
vo.setDname(rs.getString(6));
vo.setLoc(rs.getString(7));
list.add(vo);
}
rs.close();
}catch(Exception ex){
// @After-Throwing => rollback
System.out.println(ex.getMessage());
}finally{
// @After => setAutoCommit(true)
disConnection();
}
return list; // @After-Returning 정상수행 했을 때
}
// 상세보기
public EmpVO empDetailData(int empno){
EmpVO vo=new EmpVO();
try{
getConnection();
String sql="SELECT empno,ename,job,hiredate,sal,"
+"(SELECT dname FROM dept d WHERE d.deptno=e.deptno) as dname,"
+"(SELECT loc FROM dept d WHERE d.deptno=e.deptno) as loc "
+"FROM emp e "
+"WHERE empno=?";
ps=conn.prepareStatement(sql);
ps.setInt(1, empno);
ResultSet rs=ps.executeQuery();
rs.next();
vo.setEmpno(rs.getInt(1));
vo.setEname(rs.getString(2));
vo.setJob(rs.getString(3));
vo.setHiredate(rs.getDate(4));
vo.setSal(rs.getInt(5));
vo.setDname(rs.getString(6));
vo.setLoc(rs.getString(7));
rs.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}finally{
disConnection();
}
return vo;
}
}
|
cs |
WEB-INF/config 폴더 생성
application-context.xml (p : setmethod에 값을채움)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.sist.*"/>
<bean id="ds"
class="com.sist.dao.MyDataSource"
p:driverClassName="oracle.jdbc.driver.OracleDriver"
p:url="jdbc:oracle:thin:@localhost:1521:XE"
p:username="hr"
p:password="happy"
/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/"
p:suffix=".jsp"
/>
</beans>
|
cs |
webapp/emp
list.jsp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style type="text/css">
.row{
margin: 0px auto;
width:800px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<h1 class="text-center">사원목록</h1>
<table class="table table-striped">
<tr class="success">
<th>사번</th>
<th>이름</th>
<th>직위</th>
<th>입사일</th>
<th>급여</th>
<th>부서명</th>
<th>근무지</th>
</tr>
<c:forEach var="vo" items="${list }">
<tr>
<td>${vo.empno }</td>
<td><a href="detail.do?empno=${vo.empno }">${vo.ename }</a></td>
<td>${vo.job }</td>
<td><fmt:formatDate value="${vo.hiredate }" pattern="yyyy-MM-dd"/></td>
<td>${vo.sal }</td>
<td>${vo.dname }</td>
<td>${vo.sal }</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
|
cs |
@GetMapping : 메소드방식(데이터 전송) : GET <a> sendRedirect(), location.href
@PostMapping : POST => form, ajax
@RequestMapping (@GetMapping+@PostMapping)
webapp/emp
detail.jsp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style type="text/css">
.row{
margin: 0px auto;
width:350px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<h1 class="text-center"><${vo.ename }>사원 정보</h1>
<table class="table">
<tr>
<td width=25% class="text-right">사번</td>
<td width=75%>${vo.empno }</td>
</tr>
<tr>
<td width=25% class="text-right">이름</td>
<td width=75%>${vo.ename }</td>
</tr>
<tr>
<td width=25% class="text-right">직위</td>
<td width=75%>${vo.job }</td>
</tr>
<tr>
<td width=25% class="text-right">입사일</td>
<td width=75%><fmt:formatDate value="${vo.hiredate }" pattern="yyyy-MM-dd"/></td>
</tr>
<tr>
<td width=25% class="text-right">급여</td>
<td width=75%>${vo.sal }</td>
</tr>
<tr>
<td width=25% class="text-right">부서명</td>
<td width=75%>${vo.dname }</td>
</tr>
<tr>
<td width=25% class="text-right">근무지</td>
<td width=75%>${vo.loc }</td>
</tr>
</table>
</div>
</div>
</body>
</html>
|
cs |
오라클
1. 테이블 생성 (제약조건)
2. View, Sequence, Procedure, Function, Trigger
스프링
1. AOP
SpringMVCProject2
기본: jdk1.8수정, pom.xml, web.xml 복붙, 필요없는폴더,자바삭제
WEB-INF/config
application-context.xml
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
31
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- AOP적용 -->
<aop:aspectj-autoproxy/>
<!-- 사용자 클래스 메모리 할당 -->
<context:component-scan base-package="com.sist.*"/>
<!--
1. 메모리 할당 => 어노테이션 사용
2. 메모리 할당 => 값 주입 (XML)
-->
<bean id="dbconn"
class="com.sist.dao.DBConnection"
c:driver="oracle.jdbc.driver.OracleDriver"
c:url="jdbc:oracle:thin:@localhost:1521:XE"
c:password="happy"
c:username="hr"
/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/"
p:suffix=".jsp"
/>
</beans>
|
cs |
src/main/java
com.sist.web
EmpController위와 동일
com.sist.dao
EmpVO위와 동일
EmpDAO (getConnection, disConnection삭제, 밑 코드 추가)
1
2
3
4
5
6
7
|
@Autowired
private DBConnection dbConn;
private Connection conn;
private PreparedStatement ps;
public EmpDAO(){
conn=dbConn.getConn();
}
|
cs |
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.sist.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Repository //메모리할당
public class EmpDAO {
@Autowired
private DBConnection dbConn;
private Connection conn;
private PreparedStatement ps;
public EmpDAO(){
}
public List<EmpVO> empListData(){
List<EmpVO> list=new ArrayList<EmpVO>();
// @Before
try{
// @Around => 실행 전 => SetAutoCommit(false)
// 핵심 코딩(DML)
// 실행 후 => commit
String sql="SELECT empno,ename,job,hiredate,sal,"
+"(SELECT dname FROM dept d WHERE d.deptno=e.deptno) as dname,"
+"(SELECT loc FROM dept d WHERE d.deptno=e.deptno) as loc "
+"FROM emp e";
ps=dbConn.getConn().prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
EmpVO vo=new EmpVO();
vo.setEmpno(rs.getInt(1));
vo.setEname(rs.getString(2));
vo.setJob(rs.getString(3));
vo.setHiredate(rs.getDate(4));
vo.setSal(rs.getInt(5));
vo.setDname(rs.getString(6));
vo.setLoc(rs.getString(7));
list.add(vo);
}
rs.close();
}catch(Exception ex){
// @After-Throwing => rollback
System.out.println(ex.getMessage());
}
return list; // @After-Returning 정상수행 했을 때
}
// 상세보기
public EmpVO empDetailData(int empno){
EmpVO vo=new EmpVO();
try{
String sql="SELECT empno,ename,job,hiredate,sal,"
+"(SELECT dname FROM dept d WHERE d.deptno=e.deptno) as dname,"
+"(SELECT loc FROM dept d WHERE d.deptno=e.deptno) as loc "
+"FROM emp e "
+"WHERE empno=?";
ps=dbConn.getConn().prepareStatement(sql);
ps.setInt(1, empno);
ResultSet rs=ps.executeQuery();
rs.next();
vo.setEmpno(rs.getInt(1));
vo.setEname(rs.getString(2));
vo.setJob(rs.getString(3));
vo.setHiredate(rs.getDate(4));
vo.setSal(rs.getInt(5));
vo.setDname(rs.getString(6));
vo.setLoc(rs.getString(7));
rs.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return vo;
}
}
|
cs |
DBConnection (메모리할당 안함. 별도로 application-context.xml에서)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.sist.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DBConnection {
private Connection conn;
private PreparedStatement ps;
private String driver, url, username, password;
public DBConnection(String driver, String url, String username, String password){
this.driver=driver;
this.url=url;
this.username=username;
this.password=password;
try{
Class.forName(driver);
}catch(Exception ex){}
}
public void getConnection(){
try{
conn=DriverManager.getConnection(url,username,password);
}catch(Exception ex){}
}
public void disConnection(){
try{
if(ps!=null) ps.close();
if(conn!=null) conn.close();
}catch(Exception ex){}
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public PreparedStatement getPs() {
return ps;
}
public void setPs(PreparedStatement ps) {
this.ps = ps;
}
}
|
cs |
com.sist.aspect
DBAspect
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package com.sist.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/*
* @Aspect => 공통모듈 => DAO에서 공통사용 내용을 모아준다(메모리 할당은 하지 않는다)
* @Component => 메모리 할당
*/
import com.sist.dao.*;
/*
* public List<EmpVO> empListData(){
* try{
* getConnection();
* ------ SQL => 전송 => 결과값 => List, VO
* }catch(Exception e){
* e.printStackTrace();
* }finally{
* disConnection();
* }
* }
*
* ==> AOP는 반복코딩을 없애줌. 소스가 핵심코딩만 사용할 수 있게 자동호출.
*
* 자동 호출
* ======================= Advice
* 1. 언제 ==> PointCut
* 2. 어디서 ==> JoinPoint
* =======================
* Advice => 여러개모이면 => Aspect
*/
@Aspect
@Component
public class DBAspect {
@Autowired
private DBConnection dbConn;
@Before("execution(* com.sist.dao.EmpDAO.emp*(..))")
public void before(){
dbConn.getConnection();
}
@After("execution(* com.sist.dao.EmpDAO.emp*(..))")
public void after(){
dbConn.disConnection();
}
@Around("execution(* com.sist.web.EmpController.*(..))")
public Object around(ProceedingJoinPoint jp) throws Throwable{
Object obj=null;
System.out.println("사용자 호출전:"+jp.getSignature().getName());
obj=jp.proceed(); //메소드 호출
System.out.println("사용자 호출후:"+jp.getSignature().getName());
return obj;
}
@AfterReturning(value="execution(* com.sist.web.EmpController.*(..))",returning="val")
public void afterReturning(JoinPoint jp,Object val){
System.out.println("리턴값:"+val);
}
@AfterThrowing(value="execution(* com.sist.web.EmpController.*(..))",throwing="ex")
public void afterThrowing(Throwable ex){
System.out.println(ex.getMessage());
}
}
|
cs |
list.jsp, detail.jsp 위와 동일
결과도 동일하게 나옴
@Around
1. 자동호출 setAutoCommit(False)
==핵심 코딩 SQL 문장 수행
2. 자동 호출 commit()
@AfterReturning
리턴값
@AfterThrowing
catch절 없을 때 대신
(에러 없으면 출력 X)
R (지능형)
cran.r-project.org/bin/windows/base/
Robo 3T: Simple GUI for beginners
rstudio.com/products/rstudio/download/
'Back-end > Spring' 카테고리의 다른 글
Spring 17일차 - SQL PROCEDURE 게시판 (0) | 2020.11.19 |
---|---|
Spring 16일차 - Transaction 게시판 만들기 / 쿠키 (1) | 2020.11.18 |
Spring 14일차 - Tiles (1) | 2020.11.16 |
Spring 13일차 - Mybatis연동, JSON (0) | 2020.11.13 |
Spring 12일차 - PL/SQL Procedure 게시판 만들기 (0) | 2020.11.12 |