본문 바로가기

수업 복습하기/Spring

day 06 - Spring 05 + 마무리

728x90
728x90

day 05에 select 많이 했으니까

day 06에는 insert update delete 연습한다!

 

왜 mapper03(인터페이스에서) insertcustomer에 타입이 int인가?

executeupdate()를사용했다. insert update delete를 사용하고 싶을 때!

그 때, 영향을 미친 레코드의 개수가 몇개인지를 확인하기 위해서 type = int인것이다!

 

< Controller12.java >

package org.zerock.controller.p05controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.zerock.controller.p05controller.bean.Bean17;
import org.zerock.controller.p05controller.bean.Bean18;
import org.zerock.mapper.p05mapper.Mapper03;

import lombok.Setter;

@Controller
@RequestMapping("cont12")
public class Controller12 {
	
	@Setter(onMethod_=@Autowired)
	private Mapper03 mapper;
	@RequestMapping("/met01")
	
	private void method01() {
		String customerName = "ironman";
		String contactName="tony";
		
		int cnt = mapper.insertCustomer(customerName, contactName);
		System.out.println(cnt);
	}
	
	@RequestMapping("/met02")
	public void method02() {
		String supplierName = "captain";
		String contactName = "steve";
		
		int cnt = mapper.insertSupplier(supplierName, contactName);
		System.out.println(cnt);
	}
	
	@RequestMapping("/met03")
	public void method03() {
		// 2. request 분석/가공
		Bean17 bean = new Bean17();
		bean.setContactName("peter");
		bean.setCustomerName("spiderman");
		bean.setAddress("queens");
		bean.setCity("ny");
		bean.setPostalCode("2222");
		bean.setCountry("usa");
		
		// 3. business logic
		mapper.insertCustomer2(bean);
		
	}
	
	@RequestMapping("/met04")
	private void method04() {
		//2. bean 작성 (Bean18)
		Bean18 bean = new Bean18();
		
		bean.setSupplierName("trump");
		bean.setAddress("seoul");
		bean.setCity("busan");
		bean.setCountry("korea");
		bean.setPhone("00000");
		bean.setPostalCode("1111");
		bean.setContactName("donald");
		
		//3. mapper 실행
		mapper.insertSupplier2(bean);
	}
	
	@RequestMapping("/met05")
	private void method05() {
		//2
		Bean17 bean = new Bean17();
		bean.setAddress("gangnam");
		bean.setCity("seoul");
		bean.setContactName("marvel");
		bean.setCustomerName("dangers");
		bean.setCountry("france");
		bean.setPostalCode("99999");
		
		//3.
		//insert 하기 전 id
		System.out.println(bean.getId()); //null or 0
		
		mapper.insertCustomer3(bean);
		
		System.out.println(bean.getId()); //key
	}
	
	@RequestMapping("/met06") 
	public void method06() {
		// 2
		Bean18 bean = new Bean18();
		bean.setAddress("yeoksam");
		bean.setCity("incheon");
		bean.setContactName("deadpool");
		bean.setSupplierName("wade");
		bean.setCountry("uk");
		bean.setPostalCode("3333");
		bean.setPhone("111");
		
		// 3
		System.out.println(bean.getSupplierID()); // null or 0
		mapper.insertSupplier3(bean);
		System.out.println(bean.getSupplierID()); // key
	}
	
	@RequestMapping("/met07")
	public void method07() {
		// 2
		Bean17 bean = new Bean17();
		bean.setId(107);
		bean.setContactName("widow");
		bean.setCustomerName("nat");
		bean.setAddress("jongro");
		bean.setCity("dokdo");
		bean.setCountry("korea");
		bean.setPostalCode("77777");
		
		// 3
		int cnt = mapper.updateCustomer(bean);
		System.out.println(cnt);
	}
	
	@RequestMapping("/met08")
	public void method08() {
		Bean18 bean = new Bean18();
		bean.setSupplierID(35);
		bean.setAddress("gangnam");
		bean.setCity("busan");
		bean.setContactName("wolverine");
		bean.setCountry("canada");
		bean.setPhone("0000000");
		bean.setPostalCode("99999");
		bean.setSupplierName("logan");
		
		int cnt = mapper.updateSupplier(bean);
	}
	
	@RequestMapping("/met09")
	public void method09(Integer id) {
		int cnt = mapper.deleteCustomer(id);
	}
	
	// TODO : delete Supplier by id
	@RequestMapping("/met10")
	public void method10(Integer id) {
		int cnt = mapper.deleteSupplier(id);
	}
}

< Mapper03.java >

package org.zerock.mapper.p05mapper;

import org.apache.ibatis.annotations.Param;
import org.zerock.controller.p05controller.bean.Bean17;
import org.zerock.controller.p05controller.bean.Bean18;

public interface Mapper03 {
	public int insertCustomer(@Param("customerName") String customerName,@Param("contactName")String contactName);

	public int insertSupplier(@Param("supplierName")String supplierName, @Param("contactName")String contactName);

	public int insertCustomer2(Bean17 bean);
	
	public int insertSupplier2(Bean18 bean);

	public int insertCustomer3(Bean17 bean);

	public int insertSupplier3(Bean18 bean);
	
	public int updateCustomer(Bean17 bean);

	public int updateSupplier(Bean18 bean);

	public int deleteCustomer(Integer id);

	public int deleteSupplier(Integer id);
}

< Mapper03.xml >

