My footsteps

CRUD 연습 / 0816 본문

Develop/곤부📙

CRUD 연습 / 0816

밀김 2023. 8. 16. 17:35
728x90

 

 

 

 

 

 

 

 

 

- useRoute 메서드(사용시 import 필요) : 라우터 폴더....?

 

- 라우터는 경로를 찾아주는 애임

 

- reactive?

 

- 마운트가되면 자스랑 템플릿이 읽혀야 한다

 

https://vuejs.org/api/composition-api-lifecycle.html#onmounted

 

Composition API: Lifecycle Hooks | Vue.js

 

vuejs.org

 

 

 

 

 

 

- 꼰뜨롤라

package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.Restaurant;
import com.example.demo.service.RestaurantService;

@RequestMapping("restaurant")
@RestController
public class RestaurantController {

    // 리스트, 디테일, 추가, 업데이트, 삭제
    @Autowired
    private RestaurantService service;

    @GetMapping("list")
    public List<Restaurant> list(){
        return service.findAll();
    }

    @GetMapping("{id}")
    public Restaurant list(@PathVariable("id") int id){
        Restaurant restaurant = service.getDetail(id);
        return restaurant;
    }

    @PostMapping("add")
    public int add(Restaurant restaurant){
        return service.add(restaurant);
    }

    @PutMapping("update")
    public int update(@RequestBody Restaurant restaurant){
        return service.update(restaurant);
    }

    @DeleteMapping("{id}/delete")
    public int delete(@PathVariable("id") int id){
        return service.delete(id);
    }


}

 

 

 

 

 

- 서비스

package com.example.demo.service;

import java.util.List;

import com.example.demo.entity.Restaurant;

public interface RestaurantService {

    List<Restaurant> findAll();

	int add(Restaurant restaurant);

    Restaurant getDetail(int id);

    int update(Restaurant restaurant);

    int delete(int id);

}

 

 

 

 

- 서비스 임프

package com.example.demo.service;

import java.util.List;

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

import com.example.demo.entity.Restaurant;
import com.example.demo.repository.RestaurantRepository;

@Service
public class RestaurantServiceImp implements RestaurantService {

    @Autowired
    private RestaurantRepository repository;

    @Override
    public List<Restaurant> findAll() {
        return repository.getList();
    }

    @Override
    public Restaurant getDetail(int id) {
        return repository.find(id);
    }

    @Override
    public int add(Restaurant restaurant) {
        return repository.insert(restaurant);
    }

    @Override
    public int update(Restaurant restaurant) {
        return repository.update(restaurant);
    }

    @Override
    public int delete(int id) {
        return repository.delete(id);
    }

}

 

 

 

- 레퍼지토리

package com.example.demo.repository;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.example.demo.entity.Restaurant;

@Mapper
public interface RestaurantRepository {

    List<Restaurant> getList();

    int insert(Restaurant restaurant);

    Restaurant find(int id);

    int update(Restaurant restaurant);

    int delete(int id);

}

 

 

 

 

- 매퍼

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.repository.RestaurantRepository">

    <select id="getList" resultType="Restaurant">
        select * from restaurant_study
    </select>

    <select id="find" resultType="Restaurant">
        select * from restaurant_study where id = #{id}
    </select>

    <insert id="insert" parameterType="Restaurant">
        insert into 
            restaurant_study
                (category_id, name, img, intro, address, operating_time, contact_number)
        values
            (#{categoryId}, #{name}, #{img}, #{intro}, #{address}, #{operatingTime}, #{contactNumber})
    </insert>

	<update id="update" parameterType="Restaurant">
        update restaurant_study
        set
            name = #{name},
            category_id = #{categoryId},
            img = #{img},
            intro = #{intro},
            address = #{address},
            operating_time = #{operatingTime},
            contact_number = #{contactNumber}
        where id = #{id}        
    </update>

    <delete id="delete" parameterType="int">
        delete from 
            restaurant_study 
        where id = #{id}
    </delete>

</mapper>

 

 

 

 

 

 

728x90

'Develop > 곤부📙' 카테고리의 다른 글

스터디 / 0821  (0) 2023.08.21
스터디 / 0819  (0) 2023.08.19
CRUD 연습 / 0808  (0) 2023.08.08
코딩애플 / Vue.js  (0) 2023.08.04
코딩애플 / Javascript (기초)  (0) 2023.08.01