๐Ÿ’ป

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ - [Spring] ์–ด๋…ธํ…Œ์ด์…˜(Annotation) ๋ณธ๋ฌธ

KITRI/SPRING

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ - [Spring] ์–ด๋…ธํ…Œ์ด์…˜(Annotation)

๋˜ํšจ๋‹ˆ 2020. 7. 31. 13:24

Annotation?

์–ด๋…ธํ…Œ์ด์…˜์€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ฒ˜๋ฆฌํ•ด์•ผ๋  ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์ปดํŒŒ์ผ ๊ณผ์ •๊ณผ ์‹คํ–‰๊ณผ์ •์—์„œ ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ปดํŒŒ์ผํ•˜๊ณ  ์ฒ˜๋ฆฌํ• ์ง€๋ฅผ ์•Œ๋ ค์ค€๋‹ค.(์ฐ์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ์ผ์€ ์—†์Œ)

 

1. ์šฉ๋„

1) ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ์ฝ”๋“œ ๋ฌธ๋ฒ• ์—๋Ÿฌ๋ฅผ ์ฒดํฌํ•˜๋„๋ก ์ •๋ณด๋ฅผ ์ œ๊ณต @Overrid

2) ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ์‹œ ๋นŒ๋“œ๋‚˜ ๋ฐฐํƒ€์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ •๋ณด๋ฅผ ์ œ๊ณต (bean, aop๋“ฑ)

3) ์‹คํ–‰์‹œ ํŠน์ • ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋„๋ก ์ •๋ณด๋ฅผ ์ œ๊ณต

โ€‹

2. ์ข…๋ฅ˜

1) ์‹œ์Šคํ…œ ์ •์˜ ์–ด๋…ธํ…Œ์ด์…˜ : @Overrid @Deprecated @SupperssWarning

2) ์ปค์Šคํ…€(๊ฐœ๋ฐœ์ž) ์–ด๋…ธํ…Œ์ด์…˜ :

2-1) ํƒ€์ž…๊ณผ ์ •์˜ - ํƒ€์ž…, ์ด๋ฆ„, ๋””ํดํŠธ ๊ฐ’

2-2) ์ ์šฉ ๋Œ€์ƒ - METHOD, FLED, TYPE(CLASS), ์ƒ์„ฑ์ž, PARAMETER ๋“ฑ์ด ์žˆ๋‹ค.

2-3) ์œ ์ง€ ์ •์ฑ… - SOURCE, CLASS, RUNTIME

 

 

 

 

[Annotation ์ƒ์„ฑ]

โ€‹

โ€‹

MainClass.java

package com.java.anno01;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MainClass {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		Target t= new Target();
		t.sub();
		
		String className = "com.java.anno01.Target";
		Class<?> cls = Class.forName(className);	//ํด๋ž˜์Šค
		Target a = (Target) cls.getDeclaredConstructor().newInstance();	//๋™์ ๊ฐ์ฒด ์ƒ์„ฑ
		a.sub();
		
		
		Method[] methods = cls.getMethods();
		for(int i=0; i<methods.length; i++) {
			if(methods[i].isAnnotationPresent(MyAnno.class)) {
				MyAnno myAnno = methods[i].getAnnotation(MyAnno.class);
				System.out.println(myAnno.value());
			}
		}	
	}
}

 

[Annotation file] MyAnno.java

package com.java.anno01;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnno {	
	//ํƒ€์ž…๊ณผ ์ •์˜
	public String value();
	
}

 

Target.java

package com.java.anno01;

public class Target {
		
	@MyAnno(value="์—ฐ์Šต์ž…๋‹ˆ๋‹ค.")
	public void sub() {
		System.out.println("Target: sub~~~~~~~~~~~~~~~~~");
		
	}
}

[Annotation file] MyAnno.java

package com.java.anno02;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
	public String name() default "ํ™๊ธธ๋™";
	public int age() default 27;
	
}

 

Target.java

package com.java.anno02;

public class Target {
	
	@MyAnno			//ํ™๊ธธ๋™, 27 
	public void subA() {
		System.out.println("Target SubA Function ~~~~~~~~~~~~~~~");
	}
	
	@MyAnno(name="์ด์˜์ž")     //์ด์˜์ž, 27
	public void subB() {
		System.out.println("Target SubB Function ~~~~~~~~~~~~~~~");
	}
	
	@MyAnno(age=33)		//ํ™๊ธธ๋™, 33
	public void subC() {
		System.out.println("Target SubC Function ~~~~~~~~~~~~~~~");
	}
	
