[springMVC + Ajax] 게시판 첨부파일 추가

테이블 추가


▶attach 테이블

create table attach(
uuid varchar(100) primary key,
uploadPath varchar(200) not null,
fileName varchar(100) not null,
fileType char(3) default 'I',
board_id bigint
);

ALTER TABLE attach ADD FOREIGN KEY(board_id) REFERENCES board(id);
  • uuid: UUID가 포함된 파일 이름
  • uploadPath: 실제 파일이 업로드된 경로
  • fileName: 파일 이름
  • fileType: 파일 종류, 이미지 파일 여부 확인
  • board_id: 해당 게시물 번호 저장(board 테이블, 외래키 역할)

 

 

 

 

 

 

 

 

 

 

 

 

DTO


▶BoardAttach.java

package com.spring.boardapp.domain;

public class BoardAttach {
	private String uuid;
	private String uploadPath;
	private String fileName;
	private boolean fileType;

	private String bno;

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getUploadPath() {
		return uploadPath;
	}

	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public boolean isFileType() {
		return fileType;
	}

	public void setFileType(boolean fileType) {
		this.fileType = fileType;
	}

	public String getBno() {
		return bno;
	}

	public void setBno(String bno) {
		this.bno = bno;
	}

}

 

 

▶Board.java 수정

게시글에 포함된 여러 개의 첨부파일을 다루기 위해서 Board.java에 List<BoardAttac>를 추가해준다.
package com.spring.boardapp.domain;

import java.util.List;

public class Board {
	private String id;
	private String title;
	private String content;
	private String writer;
	private String views;
	private String regist_datetime;
	private String modify_datetime;
	private int reply_cnt;
	
	private List<BoardAttach> attachList;
	
    
	...
	
    
	public List<BoardAttach> getAttachList() {
		return attachList;
	}
	public void setAttachList(List<BoardAttach> attachList) {
		this.attachList = attachList;
	}
}

 

 

 

 

 

 

 

 

 

DAO


▶인터페이스

package com.spring.boardapp.dao;

import java.util.List;

public interface BoardAttachDao {
	public void insert(BoardAttach boardAttach);
	
	public void delete(String uuid);
	
	public List<BoardAttach> findBoardAttach(String board_id);
}

특정 게시물의 번호로 첨부파일들을 찾는다.

 

 

▶BoardAttachDaoImpl

package com.spring.boardapp.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;

public class BoardAttachDaoImpl implements BoardAttachDao {
	
	@Autowired
	private SqlSession sqlSession;
	
	@Override
	public void insert(BoardAttachDao boardAttach) {
		// TODO Auto-generated method stub
		sqlSession.insert("insert", boardAttach);
	}
	@Override
	public void delete(String uuid) {
		sqlSession.delete("uuid", uuid);
		
	}
	@Override
	public List<BoardAttachDao> findBoardAttach(String board_id) {
		// TODO Auto-generated method stub
		return sqlSession.selectList("findBoardAttach", board_id);
	}
}

 

 

 

 

 

 

 

 

 

 

Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.spring.boardapp.dao.BoardAttachDao">
	<insert id="insert">
		INSERT INTO attach (uuid, 
							uploadpath, 
							filename, 
							filetype, 
							board_id)
		VALUES (#{uuid},
				#{uploadpath},
				#{filename},
				#{filetype},
				#{board_id})
	</insert>
	<delete id="delete">
		DELETE FROM attach WHERE uuid = #{uuid}
	</delete>
	<select id="findBoardAttach">
		SELECT *
		FROM attach
		WHERE board_id = #{board_id}
	</select>
</mapper>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View


첨부파일 처리는 Ajax로 이루어진다.
첨부파일이 생성되서 등록되는 것도 게시물이 등록될 때 같이 되는 것이므로 기존 Board의 등록 뷰(boardRegister)에 작성되어야 한다.

 

▶boardRegister.jsp

    ...
    
    		<input type="reset" value="초기화" />
		<input type="submit" value="등록" /></td>
		</tr>
	</table>
	</form>
    
    
    <!-- 첨부 파일 등록 추가 -->
    <div>File Attach</div>
    	<div>
        	<div class="uploadDiv">
            	<input type="file" name='uploadFile' multiple>
        	</div>
        
        	<div class='uploadResult'> 
          	<ul>
          
          	</ul>
    	</div>
	</div>