Spring

Spring 일지 #74 (20211019) 시험 대비(물품 등록, 조회)

uni5948 2021. 10. 20. 23:54

74. 물품 등록, 조회

-물품 등록, 조회

 *service 생성(ItemService.java)

ItemService.java

package com.example.service;

 

import java.util.List;

 

import com.example.entity.Item;

 

import org.springframework.stereotype.Service;

 

@Service

public interface ItemService {

    //물품 등록

    public int insertItem(Item item);

 

    //물품 전체 조회

    public List<Item> selectItemList();

}


 *Impl 생성(ItemServiceImpl.java)

ItemServiceImpl.java

package com.example.service;

 

import java.util.List;

 

import com.example.entity.Item;

import com.example.mapper.ItemMapper;

 

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

import org.springframework.stereotype.Service;

 

@Service

public class ItemServiceImpl implements ItemService{

 

    @Autowired

    ItemMapper iMapper;

 

    @Override

    public int insertItem(Item item) {

        return iMapper.queryInsertItem(item);

    }

 

    @Override

    public List<Item> selectItemList() {

        return iMapper.querySelectList();

    }

}

 

 * insert추가(RestItemController.java)

package com.example.controller;

더보기

 

import java.util.HashMap;

import java.util.Map;

 

import com.example.entity.Item;

import com.example.entity.Member;

import com.example.service.ItemService;

 

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

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

@RestController

@RequestMapping(value = "/item")

public class RestItemController {

    @Autowired

    ItemService iService;

더보기

 

    //127.0.0.1:8080/ROOT/item/home.json

    @GetMapping(value = "/home.json",

        consumes = MediaType.ALL_VALUE,

        produces = MediaType.APPLICATION_JSON_VALUE)

    public Object homeGET(){

        Map<String, Object> map = new HashMap<>();

        map.put("status", 200);

        return map;

    }

 

     //127.0.0.1:8080/ROOT/item/insert.json

     @PostMapping(value = "/insert.json",

     consumes = MediaType.ALL_VALUE,

     produces = MediaType.APPLICATION_JSON_VALUE)

    public Object insertItemPOST(@RequestBody Item item){

        Map<String, Object> map = new HashMap<>();

        try{

            Member member = new Member();

            member.setId("a");

            item.setMember(member);

 

            iService.insertItem(item);

            map.put("status", 200);

        }

        catch(Exception e){

            e.printStackTrace();

            map.put("status", e.hashCode());

        }

        return map;

    }

}

 

 *Application 수정

package com.example.b20211019;

 

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.domain.EntityScan;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

 

@SpringBootApplication

@EnableJpaRepositories(basePackages = "com.example.repository")

@EntityScan(basePackages = {"com.example.entity"})

@ComponentScan(basePackages = { "com.example.controller",

"com.example.service", "com.example.config" })

@MapperScan(basePackages = "com.example.mapper")

public class B20211019Application {

 

    public static void main(String[] args) {

        SpringApplication.run(B20211019Application.class, args);

        System.out.println("start");

    }

}

 

 *물품 등록.

물품등록
등록 확인

 

  * select 추가 (RestItemController.java)

... ...

    //127.0.0.1:8080/ROOT/item/select.json

    @GetMapping(value = "/select.json",

    consumes = MediaType.ALL_VALUE,

    produces = MediaType.APPLICATION_JSON_VALUE)

        public Object selectitemGET(){

    Map<String, Object> map = new HashMap<>();

    try{

        map.put("status", 200);

        map.put("list", iService.selectItemList());

    }

    catch(Exception e){

        e.printStackTrace();

        map.put("status", e.hashCode());

    }

    return map;

  }

}

 

 *목록 조회

목록 조회