	@MyAnno(name="์กฐ์ธ์„ฑ", age=40)
	public void subD() {
		System.out.println("Target SubD Function ~~~~~~~~~~~~~~~");
	}
}

โ€‹

MyAnno.java(Annotation file)

package com.java.anno03;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD,
		ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
	String value() default "Spring";
}

 

Target.java

package com.java.anno03;

@MyAnno()
public class Target {
	private @MyAnno() String str;
	
	@MyAnno("์šฐํ•˜ํ•˜ํ•˜")
	public Target() {}
	
	public void sub(@MyAnno() String value) {
		@MyAnno() int x = 10;
	}
}

 

 

 


๊ฒŒ์‹œํŒ ํ”„๋กœ์ ํŠธ Annotation ์‚ฌ์šฉํ•˜๊ธฐ

 

xmlํŒŒ์ผ์„ annotationํ˜•ํƒœ๋กœ ๊ณ ์ณ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ”๊พผ๋‹ค.

- ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ณ  ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด xml๋กœ ์„ค์ •ํ•˜์˜€์œผ๋‚˜ xml์ด ๋„ˆ๋ฌด ๋งŽ์•„์ง€๋ฉด ์˜คํžˆ๋ ค ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ๋‚ฎ์•„์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒ

- ์œ ์ง€๋ณด์ˆ˜์— ์ค‘์ ์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์‹œ์Šคํ…œ ์ „์ฒด์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์ดํ›„์— ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ฒƒ์€ xml๋กœ ์„ค์ •

โ€‹

โ€‹

 

SpringController-servlet.xmlโ€‹

<aop:aspectj-autoproxy/> ํƒœ๊ทธ๋ฅผ ์„ค์ •ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•˜๋ฉด @Aspect ์–ด๋…ธํ…Œ์ด์…˜์ด ์ ์šฉ๋œ ๋นˆ์„ Aspect๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

โ€‹

context:component-scan

: @Controller, @Service, @Repository, @Component๋“ฑ์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ component-scan์„ xml์— ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•จ.

base-package๋Š” ํŒจํ‚ค์ง€๋ฅผ ์–ด๋””๋ถ€ํ„ฐ ์Šค์บ”ํ• ์ง€ ์ง€์ •ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„. ์Šค์บ”ํ•  ํŒจํ‚ค์ง€๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ๋กœ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

๋””ํดํŠธ ๊ฐ’์œผ๋กœ @Controller, @Service, @Repository, @Component๋กœ ๋“ฑ๋ก๋œ ๋นˆ์„ ์Šค์บ”ํ•ด์ค€๋‹ค.

โ€‹

๊ธฐ์กด XML์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” HandlerMapping์„ ์‚ฌ์šฉํ•˜์˜€์ง€๋งŒ ์ด์ œ๋Š” context:component-scan ๊ฐ€ ๊ทธ ์—ญํ• ์„ ๋Œ€์‹ ํ•œ๋‹ค.

<?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"
	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-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
	
	<!-- Controller, Service, Dao -->
	<context:component-scan base-package="com.java.*"/>
	
	<!-- ViewResolver : MVC RequestDispatcher / forward -->
	<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- AOP -->
	<aop:aspectj-autoproxy/>
	
</beans>

 

mybatis.xml

<?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:tx="http://www.springframework.org/schema/tx"
	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-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

	<!-- Spring DB -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
		<property name="username" value="mvc"/>
		<property name="password" value = "1234"/>
	</bean>

	
	<!-- MyBatis : sqlSessionFactoryBean-->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="driverManagerDataSource"/>
		<property name="mapperLocations">
			<list>
				<value>classpath:com/java/member/dao/mapper/MemberMapper.xml</value>
				<value>classpath:com/java/fileBoard/dao/mapper/FileBoardMapper.xml</value>
			</list>
		</property>
		
		<!-- <property name="mapperLocations" value="classpath:com/java/member/dao/mapper/MemberMapper.xml"/> -->
	</bean>
	
	<!-- MyBatis : sqlsessionTemplate-->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactoryBean"/>
	</bean>
	
	<!-- Transaction ๊ณตํ†ตํด๋ž˜์Šค -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"/>
	</bean>
	
	<!--transactionAOP  -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	
	<!--propagation="REQUIRED" ์ด๋ฏธ ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜์ด ์กด์žฌํ•˜๋ฉด ์ง€์›ํ•˜๊ณ  ์—†๋‹ค๋ฉด ์ƒˆ๋กœ ์ง€์› -->
	
	
	<aop:config>
		<aop:pointcut expression="within(com.java.*)" id="txPublic"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPublic"/>
	</aop:config>
		
