자바 스프링 환경설정

WRITTEN BY

baealex

소비적인 일보단 생산적인 일을 추구하며, 좋아하는 일을 잘하고 싶어합니다 :D
Sign in to view email

환경설정

  1. 스프링 레거시 프로젝트 생성
  2. MVC 프로젝트로 만들기(spring.sts.CLASS)
  3. web.xml에서 필터 등록
<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>

<servlet>
    <servlet-name>filedownload</servlet-name>
    <servlet-class>spring.utility.webtest.FileDownload</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>filedownload</servlet-name>
    <url-pattern>/download</url-pattern>
</servlet-mapping>
  1. pom.xml에서 버전 변경
  • java version 1.8
  • 프레임워크 버전 4.3.20
  1. pom.xml에서 마이바티스 파일업로드 타일즈 등록
<!--MyBatis setting -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.2</version>
</dependency>

 <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.0</version>
 </dependency>
 <!-- DBCP setting -->
 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<!-- Tiles2 -->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-tiles2</artifactId>
    <version>2.0.1</version>
</dependency>
<!-- fileupload setting -->                
<dependency> 
    <groupId>commons-fileupload</groupId> 
    <artifactId>commons-fileupload</artifactId> 
    <version>1.2.1</version> 
</dependency> 
<dependency> 
    <groupId>commons-io</groupId> 
    <artifactId>commons-io</artifactId> 
    <version>2.4</version> 
</dependency>
  1. spring/root-context.xml
  • 네임 스페이스에서 mybatis, p 등록
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
</bean> 

<bean id="tilesconfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
    <property name="definitions"> 
        <list> 
               <value>/WEB-INF/spring/img.xml</value>
           </list> 
       </property> 
   </bean>  

   <bean id="viewResolver"  
       class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
       <property name="order"> 
           <value>1</value> 
       </property> 
       <property name="viewClass"> 
           <value>org.springframework.web.servlet.view.tiles2.TilesView</value> 
       </property> 
   </bean>

<!-- 데이터베이스 접속 설정 -->

   <bean id="dataSource" 
    destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource" 
    p:driverClassName="oracle.jdbc.driver.OracleDriver" 
    p:url="jdbc:oracle:thin:@localhost:1521:XE" 
    p:username="soldesk"  
    p:password="1234">
</bean> 

<!-- MyBatis SQL 실행 클래스  --> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <!-- 아래부분은 매핑할 xml파일이 있는 패키지경로를 설정한다. -->

    <property name="typeAliasesPackage" value="spring.model" />
    <property name="mapperLocations" value="classpath:mybatis/*.xml" />
  </bean>

  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
  </bean>   

  <!-- Mapper -->
  <mybatis-spring:scan base-package="spring.model.mapper"/>
  1. spring/appServlet/servelt-context.xml
  • resources mapping 삭제
  • context:component-scan 만들어서 spring.model.{CLASS} 넣기
  1. java/spring.model.{CLASS} 생성
  • {CLASS}DTO.java 생성
  1. java/spring.utility.{CLASS} 생성
  2. java/spring.model.mapper 생성
  3. java/spring.sts.{CLASS}/{CLASS}Controller 생성
  4. resource/mybatis/{CLASS}.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="spring.model.mapper.{CLASS}">
    <select id="{Method Name}" parameterType="int" resultType="Map">
        QUERY
    </select>
</mapper>
FileDownload.java
Utility.java
OJDBC.jar


이미지

  • 업로드
String basePath = request.getRealPath("/storage");

dto.setFilesize((int)dto.getFilenameMF().getSize());
dto.setIp(request.getRemoteAddr());

if(dto.getFilesize() > 0) {
    dto.setFilename(Utility.saveFileSpring(dto.getFilenameMF(), basePath));
}
  • 변경
String basePath = request.getRealPath("/storage");

int pflag = mapper.passwdcheck(map);

dto.setFilesize((int)dto.getFilenameMF().getSize());

if(pflag == 1) {
    if(dto.getFilesize() > 0) {
        Utility.deleteFile(basePath, oldfile);
        dto.setFilename(Utility.saveFileSpring(dto.getFilenameMF(), basePath));
    }

    flag = mapper.update(dto);
}
  • 삭제
String basePath = request.getRealPath("/storage");

if(flag == 1) {
    Utility.deleteFile(basePath, oldfile);
}

작성된 댓글이 없습니다!

로그인된 사용자만 댓글을 작성할 수 있습니다.

이 작성자가 게시한 다른 글