Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

JAVA Developer Training

25. JAVA spring 이용 본문

트레이닝

25. JAVA spring 이용

Romenest 2021. 9. 6. 17:06

JAVA spring 설정 확인 할 것

 

홈 컨트롤러

package com.example.controller;

import java.util.List;

import com.example.entity.Item;
import com.example.repository.ItemRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HomeController {

    @Autowired
    ItemRepository iRepository;

	//url 설정하는것 , get,post 확인
    
    // http:/127.0.0.1:8080/home
    @RequestMapping(value = "home", method = RequestMethod.GET)
    public String home() {
        return "home"; // home.jsp
    }

    // http:/127.0.0.1:8080/join
    @RequestMapping(value = "join", method = RequestMethod.GET)
    public String join() {
        return "join"; // join.jsp
    }

    // http:/127.0.0.1:8080/insertitem 크롬에서 입력 GET
    @RequestMapping(value = "insertitem", method = RequestMethod.GET)
    public String insertitem() {
        return "insertitem"; // WEB-INF?Views/insertitem.jsp 표시
    }

    // post, submit 사용시
    @RequestMapping(value = "insertitem", method = RequestMethod.POST)
    public String insertitem(@ModelAttribute Item item) {

        // http:/127.0.0.1:8080/insertitem 크롬에서 입력 GET
        System.out.println(item.toString());
        iRepository.save(item);
        return "redirect:/insertitem"; // a태그 자동화
    }

    // http:/127.0.0.1:8080/selectitem 크롬에서 입력 GET
    @RequestMapping(value = "selectitem", method = RequestMethod.GET)
    public String selectitem(Model model) {
        // DB에서 가져오기
        List<Item> list = iRepository.findAll();
        // jsp로 값 전달
        model.addAttribute("list", list);
        // WEB-INF?Views/selectitem.jsp 표시
        return "selectitem";
    }

    // http:/127.0.0.1:8080/selectitem1 크롬에서 입력 GET
    @RequestMapping(value = "selectitem1", method = RequestMethod.GET)
    public @ResponseBody List<Item> selectitem1() {

        List<Item> list = iRepository.findAll();

        return list;
    }

    // http:/127.0.0.1:8080/deleteitem 크롬에서 입력 GET
    @RequestMapping(value = "deleteitem", method = RequestMethod.GET)
    public @ResponseBody int deleteitem() {
        try {
            iRepository.deleteById(1L);
            return 1;
        } catch (Exception e) {
            return 0;

        }

    }

    @RequestMapping(value = "updateitem", method = RequestMethod.POST)
    public String updateitem(@ModelAttribute Item item) {

        System.out.println(item.toString());
        iRepository.save(item);
        return "redirect:/updateitem";
    }
}

Item.java

package com.example.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

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

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(collection = "boot_item")
public class Item {

    @Id
    private long id; //_id 기본키설정

    @Field(name = "name")
    private String name = null;

    @Field(name = "text")
    private String text = null;

    @Field(name = "price")
    private int price = 0;

    @Field(name = "quantity")
    private long quantity = 0L;
}

ItemRepository.java

package com.example.repository;

import com.example.entity.Item;

import org.springframework.data.mongodb.repository.MongoRepository;

// MongoRepository<엔티티, 그엔티티에서의 기본키 타입>
public interface ItemRepository extends MongoRepository<Item, Long> {

    //인터페이스기에 구현은 못하고
    // public int findById(){

    // }
    
}

'트레이닝' 카테고리의 다른 글

27. 데이터 크롤링 ( node.js, selenium )  (0) 2021.09.10
26. socket.io를 이용한 실시간 채팅 기초 틀  (0) 2021.09.08
24. EnrollCourseDB 교수정보 받기  (0) 2021.09.02
23. EnrollCourseDB  (0) 2021.09.02
22. CourseDB  (0) 2021.09.02