VO
GalleryVo
public class GalleryVo {
private int no;
private String title;
private String content;
private String fName;
private long fSize;
private String fType;
private Date enrollDate;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public long getfSize() {
return fSize;
}
public void setfSize(long fSize) {
this.fSize = fSize;
}
public String getfType() {
return fType;
}
public void setfType(String fType) {
this.fType = fType;
}
public Date getEnrollDate() {
return enrollDate;
}
public void setEnrollDate(Date enrollDate) {
this.enrollDate = enrollDate;
}
@Override
public String toString() {
return "GalleryVo [no=" + no + ", title=" + title + ", content=" + content + ", fName=" + fName + ", fSize="
+ fSize + ", fType=" + fType + ", enrollDate=" + enrollDate + "]";
}
}
mybatis
gallery-mapper.xml
<mapper namespace="gallery">
<select id="getSeq" resultType="int">
SELECT GALLERY_SEQ.NEXTVAL FROM DUAL
</select>
<insert id="upload" >
INSERT INTO GALLERY
VALUES(#{no}, #{title}, #{content}, #{fName}, #{fSize}, #{fType}, SYSDATE)
</insert>
</mapper>
Controller, Repository, Service
GalleryRepository
public interface GalleryRepository {
int getSeq();
int upload(GalleryVo g);
}
GalleryRepositoryImpl
@Repository
public class GalleryRepositoryImpl implements GalleryRepository {
@Autowired
SqlSession sqlSession;
@Override
public int getSeq() {
return sqlSession.selectOne("gallery.getSeq");
}
@Override
public int upload(GalleryVo g) {
return sqlSession.insert("gallery.upload", g);
}
}
GalleryService
public interface GalleryService {
int upload(GalleryVo g, MultipartFile f) throws IllegalStateException, IOException;
}
GalleryServiceImpl
@Service
public class GalleryServiceImpl implements GalleryService {
@Autowired
private GalleryRepository galleryRepository;
@Override
public int upload(GalleryVo g, MultipartFile f) throws IllegalStateException, IOException {
g.setfName(f.getOriginalFilename());
g.setfSize(f.getSize());
g.setfType(f.getContentType());
int no = galleryRepository.getSeq();
g.setNo(no);
System.out.println(g);
int result = galleryRepository.upload(g);
File file = new File("D:\\uploadForSpring", String.valueOf(no));
f.transferTo(file);
return result;
}
}
GalleryController
@Controller
@RequestMapping("gallery")
public class GalleryController {
@Autowired
private SqlSession sqlSession;
@Autowired
private GalleryService galleryService;
@GetMapping("upload")
public String upload() {
return "gallery/upload";
}
@PostMapping("upload")
public String upload(GalleryVo g, MultipartFile f) throws IllegalStateException, IOException {
if(f.isEmpty()) {
return "redirect:/error404";
}
int result = galleryService.upload(g, f);
if(result > 0) {
return "redirect:/gallery/upload";
}else {
return "redirect:/error404";
}
}
}
뷰
upload.jsp
<body>
<h1>갤러리 업로드 페이지</h1>
<form action="" method="post" enctype="multipart/form-data">
제목 : <input type="text" name="title"><br>
내용 : <input type="text" name="content"><br>
파일 : <input type="file" name="f" multiple accept=".jpg, .png"><br>
<input type="submit" value="등록">
</form>
</body>