웹개발 교육/Spring

[74~5일] Spring (19) - MyBatis 프로젝트 (쓰기)

2022. 11. 11. 11:33
목차
  1. product 쓰기

product 쓰기

 

ProductCont.java

	@RequestMapping("/write")
	public String write() {
		return "product/write";
	}//write() end

 

 

write.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>write.jsp</title>
	<link href="../css/main.css" rel="stylesheet" type="text/css">
</head>
<body>
	<h3>상품목록</h3>
	<p>
		<button type="button" onclick="location.href='list'">상품전체목록</button>
	</p>
	
	<form name="form1" method="post" action="insert" enctype="multipart/form-data">
	<table border="1">
	<tr>
		<td>상품명</td>
		<td><input type="text" name="product_name"></td>
	</tr>
	<tr>
		<td>상품가격</td>
		<td><input type="number" name="price"></td>
	</tr>
	<tr>
		<td>상품설명</td>
		<td>
			<textarea rows="5" cols="60" name="description"></textarea>
		</td>
	</tr>
	<tr>
		<td>상품사진</td>
		<td><input type="file" name="img"></td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type="submit" value="상품등록">
		</td>
	</tr>
			
	</table>
	</form>
</body>
</html>

 

 

ProductCont.java

	@RequestMapping("/insert")
	public String insert(@RequestParam Map<String, Object> map
					   , @RequestParam MultipartFile img
					   , HttpServletRequest req) {
		
		//주의사항 : 파일 업로드 할 때 리네임 되지 않음
		
		//업로드된 파일을 /storage 폴더에 저장		
		String filename = "-";
		long filesize = 0;        //테이블에 filesize칼럼 추가하면 된다.
		if(img != null && !img.isEmpty()) {
			filename = img.getOriginalFilename();
			filesize = img.getSize();
			try {
				
				ServletContext application = req.getSession().getServletContext();
				String path = application.getRealPath("/storage");
				//System.out.println(path);
				//I:\java202207\workspace_spring\spring07_myshop\src\main\webapp\storage
				img.transferTo(new File(path+"\\"+filename));
				
			} catch (Exception e) {
				e.printStackTrace(); //System.out.println(e);
			}//try end
		}//if end
		
		map.put("filename", filename);
		map.put("filesize", filesize);
		productDao.insert(map);
		return "redirect:/product/list";
	}//insert() end

melon 프로젝트에서는 DTO에 담았다

 

 

ProductDAO.java

	public void insert(Map<String, Object> map) {
		sqlSession.insert("product.insert", map);
	}//insert() end

 

 

product.xml

	<insert id="insert" parameterType="java.util.Map">
		INSERT INTO product(product_code, product_name, description, price, filename, filesize)
		VALUES (product_seq.nextval, #{product_name}, #{description}, #{price}, #{filename}, #{filesize})
	</insert>

Map을 사용했기 때문에 key값을 통해 바인딩을 한다.

 

'웹개발 교육 > Spring' 카테고리의 다른 글

[75일] Spring (21) - MyBatis 프로젝트 (상세보기)  (0) 2022.11.11
[75일] Spring (20) - MyBatis 프로젝트 (검색)  (0) 2022.11.11
[74일] Spring (18) - MyBatis 프로젝트 (설정, 첫페이지, 목록)  (0) 2022.11.10
[74일] Spring (17) - Ajax 검색  (0) 2022.11.10
[73일] Spring (16) - Ajax 이미지 출력  (0) 2022.11.09
  1. product 쓰기
'웹개발 교육/Spring' 카테고리의 다른 글
  • [75일] Spring (21) - MyBatis 프로젝트 (상세보기)
  • [75일] Spring (20) - MyBatis 프로젝트 (검색)
  • [74일] Spring (18) - MyBatis 프로젝트 (설정, 첫페이지, 목록)
  • [74일] Spring (17) - Ajax 검색
ewok
ewok
ewok
기록장
ewok
전체
오늘
어제
  • 분류 전체보기
    • 웹개발 교육
      • HTML
      • CSS
      • JavaScript
      • Database
      • Java
      • jQuery
      • Ajax
      • Bootstrap
      • jsp
      • Spring
      • MyBatis
      • 프로젝트
    • JAVA
    • SpringBoot
      • 기초
      • AWS
      • 개인프로젝트
    • Spring Security
    • JPA
    • 테스트코드
    • Error
    • CS
      • 컴퓨터 구조
      • 이산수학
    • 알고리즘
      • 정리
      • Java
    • SQL
    • 자격증
      • SQLD
      • 정보처리기사
    • Git

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • org.springframework.beans.factory.UnsatisfiedDependencyException
  • SQLD
  • branch
  • base
  • GIT
  • 생성자
  • org.hibernate.tool.schema.spi.CommandAcceptanceException
  • 브랜치
  • 버전 관리
  • sqld 합격
  • merge commit
  • sqld 자격증
  • 노랭이
  • git bash
  • this

최근 댓글

최근 글

hELLO · Designed By 정상우.
ewok
[74~5일] Spring (19) - MyBatis 프로젝트 (쓰기)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.