</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>05_SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  
  <!-- DispatcherServlet -->
  <servlet>
  	<servlet-name>SpringController</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>
  			/WEB-INF/SpringController-servlet.xml
  			/WEB-INF/mybatis.xml
  		</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>SpringController</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>
  			/WEB-INF/SpringController-servlet.xml
  			/WEB-INF/mybatis.xml
  	</param-value>
  </context-param>
  
  <!-- ํ•œ๊ธ€ ์ž‘์—… -->
  <filter>
  	<filter-name>enCodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>enCodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

 

MemberController.java

@Autowired : ๊ฐ ์ƒํ™ฉ์˜ ํƒ€์ž…์— ๋งž๋Š” IoC์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์กด์žฌํ•˜๋Š” Bean์„ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•ด์คŒ

๊ฐ์ฒด์˜ ์˜์กด์„ฑ์„ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„์— ๊ฐ„๋‹จํžˆ @Autowired์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์˜์กด์„ฑ ์ฃผ์ž…์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋จ.

์ฃผ์ž…ํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฐ์ฒด์˜ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.

โ€‹

์˜์กด๊ฐ์ฒด ์ž๋™ ์ฃผ์ž…?

์Šคํ”„๋ง ์„ค์ • ํŒŒ์ผ์—์„œ ์˜์กด ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•  ๋•Œ <constructor-org> ๋˜๋Š” <property>ํƒœ๊ทธ๋กœ ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ํ•„์š”ํ•œ ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ์ฐพ์•„์„œ ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•œ ๊ฐ์ฒด์— ์ฃผ์ž…ํ•ด ์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๊ตฌํ˜„๋ฐฉ๋ฒ•์€ @Autowired์™€ @Resource ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ๋‹ค.

@Controller : ์ฃผ๋กœ View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ,

ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ Controller์ž„์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜

@RequestMapping : ์š”์ฒญ์— ๋Œ€ํ•ด ์–ด๋–ค Controller, ์–ด๋–ค ๋ฉ”์†Œ๋“œ๊ฐ€ ์ฒ˜๋ฆฌํ• ์ง€๋ฅผ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜, URL์„ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์™€ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง ํ”„๋ ˆ์ž„ ์›Œํฌ์˜ ์–ด๋…ธํ…Œ์ด์…˜

์ฆ‰, ํด๋ผ์ด์–ธํŠธ๋Š” URL๋กœ ์š”์ฒญ์„ ์ „์†กํ•˜๊ณ  ์š”์ฒญ URL์„ ์–ด๋–ค ๋ฉ”์„œ๋“œ๊ฐ€ ์ฒ˜๋ฆฌํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ

@RequestMapping์ด ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ

 

์ด๋ฆ„

ํƒ€์ž…

์„ค๋ช…

value

String[]

URL ๊ฐ’์œผ๋กœ ๋งคํ•‘ ์กฐ๊ฑด์„ ๋ถ€์—ฌ (default)

method

RequetMethod[]

HTTP Request ๋ฉ”์†Œ๋“œ ๊ฐ’์„ ๋งคํ•‘ ์กฐ๊ฑด์œผ๋กœ ๋ถ€์—ฌ

์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉ”์†Œ๋“œ๋Š” GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE (7๊ฐœ)

params

String[]

HTTP Request ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋งคํ•‘ ์กฐ๊ฑด์œผ๋กœ ๋ถ€์—ฌ

consumes

String[]

์„ค์ •๊ณผ Content-Type request ํ—ค๋”๊ฐ€ ์ผ์น˜ํ•  ๊ฒฝ์šฐ์—๋งŒ URL์ด ํ˜ธ์ถœ๋จ

produces

String[]

์„ค์ •๊ณผ Accept request ํ—ค๋”๊ฐ€ ์ผ์น˜ํ•  ๊ฒฝ์šฐ์—๋งŒ URL์ด ํ˜ธ์ถœ๋จ

 

URL

 

https://blog.naver.com/yunnie222/221856149699

 

WEB์„ ์ด๋ฃจ๋Š” ๊ธฐ์ˆ  - URI

♥ URI : ๋ฐ์ดํ„ฐ๋Š” ์–ด๋””์— ์žˆ๋Š”๊ฐ€? (Uniform Resource Identifier)โ€‹- ํ†ตํ•ฉ ์ž์› ์‹๋ณ„์žโ€‹- ์ธํ„ฐ๋„ท์—...

blog.naver.com

package com.java.member.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.InternalResourceView;

