파일명 dto/ ItemDTO
package com.example.dto;
import java.util.Date;
import lombok.Data;
@Data
public class ItemDTO {
// 물품코드
private Long icode;
// 물품이름
private String iname;
// 물품내용
private String icontent;
// 무품가격
private Long iprice;
// 재고수량
private Long iquantity;
// 등록일
private Date iregdate;
// 이미지
private byte[] iimage;
// 이미지크기
private Long iimagesize;
// 이미지타입
private String iimagetype;
// 이미지명
private String iimagename;
// 이메일
private String uemail;
}
파일명 itemService.java
package com.example.service;
import java.util.List;
import java.util.Map;
import com.example.dto.ItemDTO;
import org.springframework.stereotype.Service;
@Service
public interface ItemService {
// 물품등록
public int insertItemOne(ItemDTO item);
// 물품조회(검색어 + 페이지네이션)
public List<ItemDTO> selectItemList(Map<String, Object> map);
// 페이지네이션용
public long selectItemCount(String txt);
}
파일명 itemServiceImpl.java
package com.example.service;
import java.util.List;
import java.util.Map;
import com.example.dto.ItemDTO;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
SqlSessionFactory sqlFactory;
@Override
public int insertItemOne(ItemDTO item) {
return sqlFactory.openSession().insert("Item.insertItemOne", item);
}
@Override
public List<ItemDTO> selectItemList(Map<String, Object> map) {
return sqlFactory.openSession().selectList("Item.selectItemList", map);
}
@Override
public long selectItemCount(String txt) {
return sqlFactory.openSession().selectOne("Item.selectItemCount", txt);
}
}
파일명 mapper/ itemMapper.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="Item">
<insert id="insertItemOne" parameterType="com.example.dto.ItemDTO">
INSERT INTO ITEM( ICODE, INAME, ICONTENT, IPRICE, IQUANTITY,
IIMAGE, IIMAGESIZE, IIMAGETYPE, IIMAGENAME, UEMAIL )
VALUES ( SEQ_ITEM_ICODE.NEXTVAL, #{iname}, #{icontent}, #{iprice}, #{iquantity},
#{iimage, jdbcType=BLOB}, #{iimagesize}, #{iimagetype}, #{iimagename}, #{uemail} )
</insert>
<select id="selectItemList" parameterType="map" resultType="com.example.dto.ItemDTO">
SELECT * FROM (
SELECT I.ICODE, I.INAME, I.IPRICE, I.IQUANTITY, I.IREGDATE,
ROW_NUMBER() OVER (ORDER BY I.ICODE DESC) ROWN
FROM ITEM I
WHERE I.INAME LIKE '%' || #{txt} || '%'
)
WHERE ROWN BETWEEN #{start} AND #{end}
</select>
<select id="selectItemCount" parameterType="String" resultType="long">
SELECT
COUNT(*) CNT
FROM
ITEM I
WHERE
I.INAME LIKE '%' || #{txt} || '%'
</select>
</mapper>
파일명 itemController.java
package com.example.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import com.example.dto.ItemDTO;
import com.example.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping(value="/item")
public class ItemController {
@Autowired
ItemService iService;
// 127.0.0.1:9090/ROOT/item/selectlist?txt=검색어&page=1
@GetMapping(value = "/selectlist")
public String selectlistGET(
Model model,
@RequestParam(name = "txt", defaultValue = "") String txt,
@RequestParam(name = "page", defaultValue = "1") int page
){
Map<String, Object> map = new HashMap<>();
map.put("txt", txt);
map.put("start", page * 10 -9);
map.put("end", page * 10);
// page 1, start 1 end 10
// page 2, start 11 end 20
// page 3, start 21 end 30
List<ItemDTO> list = iService.selectItemList(map);
model.addAttribute("list", list);
long cnt = iService.selectItemCount(txt);
// 9 => 1
// 11 => 2
// 24 => 3
model.addAttribute("pages", (cnt-1)/10+1);
// System.out.println(list);
return "/item/selectlist";
}
// 127.0.0.1:9090/ROOT/item/insert
@GetMapping(value="/insert")
public String insertGET(){
// templates폴더 item폴더 insert.html 표시(렌더링)
return "/item/insert";
}
@PostMapping(value = "/insert")
public String insertPOST(
HttpSession httpSession,
@ModelAttribute ItemDTO item,
@RequestParam(name = "timage") MultipartFile file) throws IOException {
// System.out.println("==================================");
// System.out.println(item.toString());
// System.out.println(file.getOriginalFilename());
// System.out.println("==================================");
// 파일관련내용
item.setIimagetype(file.getContentType());
item.setIimagename(file.getOriginalFilename());
item.setIimagesize(file.getSize());
item.setIimage(file.getBytes());
// 세션에서 이메일 꺼내기
String em = (String)httpSession.getAttribute("SESSION_EMAIL");
item.setUemail(em);
int ret = iService.insertItemOne(item);
// System.out.println(ret);
// System.out.println("==================================");
if(ret == 1) {
return "redirect:/item/selectlist";
}
return "redirect:/item/insert";
}
}
파일명 item/ insert.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>물품등록</title>
</head>
<body style="padding: 10px;">
<h3>물품 등록 페이지 입니다.</h3>
<hr />
<div style="padding: 20px;">
<form th:action="@{/item/insert}" method="post" enctype="multipart/form-data">
<label style="width:75px; height: 30px; display:inline-block;">물품명 : </label>
<input type="text" placeholder="물품명" name="iname"/></br>
<label style="width:75px; height: 30px; display:inline-block;">물품내용 : </label>
<textarea cols="30" rows="10" placeholder="물품내용" name="icontent"></textarea><br />
<label style="width:75px; height: 30px; display:inline-block;">물품가격 : </label>
<input type="text" placeholder="물품가격" name="iprice"/></br>
<label style="width:75px; height: 30px; display:inline-block;">물품수량 : </label>
<input type="text" placeholder="물품수량" name="iquantity"/></br>
<label style="width:75px; height: 30px; display:inline-block;">이미지 : </label>
<input type="file" name="timage"></br>
<label style="width:75px; height: 30px; display:inline-block;"></label>
<input type="submit" value="물품등록" />
</form>
</div>
</body>
</html>
파일명 item/ selectlist.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>물품목록</title>
</head>
<body style="padding: 10px;">
<h3>물품목록</h3>
<hr />
<div style="padding:20px">
<a th:href="@{/item/insert}">물품등록</a>
<form th:action="@{/item/selectlist}" method="get">
<input type="text" name="txt" placeholder="검색어" />
<input type="submit" value="검색" />
</form>
<table border="1">
<tr>
<th>번호</th>
<th>물품코드</th>
<th>물품명</th>
<th>가격</th>
<th>수량</th>
<th>등록일</th>
</tr>
<tr th:each="tmp, idx : ${list}">
<td th:text="${idx.count}"></td>
<td>
<a th:href="@{/item/selectone(code=${tmp.icode})}"
th:text="${tmp.icode}"></a>
</td>
<td th:text="${tmp.iname}"></td>
<td th:text="${tmp.iprice}"></td>
<td th:text="${tmp.iquantity}"></td>
<td th:text="${tmp.iregdate}"></td>
</tr>
</table>
<th:block th:each="i : ${#numbers.sequence(1, pages)}">
<a th:href="@{/item/selectlist(page=${i}, txt=${param.txt})}" th:text="${i}"></a>
</th:block>
</div>
</body>
</html>