java

์Šคํ”„๋ง - ์ƒํ’ˆ ์กฐํšŒ ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ(2)

dev_summer 2021. 2. 27. 14:10

 

 

(1)์—์„œ๋Š” DB๋“ฑ์„ ์—ฐ๊ฒฐํ•˜๊ณ  seleteํ•˜์—ฌ jspํŽ˜์ด์ง€์— ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฟŒ๋ ค๋ณด์•˜๋‹ค.

 

์•„๋ž˜๋Š” ์ดˆ๊ธฐํ™”๋ฉด์ด๋‹ค.

 

์ดˆ๊ธฐํ™”๋ฉด์€ ์ „์ฒด๋ณด๊ธฐํ™”๋ฉด์ด๋‹ค.

๋ฒ ์ด์ปค๋ฆฌ, ์ปคํ”ผ๋ฅผ ํด๋ฆญํ•˜๋ฉด ํŽ˜์ด์ง€๋„˜๊น€ ์—†์ด ajax๋กœ type์ด ๋ฒ ์ด์ปค๋ฆฌ, ์ปคํ”ผ์ธ ๊ฒƒ์„ ์ถœ๋ ฅ ํ•  ๊ฒƒ์ด๊ณ 

๊ฐ€๊ฒฉ์ˆœ, ์ฃผ๋ฌธ์ˆœ ๋ฒ„ํŠธ๋Šฅ ํด๋ฆญํ•˜๋ฉด ์ง€๊ธˆ ํ˜„์žฌ ํŽ˜์ด์ง€์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ์—์„œ ๊ฐ€๊ฒฉ์ˆœ, ์ฃผ๋ฌธ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•  ๊ฒƒ์ด๋‹ค.

 

 

 

-product.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>seoha_procudt</title>
</head>
<body>
	<button id="product">์ „์ฒด๋ณด๊ธฐ</button>
	<button id="bakery">๋ฒ ์ด์ปค๋ฆฌ</button>
	<button id="coffee">์ปคํ”ผ</button>
	<button id="price_desc">๊ฐ€๊ฒฉ์ˆœ</button>
	<button id="order_desc">์ฃผ๋ฌธ์ˆœ</button>
	<table border="1">
		<tr id="title">
			<th>id</th>
			<th>name</th>
			<th>price</th>
			<th>orderCount</th>
			<th>type</th>
		</tr>
		
		<c:forEach var="product" items="${products}">

			<tr class="item">
				<td>${product.id}</td>
				<td>${product.name}</td>
				<td>${product.price}</td>
				<td>${product.orderCount}</td>
				<td>${product.type}</td>

			</tr>
		</c:forEach>

	</table>


	<script
		src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
	<script>

		let type="";

		
 		$('#product').on('click', function() {
			$.ajax({
				type : 'GET',
				url : '/product',
				data : 'json'
			}).done(function(result){
				console.log(result);
				$('.item').remove();
				for(var r of result){
					var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
					$('#title').after(item_el);
				}
			}).fail(function(result){
				console.log('์—๋Ÿฌ');
				console.log(result);
			});;
		});

		$('#bakery').on('click', function(){

		
			
			$.ajax({
				type: 'GET',
				url: '/product/๋ฒ ์ด์ปค๋ฆฌ',
				dataType: 'json'
			}).done(function(result){
				type="๋ฒ ์ด์ปค๋ฆฌ";
				console.log(result);
				$('.item').remove();
				for(var r of result){
					var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
					$('#title').after(item_el);
				}
				
			}).fail(function(result){
				console.log('์—๋Ÿฌ');
				console.log(result);
			});;
		});


		$('#coffee').on('click', function(){
	
			$.ajax({
				type: 'GET',
				url: '/product/์ปคํ”ผ',
				dataType: 'json'
			}).done(function(result){
				type="์ปคํ”ผ";
				console.log(result);
				$('.item').remove();
				for(var r of result){
					var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
					$('#title').after(item_el);
				}
			}).fail(function(result){
				console.log('์—๋Ÿฌ');
				console.log(result);
			});;
		});

		

		$('#price_desc').on('click', function(){

			$.ajax({
				type: 'GET',
				url: '/product/price/'+type,
				dataType: 'json'
			}).done(function(result){
				console.log(result);
				$('.item').remove();
				for(var r of result){
					var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
					$('#title').after(item_el);
				}
			}).fail(function(result){
				console.log('์—๋Ÿฌ');
				console.log(result);
			});;
		});

		$('#order_desc').on('click', function(){

			$.ajax({
				type: 'GET',
				url: '/product/orderByCount/'+type,
				dataType: 'json'
			}).done(function(result){
				console.log(result);
				$('.item').remove();
				for(var r of result){
					var item_el= "<tr class='item'><td>"+r.id+"</td><td>"+r.name+"</td><td>"+r.price+"</td><td>"+r.orderCount+"</td><td>"+r.type+"</td></tr>";
					$('#title').after(item_el);
				}
			}).fail(function(result){
				console.log('์—๋Ÿฌ');
				console.log(result);
			});;
		});

		

		
	</script>
