4월 13일

SJY0000·2022년 4월 13일
0

Springboot

목록 보기
14/24

오늘 배운 것

  • MyBatis

MyBatis

  • JPA를 대신하여 간편하게 DB에 접근하여 사용할 수 있는 라이브러리
  • JPA보다 가볍고 복잡한 쿼리문을 사용하기에 더 간편하고 한국, 중국, 일본에서 많이 사용함(다른나라는 JPA를 사용)

참고(MyBatis에 대하여)

https://kils-log-of-develop.tistory.com/576

참고(JPA와 MyBatis 장단점)

https://velog.io/@gkskaks1004/JPA%EC%99%80-MyBatis%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EB%A9%B4%EC%84%9C-%EC%9E%A5%EB%8B%A8%EC%A0%90

MyBatis 준비

  1. Pom.xml에 MyBatis FrameWork 추가
  2. MySQL에 mybatis DB 생성 및 Table 생성
  3. application.properties에 DB 설정 추가
# MySQL DB 설정
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=Asia/Seoul
spring.datasource.username=mysql아이디
spring.datasource.password=비밀번호

MyBatis 사용하기

  • MyBatis는 Repository 대신에 Mapper를 사용
  • sql문 종류에 따라 @어노테이션을 줘야함
package com.myapp.mybatis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.myapp.mybatis.model.User;

@Mapper // jpa의 repository와 같음
public interface UserMapper {

	@Select("select * from user where id = #{id}") // #{매개변수}, 메소드에 설정한 매개변수가 여기 들어감
	User getUser(String id); // id로 DB user테이블에서 찾아 User객체 Return

	@Select("select * from user")
	List<User> getUserList();

	@Insert("insert into user values (#{id}, #{name}, #{phone}, #{address})")
	void insertUser(String id, String name, String phone, String address);
	
	@Update("update user set name=#{name}, phone=#{phone}, address=#{address} where id=#{id}")
	int updateUser(String id, String name, String phone, String address);

	@Delete("delete from user where id=#{id}")
	void deleteUser(String id);
}
  • 간편하게 RestController로 사용(View로 이동하지 않고 바로 결과 출력)
  • 사용방법은 JPA와 같음
package com.myapp.mybatis.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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.myapp.mybatis.mapper.UserMapper;
import com.myapp.mybatis.model.User;

@RestController // view 없이 바로 return
@RequestMapping("/users")
public class UserController {

	@Autowired
	private UserMapper usermapper;
	
	@GetMapping("/{id}")
	public  User getUser(@PathVariable("id") String id) {
		// mybatis는 repository 대신 매퍼를 만듬 
		User user = usermapper.getUser(id);
				
		return user;
	}
	@GetMapping
	public List<User> getUserList() {
		List<User> userList = usermapper.getUserList();
		
		return userList;
	}
	@PostMapping
	public void createUser(@RequestParam("id") String id, @RequestParam("name") String name, 
							@RequestParam("phone") String phone, @RequestParam("address") String address) {
		
		usermapper.insertUser(id, name, phone, address);
	}
	
	@PutMapping("/{id}")
	public void updateUser(@PathVariable("id") String id, @RequestParam("name") String name, 
							@RequestParam("phone") String phone, @RequestParam("address") String address) {
		
		usermapper.updateUser(id, name, phone, address);
	}
	@DeleteMapping("/{id}")
	public void deleteUser(@PathVariable("id") String id) {
		usermapper.deleteUser(id);
	}
}

0개의 댓글