오늘 배운 것
MyBatis XML을 이용하여 사용하기
package com.myapp.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.myapp.mybatis.model.Product;
@Mapper
public interface ProductMapper {
Product selectProductById(Long id);
List<Product> selectAllProducts();
void insertProduct(Product product);
void updateProduct(Product product);
void deleteProductById(Long id);
}
- resources 폴더 안에 mapper폴더 생성 후 안에 .xml파일 생성
- mapper 태그안에 추상메소드를 입력한 Interface파일의 위치를 설정
- id는 추상메소드이름, resultType은 결과를 저장하고 Return할 객체
parameterType은 입력한 데이터를 받을 객체
<?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.myapp.mybatis.mapper.ProductMapper">
<select id="selectProductById" resultType="Product">
SELECT prod_id
,prod_name
,prod_price
FROM products
WHERE prod_id = #{prodId}
</select>
<select id="selectAllProducts" resultType="Product">
SELECT prod_id
,prod_name
,prod_price
FROM products
</select>
<insert id="insertProduct" parameterType="Product">
INSERT INTO products (prod_name, prod_price)
VALUES (#{prodName}, #{prodPrice})
</insert>
<update id="updateProduct" parameterType="Product">
UPDATE products
SET prod_name=#{prodName}, prod_price=#{prodPrice}
WHERE prod_id=#{prodId}
</update>
<delete id="deleteProductById">
DELETE FROM products
WHERE prod_id=#{prodId}
</delete>
</mapper>
- View가 필요없는 RestController라서 Postman으로 테스트
package com.myapp.mybatis.controller;
import java.util.List;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.myapp.mybatis.mapper.ProductMapper;
import com.myapp.mybatis.model.Product;
@RestController
@RequestMapping("/products")
public class ProductController {
private ProductMapper productMapper;
public ProductController(ProductMapper productMapper) {
this.productMapper = productMapper;
}
@GetMapping("/{id}")
public Product getProduct(@PathVariable("id") Long prodId) {
Product product = productMapper.selectProductById(prodId);
return product;
}
@GetMapping
public List<Product> getproductList() {
List<Product> products = productMapper.selectAllProducts();
return products;
}
@PostMapping
public void createProduct(@RequestParam("prodName") String prodName, @RequestParam("prodPrice") int prodPrice) {
productMapper.insertProduct(new Product(prodName, prodPrice));
}
@PutMapping("/{id}")
public void updateProduct(@PathVariable("id") Long prodId ,@RequestParam("prodName") String prodName, @RequestParam("prodPrice") int prodPrice) {
productMapper.updateProduct(new Product(prodId, prodName, prodPrice));
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable("id") Long prodId) {
productMapper.deleteProductById(prodId);
}
}