import com.java.aop.HAspect;
import com.java.member.dto.MemberDto;
import com.java.member.service.MemberService;

@Controller
public class MemberController{	//Command
	@Autowired
	private MemberService memberService;
	
	@RequestMapping(value="/member/test.do", method=RequestMethod.GET)
	public ModelAndView testing(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("OK");
		
		//Service - Dao / Dto - Service
		//์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋Š” View
		/*
		InternalResourceView view = new InternalResourceView("/WEB-INF/member/test.jsp");
		ModelAndView mav = new ModelAndView();
		mav.addObject("msg", "hi");
		mav.setView(view);
		*/
		
		//๋‹ค์–‘ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๊ธฐ์— ๊ทธ์— ๋งž์ถฐ์„œ ์‚ฌ์šฉ!!!
		//ModelAndView mav = new ModelAndView(view);
		ModelAndView mav = new ModelAndView("member/test");
		mav.addObject("msg", "hi");
		
		return mav;
	}
	
	@RequestMapping(value="/member/register.do", method=RequestMethod.GET)
	public ModelAndView memberRegister(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("member/register");
	}
	
	@RequestMapping(value="/member/registerOk.do", method=RequestMethod.POST)
	public ModelAndView memberRegisterOk(HttpServletRequest request, HttpServletResponse response, MemberDto memberDto) {
		HAspect.logger.info(HAspect.logMsg + memberDto.toString());
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("memberDto", memberDto);
		
		memberService.memberRegisterOk(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/idCheck.do", method=RequestMethod.GET)
	public ModelAndView memberidCheck(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		memberService.memberidCheck(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/zipcode.do", method=RequestMethod.POST)
	public ModelAndView memberZipcode(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		memberService.memberZipcode(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/login.do", method=RequestMethod.GET)
	public ModelAndView memberLogin(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("member/login");
	}
	
	@RequestMapping(value="/member/loginOk.do", method=RequestMethod.POST)
	public ModelAndView memberLoginOk(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.memberloginOk(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/main.do", method=RequestMethod.GET)
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.main(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/logout.do", method=RequestMethod.GET)
	public ModelAndView memberLogout(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.memberloginOut(mav);
		return mav;
	}
	
	@RequestMapping(value="/member/update.do", method=RequestMethod.GET)
	public ModelAndView memberUpdate(HttpServletRequest request, HttpServletResponse response) {
		//select ์ถ”๊ฐ€
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		memberService.memberUpdate(mav);
		
		return mav;
	}
	
	@RequestMapping(value="/member/updateOk.do", method=RequestMethod.POST)
	public ModelAndView memberUpdateOk(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		//memberService.memberUpdate(mav);
		return mav;
	}	
}

 

[Interface] MemberService.java

package com.java.member.service;

import org.springframework.web.servlet.ModelAndView;

public interface MemberService {

	public void memberRegisterOk(ModelAndView mav);
	
	public void memberidCheck(ModelAndView mav);

	public void memberZipcode(ModelAndView mav);

	public void memberlogin(ModelAndView mav);

	public void memberloginOk(ModelAndView mav);

	public void main(ModelAndView mav);

	public void memberloginOut(ModelAndView mav);

	public void memberUpdateOk(ModelAndView mav);

	public void memberUpdate(ModelAndView mav);
}

 

MemberServiceImp.java

@Component : bean์œผ๋กœ ๋“ฑ๋ก๋˜๊ธธ ์›ํ•˜๋Š” ํด๋ž˜์Šค์— ์ถ”๊ฐ€

package com.java.member.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.java.aop.HAspect;
import com.java.member.dao.MemberDao;
import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;

//๊ตฌํ˜„ ํด๋ž˜์Šค

@Component
public class MemberServiceImp implements MemberService {
	@Autowired
	private MemberDao memberDao;

	@Override
	public void memberRegisterOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		MemberDto memberDto = (MemberDto)map.get("memberDto");
		
		memberDto.setMemberLevel("BA");
		
		int check = memberDao.memberinsert(memberDto);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("check", check);
		mav.setViewName("member/registerOk");
		
	}
	
	@Override
	public void memberidCheck(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		HAspect.logger.info(HAspect.logMsg + id);
		
		int check = memberDao.memberidCheck(id);
		HAspect.logger.info(HAspect.logMsg + check);
	
		mav.addObject("check", check);
		mav.addObject("id", id);
		mav.setViewName("member/idCheck");
	}
	
	@Override
	public void memberZipcode(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		
		String dong = request.getParameter("dong");
		
		if(dong!=null) {
			List <ZipcodeDto> zipcodeDto = memberDao.zipcode(dong);
			HAspect.logger.info(HAspect.logMsg + zipcodeDto.size());
			
			mav.addObject("zipcodeList", zipcodeDto);
		}
		mav.setViewName("member/zipcode");
	}
	
	@Override
	public void memberlogin(ModelAndView mav) {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public void memberloginOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("id", id);
		mav.addObject("password", password);
		mav.addObject("memberLevel", "BB");
		mav.setViewName("member/loginOk");
	}
	
	@Override
	public void main(ModelAndView mav) {
		mav.setViewName("member/main");
	}
	
	@Override
	public void memberloginOut(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("id", id);
		mav.addObject("password", password);
		mav.addObject("memberLevel", "BB");
		mav.setViewName("member/logout");
		
	}
	@Override
	public void memberUpdate(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		//int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + id + "@@" + password);
		
		mav.addObject("id", id);
		mav.setViewName("member/update");
		
	}
	
	@Override
	public void memberUpdateOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String id = request.getParameter("id");
		String password=request.getParameter("password");
		int check = memberDao.loginOk(id, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("id", id);
		mav.addObject("password", password);
		mav.addObject("memberLevel", "BB");
		mav.setViewName("member/updateOk");
		
	}
}

 

MemberDao.java

package com.java.member.dao;

import java.util.List;

import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;

public interface MemberDao {
	public int memberinsert(MemberDto memberDto);
	
	public int memberidCheck(String id);

	public List<ZipcodeDto> zipcode(String dong);

	public int loginOk(String id, String password);	
}

 

MemberDaoImp.java

package com.java.member.dao;

import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.java.member.dto.MemberDto;
import com.java.member.dto.ZipcodeDto;

@Component
public class MemberDaoImp implements MemberDao {
	//My batis SqlSessionTemplate
	@Autowired
	private SqlSessionTemplate  sqlSessionTemplate;
	
	@Override
	public int memberinsert(MemberDto memberDto) {
		return sqlSessionTemplate.insert("member_insert", memberDto);
	}
	
	@Override
	public int memberidCheck(String id) {
		String value =  sqlSessionTemplate.selectOne("id_check",id);
		int check = 0;
		if(value!=null) check=1;
		
		
		return check;
	}
	@Override
	public List<ZipcodeDto> zipcode(String dong) {
		return sqlSessionTemplate.selectList("dto.memberDto.member_zipcode", dong);
	}
	
	
	@Override
	public int loginOk(String id, String password) {
		return 0;
	}
}

 

ํŒŒ์ผ ๊ฒŒ์‹œํŒ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ˆ˜์ •

fileBoardController.java

package com.java.fileBoard.controller;

import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.java.fileBoard.dto.FileBoardDto;
import com.java.fileBoard.service.FileBoardService;

@Controller
public class FileBoardController{
	@Autowired
	private FileBoardService fileBoardService;
	
	@RequestMapping(value="/fileBoard/write.do", method=RequestMethod.GET)
	public ModelAndView fileBoardWrite(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request" , request);
		
		fileBoardService.fileBoardWrite(mav);
		return mav;
	}
	
	@RequestMapping(value="/fileBoard/writeOk.do", method=RequestMethod.POST)
	public ModelAndView fileBoardWriteOk(HttpServletRequest request, HttpServletResponse response,FileBoardDto fileBoardDto) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		mav.addObject("fileBoardDto", fileBoardDto);
		
		fileBoardService.fileBoardWriteOk(mav);
		return mav;
	}
	
	@RequestMapping(value="/fileBoard/list.do", method=RequestMethod.GET)
	public ModelAndView fileBoardList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		fileBoardService.fileBoardList(mav);
		return mav;
		
	}
	@RequestMapping(value="/fileBoard/read.do", method=RequestMethod.GET)
	public ModelAndView fileBoardRead(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		fileBoardService.fileBoardRead(mav);
		return mav;
	}
	

	@RequestMapping(value="/fileBoard/downLoad.do", method=RequestMethod.GET)
	public void fileDownLoad(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		mav.addObject("response", response);
		
		fileBoardService.fileDownLoad(mav);
		
	}
	
	@RequestMapping(value="/fileBoard/update.do", method=RequestMethod.GET)
	public ModelAndView fileUpdate(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		fileBoardService.fileBoardUpdate(mav);
		return mav;
	}
	
	@RequestMapping(value="/fileBoard/updateOk.do", method=RequestMethod.POST)
	public ModelAndView fileUpdateOk(HttpServletRequest request, HttpServletResponse response,FileBoardDto fileBoardDto) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		fileBoardService.fileBoardUpdateOk(mav);
		return mav;
	}	
}

 

[Interface] fileBoardService.java

package com.java.fileBoard.service;

import org.springframework.web.servlet.ModelAndView;

public interface FileBoardService {
	public void fileBoardWrite(ModelAndView mav);

	public void fileBoardWriteOk(ModelAndView mav);
	
	public void fileBoardList(ModelAndView mav);
	
	public void fileBoardRead(ModelAndView mav);
	
	public void fileDownLoad(ModelAndView mav);

	public void fileBoardUpdate(ModelAndView mav);

	public void fileBoardUpdateOk(ModelAndView mav);
}

 

fileBoardServiceImp.java

package com.java.fileBoard.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.java.aop.HAspect;
import com.java.fileBoard.dao.FileBoardDao;
import com.java.fileBoard.dto.FileBoardDto;

@Component
public class FileBoardServiceImp implements FileBoardService {
	@Autowired
	private FileBoardDao fileBoardDao;

	@Override
	public void fileBoardWrite(ModelAndView mav) {
		//๋ถ€๋ชจ๊ธ€(ROOT)
		int boardNumber = 0;	//ROOT๊ธ€์ด๋ฉด 0
		int groupNumber = 1;	//๊ทธ๋ฃน๋ฒˆํ˜ธ
		int sequenceNumber = 0;	//๊ธ€์ˆœ์„œ
		int sequenceLevel = 0;	//๊ธ€๋ ˆ๋ฒจ
		
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		//๋‹ต๊ธ€์ธ๊ฒฝ์šฐ ๋ถ€๋ชจ๊ธ€์˜  DB ๊ธ€๋ฒˆํ˜ธ, ๊ทธ๋ฃน๋ฒˆํ˜ธ, ๊ธ€์ˆœ์„œ, ๊ธ€๋ ˆ๋ฒจ
		if(request.getParameter("boardNumber")!=null) {  
		    boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		    groupNumber = Integer.parseInt(request.getParameter("groupNumber"));
		    sequenceNumber = Integer.parseInt(request.getParameter("sequenceNumber"));
		    sequenceLevel = Integer.parseInt(request.getParameter("sequenceLevel"));
		}
		
		
		mav.addObject("boardNumber", boardNumber);
		mav.addObject("groupNumber", groupNumber);
		mav.addObject("sequenceNumber", sequenceNumber);
		mav.addObject("sequenceLevel", sequenceLevel);
		
		mav.setViewName("fileBoard/write");
	}
	
	@Override
	public void fileBoardWriteOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		FileBoardDto fileBoardDto = (FileBoardDto) map.get("fileBoardDto");
		MultipartHttpServletRequest request = (MultipartHttpServletRequest)map.get("request");
		
		
		writeNumber(fileBoardDto);
		fileBoardDto.setWriteDate(new Date());
		fileBoardDto.setReadCount(0);
		
		MultipartFile upFile = request.getFile("file");
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		
		
		if(upFile!=null) {
			//์ €์žฅ๊ฒฝ๋กœ, ํŒŒ์ผ๋ช…, ์‚ฌ์ด์ฆˆ
			String fileName = Long.toBinaryString(System.currentTimeMillis())+ "_" + upFile.getOriginalFilename();
			long fileSize = upFile.getSize();
			
			File path = new File("C:\\pds\\");
			path.mkdir();
			
			if(path.exists() && path.isDirectory()) {
				File file = new File(path, fileName);
				
				try {
					upFile.transferTo(file);
					
					fileBoardDto.setPath(file.getAbsolutePath());
					fileBoardDto.setFileName(fileName);
					fileBoardDto.setFileSize(fileSize);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		HAspect.logger.info(HAspect.logMsg +fileBoardDto);
		
		int check = fileBoardDao.fileBoardWrite(fileBoardDto);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("check", check);
		mav.setViewName("fileBoard/writeOk");
	}
	
	public void writeNumber(FileBoardDto boardDto) {
		
		// ๊ทธ๋ฃน๋ฒˆํ˜ธ(ROOT), ๊ธ€์ˆœ์„œ(์ž์‹), ๊ธ€๋ ˆ๋ฒจ(์ž์‹)
		int boardNumber = boardDto.getBoardNumber(); // 0
		int groupNumber = boardDto.getGroupNumber(); // 1
		int sequenceNumber = boardDto.getSequenceNumber(); // 0
		int sequenceLevel = boardDto.getSequenceLevel(); // 0

	
			if (boardNumber == 0) { // ROOT : ๊ทธ๋ฃน๋ฒˆํ˜ธ
				int max = fileBoardDao.fileBoardGroupNumberMax();

				if (max != 0)
					boardDto.setGroupNumber(max + 1);

			} else { // ๋‹ต๊ธ€ : ๊ธ€ ์ˆœ์„œ, ๊ธ€ ๋ ˆ๋ฒจ
					HashMap<String, Integer> hMap = new HashMap<String, Integer>();
					hMap.put("groupNumber", groupNumber);
					hMap.put("sequenceNumber", sequenceNumber);
					
					int check = fileBoardDao.fileBoardWriteNumber(hMap);
					
					
				  sequenceNumber = sequenceNumber + 1;
				  sequenceLevel = sequenceLevel + 1;
				  
				  boardDto.setSequenceNumber(sequenceNumber);
				  boardDto.setSequenceLevel(sequenceLevel);
		}		
	}
    @Override
	public void fileBoardList(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		String pageNumber = request.getParameter("pageNumber");
		if (pageNumber == null)
		    pageNumber = "1";

		int currentPage = Integer.parseInt(pageNumber); // ์š”์ฒญ 1page
		HAspect.logger.info(HAspect.logMsg +"currentPage" + currentPage);

		int boardSize = 5; // view์— ๋ช‡๊ฐœ์˜ ๊ธ€์„ ๋ณด์—ฌ์ค„์ง€ 
		int startRow = (currentPage - 1) * boardSize + 1; // ์‹œ์ž‘๋ฒˆํ˜ธ 1 11
		int endRow = currentPage * boardSize; // ๋๋ฒˆํ˜ธ 10 20

		int count = fileBoardDao.fileBoardCount();
		HAspect.logger.info(HAspect.logMsg + count);

		List<FileBoardDto> boardList = null;
		if (count > 0) {
		    // startRow, endRow
		    boardList = fileBoardDao.fileBoardList(startRow, endRow);
		    HAspect.logger.info(HAspect.logMsg + boardList.size());
		}
		
		mav.addObject("boardList", boardList);
		mav.addObject("boardSize", boardSize);
		mav.addObject("currentPage", currentPage);
		mav.addObject("count", count);
		mav.setViewName("fileBoard/list");
		
	}
	
	@Override
	public void fileBoardRead(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		HAspect.logger.info(HAspect.logMsg + boardNumber + "," + pageNumber);
		  
		FileBoardDto fileBoardDto = fileBoardDao.fileBoardRead(boardNumber);
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		
		if(fileBoardDto.getFileName() !=null) {
			int index=fileBoardDto.getFileName().indexOf("_")+1;
			fileBoardDto.setFileName(fileBoardDto.getFileName().substring(index));
		}
		
		mav.addObject("boardDto", fileBoardDto);
		mav.addObject("pageNumber", pageNumber);
		
		mav.setViewName("fileBoard/read");
	}
	
	@Override
	public void fileDownLoad(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		HAspect.logger.info(HAspect.logMsg + boardNumber);
		
		
		FileBoardDto fileBoardDto = fileBoardDao.filBoardSelect(boardNumber);
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		


			   int index = fileBoardDto.getFileName().indexOf("_") + 1;
			   String fName = fileBoardDto.getFileName().substring(index);
			   String fileName;
			try {
				fileName = new String(fName.getBytes("utf-8"), "ISO-8859-1");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			   long fileSize = fileBoardDto.getFileSize();
			   String path = fileBoardDto.getPath();
			   
			   // ์„ธํŒ…์ด ํ•„์š”   
			   response.setHeader("Content-Disposition", "attachment;filename=" + fName);
			   response.setContentType("application/octet-stream");
			   response.setContentLengthLong(fileSize);
			   
			   BufferedInputStream bis = null;
			   BufferedOutputStream bos = null;
			   
			   try {
			       bis = new BufferedInputStream(new FileInputStream(path), 1024);
			       bos = new BufferedOutputStream(response.getOutputStream(), 1024);
			       
			       while(true) {
			    	   	int data = bis.read();
			    	   	if(data == -1) break;
			    	   	bos.write(data);
			       }
			       bos.flush();
			       
			   } catch (Exception e) {
			       e.printStackTrace();
			   } finally {
				   try {
			       if(bis!=null) bis.close();
			       if(bos!=null) bos.close();
				   }catch(Exception e) {
					   e.printStackTrace();
			   }	
		 }
	}
    @Override
	public void fileBoardUpdate(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		
		if(request.getParameter("boardNumber")!=null) {
			boardNumber=Integer.parseInt(request.getParameter("boardNumber"));
			pageNumber=Integer.parseInt(request.getParameter("pageNumber"));
		}
		
		FileBoardDto fileBoardDto = fileBoardDao.fileBoardUpdate(boardNumber);
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		
				
		mav.addObject("boardNumber", boardNumber);
		mav.addObject("pageNumber", pageNumber);
		mav.addObject("boardDto", fileBoardDto);
		HAspect.logger.info(HAspect.logMsg + "boardNumber" + pageNumber);
		mav.setViewName("fileBoard/update");
		
	}
	
	@Override
	public void fileBoardUpdateOk(ModelAndView mav) {
		System.out.println("dddddd");
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		FileBoardDto fileBoardDto = fileBoardDao.filBoardSelect(boardNumber);
		
		fileBoardDto.setWriter(request.getParameter("name"));
		fileBoardDto.setSubject(request.getParameter("subject"));
		fileBoardDto.setEmail(request.getParameter("email"));
		fileBoardDto.setContent(request.getParameter("content"));
		fileBoardDto.setPassword(request.getParameter("password"));
		fileBoardDto.setWriteDate(new Date()); //์‹œ๊ฐ„
		
		int check = fileBoardDao.fileBoardupdateOk(boardNumber, fileBoardDto);
		
		HashMap<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("boardNumber", boardNumber);
		hMap.put("fileBoardDto", fileBoardDto);
		
		mav.addObject("check", check);
		mav.addObject("pageNumber", pageNumber);
		mav.setViewName("fileBoard/updateOk");
	
	    }
	}

 

fileBoardDao.java

package com.java.fileBoard.dao;

import java.util.HashMap;
import java.util.List;

import com.java.fileBoard.dto.FileBoardDto;

public interface FileBoardDao {
	public int fileBoardGroupNumberMax();
	
	public int fileBoardWriteNumber(HashMap<String, Integer> hMap);
	
	public int fileBoardWrite(FileBoardDto fileBoardDto);
	
	public int fileBoardCount();
	
	public List<FileBoardDto> fileBoardList(int startRow, int endRow);
	
	public FileBoardDto fileBoardRead(int boardNumber);
	
	public FileBoardDto filBoardSelect(int boardNumber);
	

	public FileBoardDto fileBoardUpdate(int boardNumber);
	
	public int fileBoardupdateOk(int boardNumber, FileBoardDto fileBoardDto);
		

}

 

fileBoardDaoImp.java

package com.java.fileBoard.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.java.fileBoard.dto.FileBoardDto;

@Component
public class FileBoardDaoImp implements FileBoardDao {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	
	@Override
	public int fileBoardGroupNumberMax() {
		return sqlSessionTemplate.selectOne("board_group_number_max");
		
	}
	
	@Override
	public int fileBoardWriteNumber(HashMap<String, Integer> hMap) {
		
		return sqlSessionTemplate.update("board_writer_number", hMap);
	}
	
	@Override
	public int fileBoardWrite(FileBoardDto fileBoardDto) {
		return sqlSessionTemplate.insert("board_insert",fileBoardDto);
	}
	
	@Override
	public int fileBoardCount() {
		return sqlSessionTemplate.selectOne("board_count");
	}
	
	@Override
	public List<FileBoardDto> fileBoardList(int startRow, int endRow) {
		Map<String, Integer> hMap = new HashMap<String, Integer>();
		hMap.put("startRow", startRow);
		hMap.put("endRow", endRow);
		
		return sqlSessionTemplate.selectList("board_list", hMap);
	}
	
	@Override
	public FileBoardDto fileBoardRead(int boardNumber) {
		sqlSessionTemplate.update("board_read_count", boardNumber);
		return sqlSessionTemplate.selectOne("board_read", boardNumber);
		
	}
	
	@Override
	public FileBoardDto filBoardSelect(int boardNumber) {
		return sqlSessionTemplate.selectOne("board_read", boardNumber);
		
	}
	
	@Override
	public FileBoardDto fileBoardUpdate(int boardNumber) {
		return sqlSessionTemplate.selectOne("board_read", boardNumber);
	}
	

	@Override
	public int fileBoardupdateOk(int boardNumber, FileBoardDto fileBoardDto) {
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("boardNumber", boardNumber);
		hMap.put("boardDto", fileBoardDto);
		
		//sqlSessionTemplate.update("board_read_count", boardNumber);
		return sqlSessionTemplate.update("board_update", hMap);
	}
}
๋ฐ˜์‘ํ˜•
Comments