</body>
</html>

 

ajax url์— ๋ฒ ์ด์ปค๋ฆฌ, ์ปคํ”ผ๋ฅผ ํ•œ๊ธ€๋กœ ๋‚ด๊ฐ€ ํƒ€์ดํ•‘ํ–ˆ๋Š”๋ฐ ์ง„๋„๋ฅผ ๋‚˜๊ฐ€๋‹ค๋ณด๋‹ˆ ๊ทธ ์•ˆ์—์„œ value๊ฐ’์„ ๋“ค๊ณ ์™€์„œ ํ•˜๋‚˜์˜ jax๋ฌธ์œผ๋กœ ํ•ฉ์น  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹น.

๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ ์–ด๋–ค ํƒ€์ž…์œผ๋กœ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฟŒ๋ ค์ ธ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” let type๋Š” ์ž๋ฐ”์˜ static์˜ ๊ธฐ๋Šฅ๊ณผ๋„ ๋น„์Šทํ•œ let์„ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•˜์—ฌ์„œ ์‰ฝ๊ฒŒ ๋ฟŒ๋ฆด์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ด๋Ÿฐ ๊ธฐ๋Šฅ์ด ์—†์—‡๋”๋ผ๋ฉด ๋ฒ„ํŠผ์„ ํด๋ฆญํ• ๋•Œ๋งˆ๋‹ค ์–ด๋– ํ•œ ๋ณ€์ˆ˜์— ์ด๊ฒƒ์„ ๋‹ด์•˜์–ด์•ผ๊ฒ ์ง€..

 

 

- ProductController

package com.seoha.product.controller;

import java.util.List;

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.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seoha.product.model.Product;
import com.seoha.product.repository.ProductRepository;

@Controller
public class ProductController {

	@Autowired
	private ProductRepository productRepository;

	@GetMapping({ "", "/" })
	public String index(Model model) {
		
		List<Product> products = productRepository.findAll();
		model.addAttribute("products", products);
		return "product";
		
	}

	@GetMapping("/product")
	public @ResponseBody List<Product> product() {
		List<Product> products = productRepository.findAll();
		return products;
	}
	
	@GetMapping("/product/{type}")
	public @ResponseBody List<Product> productType(@PathVariable String type){
		List<Product> products = productRepository.findType(type);
		return products;
	}

	@GetMapping("/product/orderByCount/{type}")
	public @ResponseBody List<Product> productOrderbyCount(@PathVariable String type){
		List<Product> products = productRepository.orderByCount(type);
		return products;
	}
	
	@GetMapping("/product/price/{type}")
	public @ResponseBody List<Product> productOrderbyprice(@PathVariable String type){
		List<Product> products = productRepository.orderByPrice(type);
		return products;
	}
	
}

 

 

- product.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="com.seoha.product.repository.ProductRepository">

	<select id="findAll" resultType="com.seoha.product.model.Product">
		select * from product
	</select>
	
	<select id="findType" resultType="com.seoha.product.model.Product">
		select * from product where type= #{type}
	</select>

	<select id="orderByPrice" resultType="com.seoha.product.model.Product">
		select * from product where type= #{type} order by price desc;
	</select>
	
	<select id="orderByCount" resultType="com.seoha.product.model.Product">
		select * from product where type= #{type} order by orderCount desc;
	</select>


</mapper>

 

 

- ProductRepository

package com.seoha.product.repository;

import java.util.List;

import com.seoha.product.model.Product;

public interface ProductRepository {
	List<Product> findAll();
	List<Product> findType(String type);
	List<Product> orderByPrice(String type);
	List<Product> orderByCount(String type);
}	

 

 

 

 

์ง„์งœ 1๋„ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ์•Œ๊ฒ ๋‹ค..

๋‹ค๋ฅธ๊ฒŒ ๋งˆ๋ฒ•์ด ์•„๋‹ˆ๊ณ  ์ด๊ฒŒ ๋งˆ๋ฒ•์ด๋„ค..