이미지 파일 불러오기(select-image)(java)

2024. 6. 20. 14:31JSP+Spring Boot

PigController

package com.khmall.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.RequestMapping;

import com.khmall.dto.Pig;
import com.khmall.service.PigService;

@Controller
public class PigController {
	
	@Autowired
	private PigService pigService;
	
	@GetMapping("/")
	public String getAllPigs(Model model) {
		
		List<Pig> pigList = pigService.getAllPigs();
		model.addAttribute("pigList", pigList);
		
		return "index";
	}
	
	@GetMapping("/pigDetail/{pig_id}")
	public String getPigById(Model model, @PathVariable int pig_id) {
		
		Pig pig = pigService.getPigById(pig_id);
		model.addAttribute("pig",pig);
		return "pigDetail";
	}
}

 

---------------------------------------------------------------------------------------------------------

 

Pig (DTO)

package com.khmall.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Pig {
	private int pig_id;
	private String pig_name;
	private int pig_age;
	private String pig_image_path;
}

 

-----------------------------------------------------------------------------------------------------------

 

PigMapper

 

package com.khmall.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.khmall.dto.Pig;

@Mapper
public interface PigMapper {

	List<Pig> getAllPigs();
	
	Pig getPigById(int pig_id);
}

 

------------------------------------------------------------------------------------------------------------

 

PigService

package com.khmall.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.khmall.dto.Pig;
import com.khmall.mapper.PigMapper;

@Service
public class PigService {

	@Autowired
	private PigMapper pigMapper;
	
	public List<Pig> getAllPigs(){
		return pigMapper.getAllPigs();
	}
	
	public Pig getPigById(int pig_id) {
		return pigMapper.getPigById(pig_id);
	}
}