<?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="org.zerock.mapper.p05mapper.Mapper03">

	<insert id="insertCustomer">
		INSERT INTO Customers(CustomerName,ContactName)
		VALUES (#{customerName},#{contactName})
	</insert>

	<insert id="insertSupplier">
		INSERT INTO Suppliers(SupplierName,ContactName)
		VALUES (#{supplierName},#{contactName})
	</insert>

	<insert id="insertCustomer2">
		INSERT INTO Customers (CustomerName, ContactName,
		Address, PostalCode,
		Country, City)
		VALUES (#{customerName},
		#{contactName}, #{address}, #{postalCode},
		#{country}, #{city})
	</insert>

	<insert id="insertSupplier2">
		INSERT INTO
		Suppliers(SupplierName,ContactName,Address,PostalCode, Country,
		Phone)
		VALUES
		(#{supplierName},#{contactName},#{address},#{postalCode},#{country},#{phone})
	</insert>

	<insert id="insertCustomer3" useGeneratedKeys="true"
		keyProperty="id" keyColumn="customerID">
		INSERT INTO Customers (CustomerName,
		ContactName, Address, PostalCode,
		Country, City)
		VALUES
		(#{customerName}, #{contactName}, #{address}, #{postalCode},
		#{country}, #{city})
	</insert>

	<insert id="insertSupplier3" useGeneratedKeys="true"
		keyProperty="supplierID" keyColumn="supplierID">
		INSERT INTO
		Suppliers(SupplierName,ContactName,Address,PostalCode, Country, Phone)
		VALUES
		(#{supplierName},#{contactName},#{address},#{postalCode},#{country},#{phone})
	</insert>

	<update id="updateCustomer">
		UPDATE Customers
		SET
		CustomerName = #{customerName},
		ContactName = #{contactName},
		Address = #{address},
		PostalCode = #{postalCode},
		Country = #{country},
		City = #{city}
		WHERE
		CustomerID = #{id}
	</update>

	<update id="updateSupplier">
		UPDATE Suppliers
		SET
		SupplierName = #{supplierName},
		ContactName = #{contactName},
		Address =
		#{address},
		City = #{city},
		PostalCode = #{postalCode},
		Country =
		#{country},
		Phone = #{phone}
		WHERE
		SupplierID = #{supplierID}
	</update>

	<delete id="deleteCustomer">
		DELETE FROM Customers WHERE CustomerId = #{id}
	</delete>

	<delete id="deleteSupplier">
		DELETE FROM Suppliers WHERE SupplierId = #{id}
	</delete>
</mapper>

< Bean17.java >

package org.zerock.controller.p05controller.bean;

import lombok.Data;

@Data
public class Bean17 {
	private Integer id;
	private String customerName;
	private String contactName;
	private String city;
	private String address;
	private String postalCode;
	private String country;
}

< Bean18.java >

package org.zerock.controller.p05controller.bean;

import lombok.Data;

@Data
public class Bean18 {
	private Integer supplierID;
	private String supplierName;
	private String contactName;
	private String address;
	private String city;
	private String postalCode;
	private String country;
	private String phone;
}

/met01사용한 후에 mysql 가서 조회해보면 토니 늘어난게 확인 가능하다

+) int cnt = mapper.insertCustomer(customerName, contactName);
System.out.println(cnt);

이렇게 하고, 서버 met01페이지 업데이트 하면 콘솔창에 영향을 준 레코드로 1이 뜨는 것을 확인 할 수 있다.

 

/met02 : 선생님이 controller 작성해주시고, mapper03 인터페이스랑 xml 만들기!

 

+)pair programming

ㅋㅋㅋ훈수두는사람이 잘볼 수 있는 동료 프로그램 : 에자일 하나의 컴ㅍ터에서 두사람의 프로그래머가 작업하는 방법

 

/met03

param의 개수가 너무 많아지다보면, 인터페이스에 죽 써서 내려가기보다는, 차라리

Dto를 만들어서, 하나의 값으로 넘겨주는게 오히려 나을 수도 있다.

우선 bean으로 넣는건 또 처음이라서, 우선 직접 작성해놓은 request를 넣는거로!

id는 우리가 넣는게 아니라 알아서 넣어주는 거기때문에 따로 삽입할 필요가 없다.

mapper.xml에서 id는 유일해야하기 때문에, 일부로 이번 id에는 2를 넣어서 다른 id로 만들어 주었다.

insert 쿼리 작성 할 때, 순서 상관없으니까 맘대로 편하게 쓰기

+) 파라미터를 하나만 넣었을 때는 쿼리 작성시 파라미터 명 없이, property만 작성하면 된다. (알아서 잘하네~)

 

/met04

met03 활용해서 문제풀기 

 

/met05

insert 하자마자 select 하는게 위험하기 때문에, 방지 하기 위해서 하는 일

+) oracle에서는 sequence가 따로관리하지만 mysql은 그렇지 않기 때문에 하는 일 

bean17을 다시 활용해서 키 값 알아보기

기본적으로 system.out.println(bean.getCustomerID()); : null 또는 0이다. 

private type = 0 / mapper type = null 일 것.

 

어느 key property에 넣을 것인가 (set 생략)id = bean이 가지고있는 property 명

key column이 뭔지도 알려줘야 한다. (두개가 같을 때도 있고, 다를 때도 있으니까 주의하기) 

 

/met06 

met05번 가지고 다시 복습해보는 문제풀이

 

/met07

update 쿼리 작성해서 메소드 실행해보기 문제

마찬가지로 return타입이 없다 -> 몇개에 영향을 미쳤는지를 더중요하게 보기 때문에

#{?} ?에는 bean 메소드 이름을 적으면 된다.

 

controller에 바꾸고 싶은 column을 bean.setId(바꾸고싶은 column 번호 ) ;로 입력한다

 

/met08

met07번 가지고 다시 복습해보는 문제풀이

 

/met09 + /met10

id를 request parameter로 받아서 풀어보는 문제 

mysql에 내가 삭제하고싶은 id번호 확인하고 삽인해서 문제 풀어보기

728x90